目次

09 Monolog

普通にログ出力する

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$monolog = new Logger('MONOLOG_TEST');
$handler = new StreamHandler('./app.log',Logger::DEBUG);
$monolog->pushHandler($handler);
  
$monolog->addDebug('debug log');
$monolog->addInfo('info log');
$monolog->addWarning('warning log');
$monolog->addError('error log');

ログの出力

[2019-02-07 16:41:41] MONOLOG_TEST.DEBUG: debug log [] []
[2019-02-07 16:41:41] MONOLOG_TEST.INFO: info log [] []
[2019-02-07 16:41:41] MONOLOG_TEST.WARNING: warning log [] []
[2019-02-07 16:41:41] MONOLOG_TEST.ERROR: error log [] []

ローテートする場合

use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;

$monolog = new Logger('MONOLOG_TEST');
$handler = new RotatingFileHandler('./app.log', 10, Logger::DEBUG);
$monolog->pushHandler($handler);

$monolog->addDebug('debug log');
$monolog->addInfo('info log');
$monolog->addWarning('warning log');
$monolog->addError('error log');

下記のような感じでログが出力される

$ ls -la
-rw-r--r-- 1 apache apache 476 Feb  7 16:38 app-2019-02-07.log