Move SimpleLogger lib into app source tree

This commit is contained in:
Frédéric Guillot
2018-03-05 12:04:28 -08:00
parent a991758e98
commit 299198f718
16 changed files with 99 additions and 197 deletions

89
app/Core/Log/Base.php Normal file
View File

@@ -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;
}
}

48
app/Core/Log/File.php Normal file
View File

@@ -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.');
}
}
}

94
app/Core/Log/Logger.php Normal file
View File

@@ -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);
}
}
}
}

25
app/Core/Log/Stderr.php Normal file
View File

@@ -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);
}
}

25
app/Core/Log/Stdout.php Normal file
View File

@@ -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);
}
}

72
app/Core/Log/Syslog.php Normal file
View File

@@ -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);
}
}

View File

@@ -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) {