aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Log.php
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2022-10-26 10:12:32 +0000
committerDavid Siegfried <david.siegfried@uni-vechta.de>2022-10-26 10:12:32 +0000
commitc16626dfcac357e339f48113eb646847af7dd6a9 (patch)
tree493c4ea0db09d905926fc0a2d0fa338c772e3a88 /lib/classes/Log.php
parentb8256a4732c53ca504bb8c17adcb47cfa799d81e (diff)
Use Monolog as PSR-11 compatible logger, refs #1686.
Merge request studip/studip!1117
Diffstat (limited to 'lib/classes/Log.php')
-rw-r--r--lib/classes/Log.php260
1 files changed, 28 insertions, 232 deletions
diff --git a/lib/classes/Log.php b/lib/classes/Log.php
index 335572d..4bb0e9d 100644
--- a/lib/classes/Log.php
+++ b/lib/classes/Log.php
@@ -1,256 +1,52 @@
<?php
+
+use Psr\Log\LoggerInterface;
+
/**
- * Log.php
- *
- *
- * 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 2 of
- * the License, or (at your option) any later version.
- *
- * Usage:
- * @code
- * //logging to $GLOBALS['TMP_PATH'] . '/studip.log'
- * Log::get()->setHandler($GLOBALS['TMP_PATH'] . '/studip.log');
- * Log::warn('log this'); //log a WARNING
- * Log::warning('log this'); //also log a WARNING
- * Log::w('log this'); //also log a WARNING
- * //create additional log
- * Log::set('my', '/tmp/mylog.txt');
- * Log::debug_my('debug to my');
- * //use self defined log handler
- * Log::get('my')
- * ->setHandler(function ($m) {
- * return mail( mail('noack@data-quest.de', '['.$m['level_name'].']', $m['formatted']););
- * });
- * Log::alert_my('log via mail');
- * @endcode
- *
- * @author André Noack <noack@data-quest.de>
- * @copyright 2012 Stud.IP Core-Group
- * @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
- * @category Stud.IP
- *
- * @method static mixed FATAL (string $message)
- * @method static mixed ALERT (string $message)
- * @method static mixed CRITICAL (string $message)
- * @method static mixed ERROR (string $message)
- * @method static mixed WARNING (string $message)
- * @method static mixed NOTICE (string $message)
- * @method static mixed INFO (string $message)
- * @method static mixed DEBUG (string $message)
-*/
+ * @method static void alert(string $message, array $context = [])
+ * @method static void critical(string $message, array $context = [])
+ * @method static void debug(string $message, array $context = [])
+ * @method static void emergency(string $message, array $context = [])
+ * @method static void error(string $message, array $context = [])
+ * @method static void info(string $message, array $context = [])
+ * @method static void log($level, string $message, array $context = [])
+ * @method static void notice(string $message, array $context = [])
+ * @method static void warning(string $message, array $context = [])
+ */
class Log
{
-
- const FATAL = 0; // All is lost
- const ALERT = 1; // Immediate action required
- const CRITICAL = 2; // Critical conditions
- const ERROR = 3; // An error occurred
- const WARNING = 4; // Something unexpected happening
- const NOTICE = 5; // Something worth noting
- const INFO = 6; // Information, not an error
- const DEBUG = 7; // Debugging messages
-
- /**
- * if string then complete path to logfile
- * if not schould be callable
- *
- * @var mixed
- */
- private $log_handler = null;
-
- /**
- * maximum log level
- *
- * @var integer
- */
- private $log_level = 6;
- /**
- * an array with log levels, taken from contants
- * .
- * @var array
- */
- private $log_level_names = [];
-
- /**
- * if log_handler is a string
- * the file pointer
- *
- * @var resource
- */
- private $file = null;
-
/**
- * array of used log instances
+ * The underlying logger.
*
- * @var array
+ * @var LoggerInterface
*/
- private static $instances = [];
+ protected static $instance;
/**
- * returns a log instance, identified by given name
- * if name is omitted, the default logger is returned
+ * Handle dynamic, static calls to the object.
*
- * @param string $name name of log instance
- * @throws InvalidArgumentException
- * @return Log
- */
- public static function get($name = '')
- {
- $name = mb_strlen($name) ? $name : 0;
- if ($name === 0 && !isset(self::$instances[$name])) {
- self::set();
- }
- if (!isset(self::$instances[$name])) {
- throw new InvalidArgumentException('Unknown logger: ' . $name);
- }
- return self::$instances[$name];
- }
-
- /**
- * sets a log handler for the named log instance
- * returns the old handler
- *
- * @param string $name
- * @param mixed $log_handler
+ * @param string $method
+ * @param array $args
* @return mixed
*/
- public static function set($name = '', $log_handler = null)
- {
- $name = mb_strlen($name) ? $name : 0;
- $old = self::$instances[$name] ?? null;
- self::$instances[$name] = new Log($log_handler);
- return $old;
- }
-
- /**
- * magic log, intercepts all static method calls
- * called method names are splitted by an underscore
- * first part denotes log level, second name of logger if any
- *
- * @param string $name
- * @param array $arguments
- * @return mixed number of written bytes or return value from callable handler
- */
- public static function __callStatic($name, $arguments)
- {
- list($level_name, $log_name) = explode('_', $name);
- $message = $arguments[0];
- return self::get($log_name)->{$level_name}($message);
- }
-
- /**
- * create new log instance with given handler
- *
- * @param mixed $log_handler
- */
- function __construct($log_handler = null)
- {
- $this->setHandler($log_handler);
- $r = new ReflectionClass($this);
- $this->log_level_names = array_flip($r->getConstants());
- }
-
- /**
- * set the maximum log level
- *
- * @param integer $level
- * @return integer
- */
- public function setLogLevel($level)
+ public static function __callStatic($method, $args)
{
- $old = $this->log_level;
- $this->log_level = $level;
- return $old;
- }
+ $instance = static::getInstance();
- /**
- * returns the current maximum log level
- *
- * @return integer
- */
- public function getLogLevel()
- {
- return $this->log_level;
+ return $instance->$method(...$args);
}
- /**
- * set the log handler
- * returns the old handler
- *
- * @param mixed $log_handler
- * @return mixed
- */
- public function setHandler($log_handler)
+ public static function getInstance(): LoggerInterface
{
- $old = $this->log_handler;
- $this->log_handler = $log_handler;
- if (is_resource($this->file)) {
- fclose($this->file);
+ if (!isset(static::$instance)) {
+ static::$instance = app(LoggerInterface::class);
}
- return $old;
- }
- /**
- * returns the current log handler
- *
- * @return mixed
- */
- public function getHandler()
- {
- return $this->log_handler;
+ return static::$instance;
}
- /**
- * log a message
- *
- * @param string $message the log message
- * @param integer $level log level, see constants
- * @return mixed number of written bytes or return value from callable handler
- */
- public function log($message, $level = Log::ERROR)
+ public static function setInstance(LoggerInterface $instance): void
{
- if (isset($this->log_handler) && $level <= $this->log_level) {
- $log_level_name = $this->log_level_names[$level];
- $formatted_message = date('c') . ' ['.$this->log_level_names[$level].'] ' . $message;
- if (is_callable($this->log_handler)) {
- $log_handler = $this->log_handler;
- return $log_handler(['formatted' => $formatted_message,
- 'message' => $message,
- 'level' => $level,
- 'level_name' => $this->log_level_names[$level],
- 'timestamp' => time()
- ]);
- } else {
- $logfile = $this->log_handler;
- $this->file = is_resource($this->file) ? $this->file : @fopen($logfile, 'ab');
- if ($this->file && flock($this->file , LOCK_EX)) {
- $ret = fwrite($this->file, date('c') . ' ['.$this->log_level_names[$level].'] ' . $message . "\n");
- flock($this->file, LOCK_UN);
- return $ret;
- } else {
- trigger_error(sprintf('Logfile %s could not be opened.', $logfile), E_USER_WARNING);
- }
- }
- }
- }
-
- /**
- * magic log, intercepts all undefined method calls
- * called method name must be log level name or part of
- *
- * @param string $name
- * @param array $arguments
- * @return mixed number of written bytes or return value from callable handler
- */
- public function __call($name, $arguments)
- {
- foreach ($this->log_level_names as $level_num => $level_name) {
- if (mb_stripos($level_name, $name) === 0) {
- return $this->log($arguments[0], $level_num);
- }
- }
- throw new BadMethodCallException('Unknown method called: ' . $name);
+ static::$instance = $instance;
}
}