diff options
Diffstat (limited to 'app')
-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 | ||||
-rw-r--r-- | app/ServiceProvider/LoggingProvider.php | 12 |
7 files changed, 359 insertions, 6 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); + } +} diff --git a/app/ServiceProvider/LoggingProvider.php b/app/ServiceProvider/LoggingProvider.php index cb6d0baa..eba9d0c4 100644 --- a/app/ServiceProvider/LoggingProvider.php +++ b/app/ServiceProvider/LoggingProvider.php @@ -5,11 +5,11 @@ namespace Kanboard\ServiceProvider; use Psr\Log\LogLevel; use Pimple\Container; use Pimple\ServiceProviderInterface; -use SimpleLogger\Logger; -use SimpleLogger\Stderr; -use SimpleLogger\Stdout; -use SimpleLogger\Syslog; -use SimpleLogger\File; +use Kanboard\Core\Log\Logger; +use Kanboard\Core\Log\Stderr; +use Kanboard\Core\Log\Stdout; +use Kanboard\Core\Log\Syslog; +use Kanboard\Core\Log\File; /** * Class LoggingProvider @@ -21,7 +21,7 @@ class LoggingProvider implements ServiceProviderInterface { public function register(Container $container) { - $logger = new Logger; + $logger = new Logger(); $driver = null; switch (LOG_DRIVER) { |