Custom logger for PHP exceptions/errors/warnings/notices.
- Using composer:
composer config --append repositories.xlog vcs https://github.com/Xeloses/xlog
composer require xeloses/xlog
or
composer config --append repositories.xlog vcs https://github.com/Xeloses/xlog
composer require xeloses/xlog --dev
Register errors/exception handler:
use Xeloses\XLog\XLog;
void XLog::register(?ILogOutputProvider $provider, int $log_level, bool $debug)$provider- log output provider:LogScreen- output log to browser;LogFile- output log to file;- any custom implementation of
Xeloses\XLog\Interfaces\ILogOutputProvider.
$log_level- combination of PHP LOG_LEVEL constants or:XLog::DEFAULT- equals toE_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;XLog::ALL- all errors/warnings/notices (include released in future);XLog::NONE- none;XLog::DEBUG- equals toXLog::ALL;XLog::ERRORS- equals toE_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_USER_ERROR;XLog::WARNINGS- equals toE_WARNING | E_CORE_WARNING | E_COMPILE_WARNING | E_USER_WARNING;XLog::NOTICES- equals toE_NOTICE | E_STRICT | E_DEPRECATED | E_USER_NOTICE | E_USER_DEPRECATED.
Default value is
XLog::DEFAULT.$debug- debug mode, it automatically set$log_leveltoXLog::DEBUGand allow to useXLog::dumpmethod.- Another way to go debug mode is:
When
define('DEBUG',true); XLog::register();
DEBUGis defined and set totruemethodXLog::register()can be called without arguments. It will automatically useLogScreenas output provider (but you still can pass your own provider) and set$debugattribute totrue.
Default value is
false.- Another way to go debug mode is:
This method calls
ini_set('display_errors',0);
error_reporting(0);itself. All notices, warnings, errors (except fatal) and exceptions will be processed inside this class and passed to output provider.
XLog::register(new LogScreen(['timestamp_format' => 'c']), XLog::ALL);or
define('DEBUG',true);
XLog::register();Options:
timestamp_format- format of timestamps in log output (see format of PHP date() function).Default value is
'c'- ISO 8601 datetime.
XLog::register(
new LogFile([
'timestamp_format' => 'c',
'filename' => '/logs/{yyyy}-{mm}-{dd}.{n}.log',
'filesize' => 5*1024*1024,
'overwrite' => false,
]),
XLog::ERRORS
);Options:
timestamp_format- format of timestamps in log output (see format of PHP date() function).Default value is
'c'- ISO 8601 datetime.filename- log file name (with path). Available placeholders:{date}- current date, equals to PHPdate('Y-m-d');{year}or{Y}or{yyyy}- year, 4 digits, equals to PHPdate('Y');{yy}- year, 2 digits, equals to PHPdate('y');{Month}- month name, full (January, Febrary, ...), equals to PHPdate('F');{month}or{M}- month name, short (Jan, Feb, ...), equals to PHPdate('M');{mm}- month, 2 digits, equals to PHPdate('m');{m}- month, 1 or 2 digits, equals to PHPdate('n');{Day}- day name, full (Sunday, Monday, ...), equals to PHPdate('l');{day}or{D}- day name, short (Sun, Mon, ...), equals to PHPdate('D');{dd}- day, 2 digits, equals to PHPdate('d');{d}- day, 1 or 2 digits, equals to PHPdate('j');{n}- counter (usable only whenoverwriteoption isTRUE).
Default value is
./logs/{date}.log.filesize- maximum size of log file in bytes, when lof file reach this size it will be overwritten (ifoverwriteoption istrue) or new log file will be created (ifoverwriteoption isfalse).Default value is
1048576= 1 Mb.overwrite- overwrite log file when it reach max size or not (see description offilesizeoption above)Default value is
true.
WARNING: Directories for log created by class will have
0777access mode. Log files created by class will have0775access mode. Path and/or filename should be added to.htaccessmanually to close it from public access!
To write some custom message to log use:
void XLog::log(string $message)Also you can output to log with PHP function trigger_error().
Note:
$error_typepassed totrigger_error()should match$error_levelpassed toXLog::register().
If debug mode enabled you can dump variables to log:
void XLog::dump(mixed $var, ?string $comment = '')<?php
define('DEBUG',true);
use Xeloses\XLog\XLog;
XLog::register();
XLog::log('A custom message');
$array_example = [
'first' => 1,
'second' => '2nd'
];
XLog::dump($array_example, 'Comment for dump of array');
trigger_error('User notice generated with trigger_error()',E_USER_NOTICE);
trigger_error('User warning generated with trigger_error()',E_USER_WARNING);
throw new Exception('Exception example.');
?>Output:
