Search code examples
laravelloggingdevelopment-environmentproduction-environment

How do I disable a log channel on local and testing environment


In my application I have these log channels:

'general'       => [
 'driver' => 'single',
 'path'   => storage_path('logs/general.log'),
 'level'  => 'debug',
],
'jobs'    => [
  'driver'   => 'stack',
  'channels' => [
    'general',
    'slack'
 ],
],
'slack'         => [
    'driver'   => 'slack',
    'url'      => /*Censored Hook URL*/,
    'username' => 'MyApp',
    'emoji'    => ':gear:',
    'level'    => 'debug',
],

When I log in job channel I want not to log into slack log when on local or testing environment in order to avoid having duplicate and extremely unwanted logs on the shared log channel on the slack.

So how I can specify the environments that the slack channel will be able to write logs. A dirty approach is to specify manually which logs will write in to via this snippet of code:

if(!App::environment(['local','testing')){
   Log::channel('jobs')->info('Blah blah blah');
} else {
   Log::channel('general')->info('Blah blah blah');
}

But using the code above, I am afraid that will turn my codebase into a diffucult-to-read-one. So do you know an elegant solution in order to process my logs?


Solution

  • You could simply split jobs into 2 entrys, example jobs and jobs_without_slack, let jobs be as is and jobs_without_slack will be configured without slack in it's channels. Then use different .env.xy files for testing (.env.testing), local, develop (.env.develop) and so on and set the LOG_CHANNEL to the correct logging, so you do not need to change a line of code in your logic ;)