Laravel – Sử dụng logging với channels

Đầu tiên check file config/logging.php

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => 'critical',
        ],

        'papertrail' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'handler' => SyslogUdpHandler::class,
            'handler_with' => [
                'host' => env('PAPERTRAIL_URL'),
                'port' => env('PAPERTRAIL_PORT'),
            ],
        ],

        'stderr' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'formatter' => env('LOG_STDERR_FORMATTER'),
            'with' => [
                'stream' => 'php://stderr',
            ],
        ],

        'syslog' => [
            'driver' => 'syslog',
            'level' => 'debug',
        ],

        'errorlog' => [
            'driver' => 'errorlog',
            'level' => 'debug',
        ],

        'null' => [
            'driver' => 'monolog',
            'handler' => NullHandler::class,
        ],

        'emergency' => [
            'path' => storage_path('logs/laravel.log'),
        ],

        'http' => [
            'driver' => 'daily',
            'path' => storage_path('logs/http.log'),
            'level' => 'debug',
        ],

        'crawl' => [
            'driver' => 'daily',
            'path' => storage_path('logs/crawl.log'),
            'level' => 'debug',
        ]
    ],

Đây là danh sách các channels ta có thể sử dụng. Ví dụ

  • http & crawl là 2 channels mình tạo riêng cho việc log http requests & crawl request

Riêng channel stack là một channel đặc biệt cho phép gửi log đến cùng lúc nhiều channel khác.

'channels' => ['single']

Ở ví dụ này thì chỉ gởi đến channel single

Bên cạnh đó drivers là … driver sẽ được sử dụng cho việc log của channel đó

  • single : Chỉ ghi log vào single file
  • daily : Tương tự trên nhưng theo chu kì mỗi ngày 1 file
  • errorlog
  • syslog

Chi tiết hơn

NameDescription
stackA wrapper to facilitate creating “multi-channel” channels
singleA single file or path based logger channel (StreamHandler)
dailyRotatingFileHandler based Monolog driver which rotates daily
slackSlackWebhookHandler based Monolog driver
papertrailSyslogUdpHandler based Monolog driver
syslogSyslogHandler based Monolog driver
errorlogErrorLogHandler based Monolog driver
monologA Monolog factory driver that may use any supported Monolog handler
customA driver that calls a specified factory to create a channel
Available Channel Drivers

Rồi . Vậy để sử dụng log channel thì sao

Log::stack(['http'])->info('Request URI: '.urldecode($uri).' with key: '.$key);

Ở đây mình dùng stack http và ghi 1 log info

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: