PHP code example of f-oris / easy-logger

1. Go to this page and download the library: Download f-oris/easy-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

f-oris / easy-logger example snippets




use Foris\Easy\Logger\Logger;

$config = [
    // ...
];
$logger = new Logger($config);

/**
 * 写入日志信息
 * 
 * 可以使用不同的方法写入不同级别的日志信息
 * 下面两种写法等价
 */
$logger->debug('调试日志', ['context' => 'context']);
$logger->log('debug', '调试日志', ['context' => 'context']);

/**
 * 日志信息信息写入指定通道 
 */
$logger->channel('channel')->debug('日志信息写入channel通道', ['context' => 'context']);



// 扩展的driver需要实现Psr\Log\LoggerInterface接口规范

$callback = function ($channel) {
    $logger = new \Monolog\Logger($channel);
    return $logger->pushHandler(new \Monolog\Handler\TestHandler());
};

$factory = new \Foris\Easy\Logger\Driver\Factory();
$factory->extend($callback, 'test_driver');

$config = [
    // ...
    
    "channels" => [
        // ...
        
        "test" => [
            "driver" => "test_driver",    
        ]    
    ]
];

$logger = new \Foris\Easy\Logger\Logger($factory, $config);
$logger->channel('test')->debug('This is a debug message');