Monolog是PHP记录日志的神器,它真的很强大。在我们的日常工作中,掌握其中的几项,就大体够用了…
1. 输出到文件中,并自定义格式
require_once('vendor/autoload.php');
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
$log = new Logger('debug');
$dateFormat = "Y m jS, g:i a";
$output = "%datetime% > %level_name% > %message% %context% %extra%\n";
$formatter = new LineFormatter($output, $dateFormat);
$stream = new StreamHandler('1.log', Logger::CRITICAL);
$stream->setFormatter($formatter);
$log->critical("abc");
结果:
2. 输出到控制台
require_once('vendor/autoload.php');
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
$log = new Logger('debug');
$stream = new StreamHandler('php://stdout', Logger::CRITICAL);
$stream->setFormatter($formatter);
$log->critical("abc");
3. 输出到浏览器控制台
require_once('vendor/autoload.php');
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$log = new Logger('debug');
$handler = new \Monolog\Handler\BrowserConsoleHandler(Logger::INFO);
$log->pushHandler($handler);
$log->pushProcessor(new \Monolog\Processor\WebProcessor());
$log->addRecord(Logger::DEBUG,'My logger is now ready', ['author'=>'jim','date'=>date("Y-m-d")]);
$log->addRecord(Logger::CRITICAL,'My logger is now ready', ['author'=>'jim','date'=>date("Y-m-d")]);
$log->emergency("abc");
4. 输出到FirePHP 【浏览器需要添加firePHP插件】
require_once('vendor/autoload.php');
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$log = new Logger('debug');
$firephp = new FirePHPHandler();
$log->pushHandler($firephp);
$log->pushProcessor(new \Monolog\Processor\WebProcessor());
$log->addRecord(Logger::DEBUG,'My logger is now ready', ['author'=>'liujx','date'=>date("Y-m-d")]);
$log->addRecord(Logger::CRITICAL,'My logger is now ready', ['author'=>'liujx','date'=>date("Y-m-d")]);
$log->emergency("abc");
|