Logging helps you keep track of what your Laravel app is doing. It records errors and important events so you can fix problems and understand app behavior.
Logging configuration in Laravel
config/logging.php
This file controls how Laravel logs messages.
You can set log channels, formats, and storage locations here.
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [ 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', ], ],
'channels' => [ 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'info', 'days' => 14, ], ],
This config sets the default log to 'stack' which sends logs to both a single file and Slack for critical errors.
<?php return [ 'default' => env('LOG_CHANNEL', 'stack'), 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single', 'slack'], 'ignore_exceptions' => false, ], 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', ], 'slack' => [ 'driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'), 'username' => 'Laravel Log', 'emoji' => ':boom:', 'level' => 'critical', ], ], ];
Use the .env file to set LOG_CHANNEL and LOG_SLACK_WEBHOOK_URL for easy changes without editing code.
Log levels include debug, info, notice, warning, error, critical, alert, emergency.
Stack channels let you send logs to multiple places at once.
Logging configuration controls where and how Laravel saves logs.
You can log to files, Slack, databases, and more using channels.
Use the default channel to set the main logging method for your app.
Practice
Solution
Step 1: Understand logging configuration role
Logging configuration in Laravel defines how and where logs are stored and what level of detail is recorded.Step 2: Compare with other options
Database connections, authentication, and email notifications are unrelated to logging configuration.Final Answer:
To control where logs are saved and what details are recorded -> Option BQuick Check:
Logging config = control log storage and detail [OK]
- Confusing logging with database setup
- Mixing logging with authentication settings
- Thinking logging controls email features
config/logging.php?Solution
Step 1: Identify correct driver and keys for single channel
The 'single' channel uses 'driver' => 'single', a 'path' with storage_path helper, and a 'level' key.Step 2: Check options for syntax and correctness
'single' => ['driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug'] matches correct syntax. 'single' => ['driver' => 'file', 'path' => 'storage/logs/laravel.log', 'level' => 'info'] uses wrong driver 'file'. 'single' => ['driver' => 'stack', 'channels' => ['daily'], 'level' => 'error'] uses 'stack' driver incorrectly. 'single' => ['driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'days' => 7] misses 'level' key and has wrong driver.Final Answer:
'single' => ['driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug'] -> Option AQuick Check:
Single channel = driver 'single' + path + level [OK]
- Using 'file' instead of 'single' as driver
- Confusing 'stack' driver with 'single'
- Omitting required keys like 'level'
config/logging.php:
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'slack'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'level' => 'critical',
'username' => 'Laravel Log',
],
What happens when you log an error with level 'error' using the 'stack' channel?Solution
Step 1: Understand channel levels and stack behavior
The 'stack' channel sends logs to both 'single' and 'slack' channels. 'single' logs at 'debug' level, 'slack' at 'critical'.Step 2: Compare log level 'error' with channel levels
'error' is lower than 'critical', so Slack ignores it. 'single' accepts 'error' since 'debug' is lower level threshold.Final Answer:
The message is saved to the single log file but not sent to Slack -> Option CQuick Check:
Stack sends to channels; level filters Slack but not single [OK]
- Assuming all channels receive every log regardless of level
- Confusing 'ignore_exceptions' with log filtering
- Thinking Slack receives 'error' level logs when set to 'critical'
config/logging.php:
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'days' => '7',
'level' => 'info',
],
What is the error and how do you fix it?Solution
Step 1: Identify type error in 'days' key
The 'days' key expects an integer number of days to keep logs, but it is given as a string '7'.Step 2: Correct the data type
Change 'days' => '7' to 'days' => 7 to fix the type mismatch.Final Answer:
The 'days' value should be an integer, not a string; change '7' to 7 -> Option AQuick Check:
'days' must be integer, not string [OK]
- Leaving numeric values as strings causes config errors
- Removing valid keys like 'level' mistakenly
- Changing driver unnecessarily
Solution
Step 1: Understand requirements for combined logging
You need a stack channel combining 'daily' file logging and 'slack' for critical errors.Step 2: Check each option for correct driver and channel setup
'custom' => ['driver' => 'stack', 'channels' => ['daily', 'slack'], 'ignore_exceptions' => false], 'daily' => ['driver' => 'daily', 'path' => storage_path('logs/custom.log'), 'level' => 'debug', 'days' => 14], 'slack' => ['driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'), 'level' => 'critical', 'username' => 'Custom Logger'] correctly defines 'custom' as a stack channel with 'daily' and 'slack' channels. 'daily' logs debug and above, 'slack' logs critical only. Other options misuse drivers or channels.Final Answer:
'custom' => ['driver' => 'stack', 'channels' => ['daily', 'slack'], 'ignore_exceptions' => false], 'daily' => ['driver' => 'daily', 'path' => storage_path('logs/custom.log'), 'level' => 'debug', 'days' => 14], 'slack' => ['driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'), 'level' => 'critical', 'username' => 'Custom Logger'] -> Option DQuick Check:
Stack channel combines daily + slack with correct levels [OK]
- Using wrong driver for stack channel
- Misplacing Slack inside daily channel
- Setting ignore_exceptions true when not needed
Ready to prove it?
Pass the test and this pattern moves unknown
