diff options
author | Frédéric Guillot <fred@kanboard.net> | 2018-03-05 12:04:28 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2018-03-05 12:04:28 -0800 |
commit | 299198f7181fccf1e9a684649d173a1ebbdfbd1e (patch) | |
tree | c2539efcfae3b193603bbff22dde8b84fd7f9981 /app/Core | |
parent | a991758e98fc614d35ec67ec904c78fa0ff6feeb (diff) |
Move SimpleLogger lib into app source tree
Diffstat (limited to 'app/Core')
-rw-r--r-- | app/Core/Log/Base.php | 89 | ||||
-rw-r--r-- | app/Core/Log/File.php | 48 | ||||
-rw-r--r-- | app/Core/Log/Logger.php | 94 | ||||
-rw-r--r-- | app/Core/Log/Stderr.php | 25 | ||||
-rw-r--r-- | app/Core/Log/Stdout.php | 25 | ||||
-rw-r--r-- | app/Core/Log/Syslog.php | 72 |
6 files changed, 353 insertions, 0 deletions
diff --git a/app/Core/Log/Base.php b/app/Core/Log/Base.php new file mode 100644 index 00000000..3c3c8485 --- /dev/null +++ b/app/Core/Log/Base.php @@ -0,0 +1,89 @@ +<?php + +namespace Kanboard\Core\Log; + +use Psr\Log\AbstractLogger; +use Psr\Log\LogLevel; + +/** + * Base class for loggers + * + * @package Kanboard\Core\Log + * @author Frédéric Guillot + */ +abstract class Base extends AbstractLogger +{ + /** + * Minimum log level for the logger + * + * @access private + * @var string + */ + private $level = LogLevel::DEBUG; + + /** + * Set minimum log level + * + * @access public + * @param string $level + */ + public function setLevel($level) + { + $this->level = $level; + } + + /** + * Get minimum log level + * + * @access public + * @return string + */ + public function getLevel() + { + return $this->level; + } + + /** + * Dump to log a variable (by example an array) + * + * @param mixed $variable + */ + public function dump($variable) + { + $this->log(LogLevel::DEBUG, var_export($variable, true)); + } + + /** + * Interpolates context values into the message placeholders. + * + * @access protected + * @param string $message + * @param array $context + * @return string + */ + protected function interpolate($message, array $context = array()) + { + // build a replacement array with braces around the context keys + $replace = array(); + + foreach ($context as $key => $val) { + $replace['{' . $key . '}'] = $val; + } + + // interpolate replacement values into the message and return + return strtr($message, $replace); + } + + /** + * Format log message + * + * @param mixed $level + * @param string $message + * @param array $context + * @return string + */ + protected function formatMessage($level, $message, array $context = array()) + { + return '['.date('Y-m-d H:i:s').'] ['.$level.'] '.$this->interpolate($message, $context).PHP_EOL; + } +} diff --git a/app/Core/Log/File.php b/app/Core/Log/File.php new file mode 100644 index 00000000..19260b3c --- /dev/null +++ b/app/Core/Log/File.php @@ -0,0 +1,48 @@ +<?php + +namespace Kanboard\Core\Log; + +use RuntimeException; + +/** + * File Logger + * + * @package Kanboard\Core\Log + * @author Frédéric Guillot + */ +class File extends Base +{ + /** + * Filename + * + * @access protected + * @var string + */ + protected $filename = ''; + + /** + * Setup logger configuration + * + * @param string $filename Output file + */ + public function __construct($filename) + { + $this->filename = $filename; + } + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + */ + public function log($level, $message, array $context = array()) + { + $line = $this->formatMessage($level, $message, $context); + + if (file_put_contents($this->filename, $line, FILE_APPEND | LOCK_EX) === false) { + throw new RuntimeException('Unable to write to the log file.'); + } + } +} diff --git a/app/Core/Log/Logger.php b/app/Core/Log/Logger.php new file mode 100644 index 00000000..4155a04e --- /dev/null +++ b/app/Core/Log/Logger.php @@ -0,0 +1,94 @@ +<?php + +namespace Kanboard\Core\Log; + +use Psr\Log\AbstractLogger; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; + +/** + * Handler for multiple loggers + * + * @package Kanboard\Core\Log + * @author Frédéric Guillot + */ +class Logger extends AbstractLogger implements LoggerAwareInterface +{ + /** + * Logger instances + * + * @access private + */ + private $loggers = array(); + + /** + * Get level priority + * + * @param mixed $level + * @return integer + */ + public function getLevelPriority($level) + { + switch ($level) { + case LogLevel::EMERGENCY: + return 600; + case LogLevel::ALERT: + return 550; + case LogLevel::CRITICAL: + return 500; + case LogLevel::ERROR: + return 400; + case LogLevel::WARNING: + return 300; + case LogLevel::NOTICE: + return 250; + case LogLevel::INFO: + return 200; + } + + return 100; + } + + /** + * Sets a logger instance on the object + * + * @param LoggerInterface $logger + * @return null + */ + public function setLogger(LoggerInterface $logger) + { + $this->loggers[] = $logger; + } + + /** + * Proxy method to the real loggers + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()) + { + foreach ($this->loggers as $logger) { + if ($this->getLevelPriority($level) >= $this->getLevelPriority($logger->getLevel())) { + $logger->log($level, $message, $context); + } + } + } + + /** + * Dump variables for debugging + * + * @param mixed $variable + */ + public function dump($variable) + { + foreach ($this->loggers as $logger) { + if ($this->getLevelPriority(LogLevel::DEBUG) >= $this->getLevelPriority($logger->getLevel())) { + $logger->dump($variable); + } + } + } +} diff --git a/app/Core/Log/Stderr.php b/app/Core/Log/Stderr.php new file mode 100644 index 00000000..fbe82a8c --- /dev/null +++ b/app/Core/Log/Stderr.php @@ -0,0 +1,25 @@ +<?php + +namespace Kanboard\Core\Log; + +/** + * Stderr logger + * + * @package Kanboard\Core\Log + * @author Frédéric Guillot + */ +class Stderr extends Base +{ + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()) + { + file_put_contents('php://stderr', $this->formatMessage($level, $message, $context), FILE_APPEND); + } +} diff --git a/app/Core/Log/Stdout.php b/app/Core/Log/Stdout.php new file mode 100644 index 00000000..99140801 --- /dev/null +++ b/app/Core/Log/Stdout.php @@ -0,0 +1,25 @@ +<?php + +namespace Kanboard\Core\Log; + +/** + * Stdout logger + * + * @package Kanboard\Core\Log + * @author Frédéric Guillot + */ +class Stdout extends Base +{ + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()) + { + file_put_contents('php://stdout', $this->formatMessage($level, $message, $context), FILE_APPEND); + } +} diff --git a/app/Core/Log/Syslog.php b/app/Core/Log/Syslog.php new file mode 100644 index 00000000..3a354a88 --- /dev/null +++ b/app/Core/Log/Syslog.php @@ -0,0 +1,72 @@ +<?php + +namespace Kanboard\Core\Log; + +use RuntimeException; +use Psr\Log\LogLevel; + +/** + * Syslog Logger + * + * @package Kanboard\Core\Log + * @author Frédéric Guillot + */ +class Syslog extends Base +{ + /** + * Setup Syslog configuration + * + * @param string $ident Application name + * @param int $facility See http://php.net/manual/en/function.openlog.php + */ + public function __construct($ident = 'PHP', $facility = LOG_USER) + { + if (! openlog($ident, LOG_ODELAY | LOG_PID, $facility)) { + throw new RuntimeException('Unable to connect to syslog.'); + } + } + + /** + * Get syslog priority according to Psr\LogLevel + * + * @param mixed $level + * @return integer + */ + public function getSyslogPriority($level) + { + switch ($level) { + case LogLevel::EMERGENCY: + return LOG_EMERG; + case LogLevel::ALERT: + return LOG_ALERT; + case LogLevel::CRITICAL: + return LOG_CRIT; + case LogLevel::ERROR: + return LOG_ERR; + case LogLevel::WARNING: + return LOG_WARNING; + case LogLevel::NOTICE: + return LOG_NOTICE; + case LogLevel::INFO: + return LOG_INFO; + } + + return LOG_DEBUG; + } + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()) + { + $syslogPriority = $this->getSyslogPriority($level); + $syslogMessage = $this->interpolate($message, $context); + + syslog($syslogPriority, $syslogMessage); + } +} |