* Copyright © 2019 Strangecode, LLC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* * Logger * * A Codebase implementation of a Logger Interface as per: * https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md * * @author Quinn Comendant * @version 1.0 * @since 30 Jan 2019 14:30:26 */ class Logger implements \Psr\Log\LoggerInterface { /** * System is unusable. * * @param string $message * @param array $context * * @return void */ public function emergency($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_EMERG, __FILE__, __LINE__); } /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * * @return void */ public function alert($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_ALERT, __FILE__, __LINE__); } /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * * @return void */ public function critical($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_CRIT, __FILE__, __LINE__); } /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * * @return void */ public function error($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_ERR, __FILE__, __LINE__); } /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * * @return void */ public function warning($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_WARNING, __FILE__, __LINE__); } /** * Normal but significant events. * * @param string $message * @param array $context * * @return void */ public function notice($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_NOTICE, __FILE__, __LINE__); } /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * * @return void */ public function info($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_INFO, __FILE__, __LINE__); } /** * Detailed debug information. * * @param string $message * @param array $context * * @return void */ public function debug($message, array $context = array()) { $app =& App::getInstance(); $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_DEBUG, __FILE__, __LINE__); } /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * * @return void */ public function log($level, $message, array $context = array()) { $app =& App::getInstance(); switch ($level) { case LOG_EMERG: case LOG_ALERT: case LOG_CRIT: case LOG_ERR: case LOG_WARNING: case LOG_NOTICE: case LOG_INFO: case LOG_DEBUG: $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_CRIT, __FILE__, __LINE__); break; default: throw Psr\Log\InvalidArgumentException; break; } } /** * Interpolates context values into the message placeholders. */ // function interpolate($message, array $context = array()) // { // // build a replacement array with braces around the context keys // $replace = array(); // foreach ($context as $key => $val) { // // check that the value can be casted to string // if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { // $replace['{' . $key . '}'] = $val; // } // } // // // interpolate replacement values into the message and return // return strtr($message, $replace); // } }