Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and use a custom daily log file in Laravel?

In Laravel I'm defining a custom log file in /config/logger.php:

'mycustomlog' => [
  'driver' => 'stack',
  'path' => storage_path('logs/mycustomlog.log'),
  'level' => 'info',
],

Here's my stack driver for context:

'stack' => [
  'driver' => 'stack',
  'channels' => ['daily', 'syslog'],
  'ignore_exceptions' => false,
],

and I'm calling it as follows:

Log::channel('mycustomlog')->info($e);


What I expect to happen:

A (daily) log file is created and the exception is logged. I.e mycustomlog-2019-11-07.log

What actually happens:

No log file is created, but the following error is logged to laravel.log:

[2019-11-07 10:25:31] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (ErrorException(code: 0): Undefined index: channels at /var/www/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:232)

SOLUTION:

'mycustomlog' => [
   'driver' => 'daily',
   'channels' => ['syslog'],
   'path' => storage_path('logs/mycustomlog.log'),
   'level' => 'info',
],
like image 840
f7n Avatar asked Nov 07 '19 10:11

f7n


People also ask

Where is the log file in Laravel?

By default, Laravel is configured to create a single log file for your application, and this file is stored in app/storage/logs/laravel. log .

What is log in Laravel?

Laravel logging is based on "channels". Each channel represents a specific way of writing log information. For example, the single channel writes log files to a single log file, while the slack channel sends log messages to Slack. Log messages may be written to multiple channels based on their severity.


2 Answers

You will need to have channels in the config logger.php see here. The point of the stack driver is to report to multiple channels.

'mycustomlog' => [
    'driver' => 'stack',
    'channels' => ['daily'],
    'path' => storage_path('logs/mycustomlog.log'),
    'level' => 'info',
],
like image 108
mrhn Avatar answered Oct 27 '22 00:10

mrhn


If you don't want it to be in the stack you can have your config to point to the single driver like this

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

Then call it the same way you tried on your own.

Log::channel('mycustomlog')->info($e);
like image 21
iosifv Avatar answered Oct 27 '22 00:10

iosifv