laravel 5.2 custom log file for different tasks

后端 未结 10 1035
再見小時候
再見小時候 2021-01-30 00:36

Can we create a custom log file for different purposes in laravel 5.2 like for order related log entries that should be in order.log and for payment related stuff the entry shou

10条回答
  •  爱一瞬间的悲伤
    2021-01-30 01:00

    You can try repurposing the log functions to write different types of logs to different files. This can be done by editing the bootstrap/app.php file:

    $app->configureMonologUsing(function($monolog) {
        $bubble = false;
        $infoStreamHandler = new Monolog\Handler\StreamHandler( storage_path("/logs/orders.log"), Monolog\Logger::INFO, $bubble);
        $monolog->pushHandler($infoStreamHandler);
    
        $warningStreamHandler = new Monolog\Handler\StreamHandler( storage_path("/logs/logins.log"), Monolog\Logger::WARNING, $bubble);
        $monolog->pushHandler($warningStreamHandler);
    });
    

    Then in your code, you can do:

    Log::info('Order was created', ['ORDER-123']);
    
    Log::warning('User login', ['USER-1']);
    

    You can use this method to edit all the available log functions:

    • DEBUG
    • INFO
    • NOTICE
    • WARNING
    • ERROR
    • CRITICAL
    • ALERT
    • EMERGENCY

提交回复
热议问题