silverstripe-framework/src/Logging/MonologErrorHandler.php

94 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Logging;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Monolog\ErrorHandler as MonologHandler;
class MonologErrorHandler implements ErrorHandler
{
/**
* @var LoggerInterface[]
*/
private $loggers = [];
2016-11-29 00:31:16 +01:00
/**
* Set the PSR-3 logger to send errors & exceptions to. Will overwrite any previously configured
* loggers
*
* @deprecated 4.4.0:5.0.0 Use pushHandler() instead
* @param LoggerInterface $logger
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function setLogger(LoggerInterface $logger)
2016-11-29 00:31:16 +01:00
{
$this->logger = $logger;
return $this;
}
/**
* Get the first registered PSR-3 logger to send errors & exceptions to
*
* @deprecated 4.4.0:5.0.0 Use getHandlers() instead
* @return LoggerInterface
*/
public function getLogger()
{
return reset($this->loggers);
2016-11-29 00:31:16 +01:00
}
/**
* Adds a PSR-3 logger to send messages to, to the end of the stack
*
* @param LoggerInterface $logger
* @return $this
*/
public function pushLogger(LoggerInterface $logger)
{
$this->loggers[] = $logger;
return $this;
}
/**
* Returns the stack of PSR-3 loggers
*
* @return LoggerInterface[]
*/
public function getLoggers()
{
return $this->loggers;
}
/**
* Set the PSR-3 loggers (overwrites any previously configured values)
*
* @param LoggerInterface[] $loggers
* @return $this
*/
public function setLoggers(array $loggers)
{
$this->loggers = $loggers;
return $this;
}
/**
* {@inheritDoc}
*
* @throws InvalidArgumentException
*/
public function start()
2016-11-29 00:31:16 +01:00
{
$loggers = $this->getLoggers();
if (empty($loggers)) {
throw new InvalidArgumentException(
"No Logger properties passed to MonologErrorHandler. Is your Injector config correct?"
);
2016-11-29 00:31:16 +01:00
}
foreach ($loggers as $logger) {
MonologHandler::register($logger);
}
2016-11-29 00:31:16 +01:00
}
}