Laravel : How to Log INFO to separate file
Do you want to specifically log info
to one log file and another log type to another location? My solution might not help in that case, but could still be useful.
To write a log file to another location, use the method useDailyFiles
or useFiles
, and then info to log to the log file at the path you just specified. Like so:
Log::useDailyFiles(storage_path().'/logs/name-of-log.log');
Log::info([info to log]);
The first parameter for both methods is the path of the log file (which is created if it doesn't already exist) and for useDailyFiles
the second argument is the number of days Laravel will log for before erasing old logs. The default value is unlimited, so in my example I haven't entered a value.
In Laravel 5.6 you can create your own channel in config\logging.php
. If you have upgraded from an older Laravel version you need to create this file (https://laravel.com/docs/5.6/upgrade).
Add this to you channel array in config\logging.php
'your_channel_name' => [
'driver' => 'single',
'path' => storage_path('logs/your_file_name.log'),
],
You can then call any of the 8 logging levels like that:
Illuminate\Support\Facades\Log::channel('your_channel_name')->info('your_message');
The logs will be stored in logs/your_file_name.log
Since Laravel >= 5.6 we can use Log Channels to make it work in a easy way. This allows you to create log channels which can be handled as own log files with own drivers, paths or levels. You just need this few lines to make it work.
Simple add a new channel (choose your channel name, e.g. "command")
config/logging.php:
return [
'channels' => [
'command' => [
'driver' => 'single',
'path' => storage_path('logs/command.log'),
'level' => 'debug',
],
],
];
Log where ever you want by parse the channel name:
Log::channel('command')->info('Something happened!');