monolog使用案例

1.首先要执行
composer require monolog/monolog
2.案例
<?php
require_once './vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\RedisHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\BrowserConsoleHandler;
use Monolog\Processor\WebProcessor;
use Monolog\Processor\IntrospectionProcessor;

use Monolog\Formatter\JsonFormatter;

$logfilename = "/data/logs/default/" .date('YmdH').".log";
$redis = new Redis();
$redis->connect('192.168.56.101',6379);
$key  = 'monolog';

$stream = new StreamHandler($logfilename,Logger::ERROR);
$stream->setFormatter(new JsonFormatter());

$redishandle = new RedisHandler($redis,$key,Logger::INFO);

$browserhandle = new BrowserConsoleHandler(Logger::INFO);

$channel1 = 'test';

$log = new Logger($channel1);
$log->pushHandler($stream);
$log->pushHandler($redishandle);
$log->pushHandler($browserhandle);

$log->pushProcessor(new WebProcessor());
$log->pushProcessor(new IntrospectionProcessor());


$log->info('info', ['a' => 'test']);
$log->error('error');


Tagged . Bookmark the permalink.

Comments are closed.