Wrap Monolog logger in custom logger

This commit is contained in:
Jeremy Dormitzer 2019-04-13 16:25:42 -04:00
parent 0231a41859
commit 45b441130a
2 changed files with 158 additions and 8 deletions

View File

@ -4,9 +4,8 @@ namespace ActivityPub\Config;
use ActivityPub\Objects\ContextProvider;
use ActivityPub\Objects\IdProvider;
use ActivityPub\Utils\Logger;
use Exception;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
/**
@ -296,10 +295,9 @@ class ActivityPubConfigBuilder
* This config parameter can be used to customize the log level and log destination via the PSR-3 LoggerInterface
* specification.
*
* Default: a Monolog Logger that prints all messages of level Info or higher to the SAPI logging handler.
* See https://github.com/Seldaek/monolog.
* Default: a logger that prints all messages of level Info or higher to the SAPI logging handler.
*
* @param LoggerInterface $logger A PSR3 LoggerInterface instance
* @param LoggerInterface $logger A PSR-3 LoggerInterface instance
* @return ActivityPubConfigBuilder The builder instance
*/
public function setLogger( LoggerInterface $logger )
@ -313,9 +311,7 @@ class ActivityPubConfigBuilder
*/
private function getDefaultLogger()
{
$logger = new Logger( 'ActivityPub-PHP' );
$logger->pushHandler( new ErrorLogHandler( ErrorLogHandler::SAPI ) );
return $logger;
return new Logger( 'ActivityPub-PHP' );
}
}

154
src/Utils/Logger.php Normal file
View File

@ -0,0 +1,154 @@
<?php
namespace ActivityPub\Utils;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger as MonoLogger;
use Psr\Log\LoggerInterface;
class Logger implements LoggerInterface
{
/**
* @var \Monolog\Logger
*/
private $monoLogger;
/**
* Logger constructor.
* @param int $level The log level at which to start logging. Default: INFO
*/
public function __construct( $level = MonoLogger::INFO )
{
$this->monoLogger = new MonoLogger( 'ActivityPub-PHP' );
$this->monoLogger->pushHandler( new ErrorLogHandler(ErrorLogHandler::SAPI, $level ) );
}
/**
* System is unusable.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function emergency( $message, array $context = array() )
{
$this->monoLogger->emergency( $message, $context );
}
/**
* 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() )
{
$this->monoLogger->alert( $message, $context );
}
/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function critical( $message, array $context = array() )
{
$this->monoLogger->critical( $message, $context );
}
/**
* 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() )
{
$this->monoLogger->error( $message, $context );
}
/**
* 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() )
{
$this->monoLogger->warning( $message, $context );
}
/**
* Normal but significant events.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function notice( $message, array $context = array() )
{
$this->monoLogger->notice( $message, $context );
}
/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function info( $message, array $context = array() )
{
$this->monoLogger->info( $message, $context );
}
/**
* Detailed debug information.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function debug( $message, array $context = array() )
{
$this->monoLogger->debug( $message, $context );
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log( $level, $message, array $context = array() )
{
$this->monoLogger->log( $level, $message, $context );
}
}