nickfan/channel-log

此包的最新版本(dev-develop)没有可用的许可信息。

支持Laravel日志数据,通过配置分离通道日志文件

dev-develop 2018-09-05 09:05 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:02:49 UTC


README

README(中文)| 中文说明

简介

支持Laravel日志数据,通过配置分离通道日志文件。

该项目基于ShQ针对Laravel 5.2不同任务的自定义日志文件答案

安装

  • 安装composer包
composer require "nickfan/channel-log:dev-master"
  • 编辑config/app.php文件

在提供者分组中添加

Nickfan\ChannelLog\ChannelLogServiceProvider::class,

在别名分组中添加

'ChannelLog' => Nickfan\ChannelLog\Facades\ChannelLog::class,
  • 发布配置文件
php artisan vendor:publish --provider="Nickfan\ChannelLog\ChannelLogServiceProvider"

设置

return [
    'default' => [
        'path' => 'logs/default.log',
        'level' => \Monolog\Logger::DEBUG
    ],
//    'event' => [
//        'path' => 'logs/event.log',
//        'level' => \Monolog\Logger::INFO
//    ],
    'custom'=>[  // Channel Label Name
        'name'=>'custom',           // (optional) Channel Label Name
        'log'=>'daily',             // log mode support: console (only in stdout); single (single file); daily (daily files); syslog (syslog); errorlog (php errorlog);
        'console'=>true,            // output to stdout(console) at the sametime.
        'path'=>'logs/custom.log', // Log file path,relative path will convert to relative with storage_path(yourpath)
        'level' => \Monolog\Logger::DEBUG, // Log Level
        'log_syslog_name'=>'channel_log',   // syslog mode log entry name
        'log_max_files'=>5,                 // max files keep in daily log mode
        'formatter'=>\Nickfan\ChannelLog\ChannelLogJsonFormatter::class,           // (optional) Record Formatter Class Name
    ],
];


自定义配置器

在通道设置中,你可以通过设置键名:configurator来自定义配置器

配置器类必须实现Nickfan\ChannelLog\ChannelLogConfigurator 接口

你可以根据默认配置器类Nickfan\ChannelLog\ChannelLogDefaultConfigurator编写自己的配置器

配置示例

return [
    'myconsole' => [  // Channel Label Name
        'path' => 'logs/default.log', // Log file path,relative path will convert to relative with storage_path(yourpath)
        'level' => \Monolog\Logger::DEBUG // Log Level
        // set the configurator class name
        'configurator' => \App\Support\ChannelLogMyConsoleConfigurator::class
    ],

];

Laravel使用方法


// Log to 'default' channel
ChannelLog::channel('default')->info('my test message {mykey1}',['mykey1'=>'myval1','aaa'=>'abc']);

// Log to 'event' channel
ChannelLog::channel('event')->error('my event message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);

// Log to 'mycustom' channel in daily log mode ,with filepath '/tmp/mycustom-2017-10-24.log'
ChannelLog::daily('/tmp/mycustom.log')->debug('my custom message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);

// Log to 'newdirect' channel in single log mode ,with filepath '/tmp/newdirect.log' , also log to console(stdout)
ChannelLog::direct([
            'name'=>'newdirect',// Channel Label Name
            'console'=>true,    // output to console(stdout)
            'path'=>'/tmp/newdirect.log', // log filepath
            ])->debug('new direct message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);


独立使用

use Nickfan\ChannelLog\ChannelLogWriterStandAlone;

$projectRoot = dirname(__DIR__);
$channelLogWriter = new ChannelLogWriterStandAlone(
  [
      'default'=>[
          'log' => 'single',
          'console'=> false,
          'path'=>$projectRoot.'/logs/default.log',
          'level'=>\Monolog\Logger::INFO,
      ],
      'event' => [
          'log' => 'daily',
          'console'=> true,
          'path' => $projectRoot.'/logs/event.log',
          'level' => \Monolog\Logger::DEBUG,
      ],
  ]
);

$channelLogWriter->channel('default')->info('my test message {mykey1}',['mykey1'=>'myval1','aaa'=>'abc']);


$result2 = $channelLogWriter->channel('event')->error('my event message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);


$result3 = $channelLogWriter->daily($projectRoot.'/logs/mycustom.log')->debug('my custom message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);


$result4 = $channelLogWriter->direct($projectRoot.'/logs/mydirect.log')->debug('my direct message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);


$result5 = $channelLogWriter->direct([
            'name'=>'newdirect',
            'console'=>true,
            'path'=>$projectRoot.'/logs/newdirect.log',
            ])->debug('new direct message {mykey2}',['mykey2'=>'myval2','qqq'=>'qwe']);

更多详细的使用方法请参考monolog官方站点 https://github.com/Seldaek/monolog

支持Beerpay

嘿,兄弟!帮我点杯🍻吧!

Beerpay Beerpay