php-censor/src/Logging/Handler.php

146 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Logging;
use Psr\Log\LoggerInterface;
/**
* Base Log Handler
*/
class Handler
{
/**
* @var array
*/
2016-04-20 17:39:48 +02:00
protected $levels = [
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice',
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
2016-04-20 17:39:48 +02:00
];
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}
/**
* Register a new log handler.
* @param LoggerInterface $logger
*/
public static function register(LoggerInterface $logger = null)
{
$handler = new static($logger);
2016-04-20 17:39:48 +02:00
set_error_handler([$handler, 'handleError']);
register_shutdown_function([$handler, 'handleFatalError']);
2016-04-20 17:39:48 +02:00
set_exception_handler([$handler, 'handleException']);
}
/**
* @param integer $level
* @param string $message
* @param string $file
* @param integer $line
*
* @throws \ErrorException
*/
public function handleError($level, $message, $file, $line)
{
if (error_reporting() & $level) {
2018-03-05 13:32:49 +01:00
$exceptionLevel = isset($this->levels[$level]) ? $this->levels[$level] : $level;
throw new \ErrorException(
2018-03-05 13:32:49 +01:00
sprintf('%s: %s in %s line %d', $exceptionLevel, $message, $file, $line),
0,
$level,
$file,
$line
);
}
}
/**
* @throws \ErrorException
*/
public function handleFatalError()
{
2018-03-05 13:32:49 +01:00
$fatalError = error_get_last();
try {
2014-05-02 15:48:40 +02:00
if (($error = error_get_last()) !== null) {
$error = new \ErrorException(
sprintf(
'%s: %s in %s line %d',
2018-03-05 13:32:49 +01:00
$fatalError['type'],
$fatalError['message'],
$fatalError['file'],
$fatalError['line']
),
0,
2018-03-05 13:32:49 +01:00
$fatalError['type'],
$fatalError['file'],
$fatalError['line']
);
2014-05-02 15:48:40 +02:00
$this->log($error);
}
2014-05-01 17:55:44 +02:00
} catch (\Exception $e) {
2014-05-02 15:48:40 +02:00
$error = new \ErrorException(
sprintf(
'%s: %s in %s line %d',
2018-03-05 13:32:49 +01:00
$fatalError['type'],
$fatalError['message'],
$fatalError['file'],
$fatalError['line']
),
0,
2018-03-05 13:32:49 +01:00
$fatalError['type'],
$fatalError['file'],
$fatalError['line']
);
2014-05-02 15:48:40 +02:00
$this->log($error);
}
}
/**
2018-01-28 11:18:25 +01:00
* @param $exception
*/
2018-01-28 11:18:25 +01:00
public function handleException($exception)
{
$this->log($exception);
}
/**
* Write to the build log.
2018-01-28 11:18:25 +01:00
* @param $exception
*/
2018-01-28 11:18:25 +01:00
protected function log($exception)
{
if (null !== $this->logger) {
$message = sprintf(
'%s: %s (uncaught exception) at %s line %s',
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
);
2016-04-20 17:39:48 +02:00
$this->logger->error($message, ['exception' => $exception]);
}
}
}