Search code examples
laravellaravel-5monolog

Laravel 5 log data to another file


Is there any way to log data into another file in laravel 5? Not to standard one? For examle i'd like to use something like this:

Log::info("Some log data", '/path/to/custom/file.log');

Or at least is there a possibility to divide log files basing on the log type.

Log::info("Some log data");
Log::error("Another log data");

So that info and error logs will go to different log files.

Thanks for the help.


Solution

  • Here is example:

    Log::useFiles(base_path() . '/path/to/custom/file.log', 'info');
    Log::info('Do log this another PATH');
    

    Another way

    date_default_timezone_set('Asia/Dhaka');
    $data = date('Y-m-d');
    $time = date('h-A');
    Log::useFiles(base_path() . '/log/'.$data.'/'.$time.'-'info.log', 'info');
    Log::info('Do log this another PATH');
    

    on this example every date create a folder with saperate log with hourly.

    Regarding Laravel 5:

    You can also change single log path & name.

    Add below line of code to : bootstrap>>app.php very bottom above of return $app;

    
        # SINGLE LOG
        $app->configureMonologUsing(function($monolog) use ($app) {
            $handler = new Monolog\Handler\StreamHandler($app->storagePath().'/logs/YOUR_SINGLE_LOG_NAME.log');
            $handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true));
            $monolog->pushHandler($handler);
        });