Bird
Raised Fist0
Laravelframework~10 mins

Logging configuration in Laravel - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Logging configuration
Start Laravel App
↓
Load logging config from config/logging.php
↓
Select log channel (e.g., stack, single, daily)
↓
Initialize log handler (file, syslog, errorlog, etc.)
↓
App runs and logs messages
↓
Messages written to log destination
↓
End
Laravel loads logging settings, chooses the channel, initializes handlers, then writes log messages during app execution.
Execution Sample
Laravel
<?php
Log::info('User logged in');
Log::error('Payment failed');
This code writes an info and an error message to the configured log channel.
Execution Table
StepActionLog LevelMessageLog DestinationResult
1Call Log::info()infoUser logged instorage/logs/laravel-YYYY-MM-DD.log (daily)Message appended to log file
2Call Log::error()errorPayment failedstorage/logs/laravel-YYYY-MM-DD.log (daily)Message appended to log file
3No more log calls---Logging complete for this request
💡 All log calls processed, messages saved to daily log file
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Log Channeldailydailydailydaily
Log Filestorage/logs/laravel-YYYY-MM-DD.logstorage/logs/laravel-YYYY-MM-DD.logstorage/logs/laravel-YYYY-MM-DD.logstorage/logs/laravel-YYYY-MM-DD.log
Log Messages[]["info: User logged in"]["info: User logged in", "error: Payment failed"]["info: User logged in", "error: Payment failed"]
Key Moments - 2 Insights
Why do both info and error messages go to the same log file?
Because the configured log channel is 'daily' which writes all levels to the same daily log file, as shown in execution_table steps 1 and 2.
What happens if the log channel is changed to 'single'?
The log messages would still be written to a single log file, but without daily rotation. The destination path changes, but the process is similar.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the log level of the message logged at step 2?
Ainfo
Berror
Cwarning
Ddebug
💡 Hint
Check the 'Log Level' column in row for step 2 in the execution_table.
At which step does the log message 'User logged in' get written?
AStep 1
BStep 2
CStep 3
DIt is never written
💡 Hint
Look at the 'Message' column in execution_table for step 1.
If the log channel was changed to 'errorlog', how would the log destination change?
AMessages would be discarded
BMessages would be saved in storage/logs/laravel.log
CMessages would be sent to system error log instead of a file
DMessages would be sent to a database
💡 Hint
Refer to Laravel logging channels and their destinations in the concept_flow.
Concept Snapshot
Laravel logging config is set in config/logging.php
Choose a log channel (daily, single, errorlog, etc.)
Log calls write messages to the channel's destination
Daily channel writes to storage/logs/laravel-YYYY-MM-DD.log with rotation
Log levels (info, error) are all recorded in the chosen channel
Full Transcript
Laravel starts by loading the logging configuration from config/logging.php. It selects the log channel, such as 'daily', which writes logs to a daily rotated file. When the app runs, calls like Log::info and Log::error write messages to the log file. Each message includes a level and text. The execution table shows step 1 logs an info message 'User logged in' to the daily log file. Step 2 logs an error message 'Payment failed' to the same file. After all log calls, the messages are saved and logging ends for the request. Variables like Log Channel and Log File remain constant, while Log Messages accumulate entries. Beginners often wonder why different levels go to the same file; this is because the channel handles all levels together. Changing the channel changes the destination but not the logging process. The visual quiz tests understanding of log levels, steps when messages are logged, and effects of changing channels.

Practice

(1/5)
1. What is the main purpose of Laravel's logging configuration?
easy
A. To configure user authentication methods
B. To control where logs are saved and what details are recorded
C. To manage database connections
D. To set up email notifications

Solution

  1. Step 1: Understand logging configuration role

    Logging configuration in Laravel defines how and where logs are stored and what level of detail is recorded.
  2. Step 2: Compare with other options

    Database connections, authentication, and email notifications are unrelated to logging configuration.
  3. Final Answer:

    To control where logs are saved and what details are recorded -> Option B
  4. Quick Check:

    Logging config = control log storage and detail [OK]
Hint: Logging config sets log storage and detail level [OK]
Common Mistakes:
  • Confusing logging with database setup
  • Mixing logging with authentication settings
  • Thinking logging controls email features
2. Which of the following is the correct way to define a single log channel in Laravel's config/logging.php?
easy
A. 'single' => ['driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug']
B. 'single' => ['driver' => 'file', 'path' => 'storage/logs/laravel.log', 'level' => 'info']
C. 'single' => ['driver' => 'stack', 'channels' => ['daily'], 'level' => 'error']
D. 'single' => ['driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'days' => 7]

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    'single' => ['driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug'] -> Option A
  4. Quick Check:

    Single channel = driver 'single' + path + level [OK]
Hint: Single channel uses driver 'single' with path and level keys [OK]
Common Mistakes:
  • Using 'file' instead of 'single' as driver
  • Confusing 'stack' driver with 'single'
  • Omitting required keys like 'level'
3. Given this logging configuration snippet in 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?
medium
A. The message is ignored because 'ignore_exceptions' is false
B. The message is sent to Slack but not saved to the log file
C. The message is saved to the single log file but not sent to Slack
D. The message is saved to the log file and sent to Slack

Solution

  1. 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'.
  2. 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.
  3. Final Answer:

    The message is saved to the single log file but not sent to Slack -> Option C
  4. Quick Check:

    Stack sends to channels; level filters Slack but not single [OK]
Hint: Stack sends to all channels; level filters each channel separately [OK]
Common Mistakes:
  • 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'
4. You have this incorrect logging channel configuration in 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?
medium
A. The 'days' value should be an integer, not a string; change '7' to 7
B. The 'path' should not use storage_path helper; use a string path instead
C. The 'level' key is invalid for daily driver and should be removed
D. The 'driver' should be 'single' instead of 'daily'

Solution

  1. 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'.
  2. Step 2: Correct the data type

    Change 'days' => '7' to 'days' => 7 to fix the type mismatch.
  3. Final Answer:

    The 'days' value should be an integer, not a string; change '7' to 7 -> Option A
  4. Quick Check:

    'days' must be integer, not string [OK]
Hint: Numeric config values must be integers, not quoted strings [OK]
Common Mistakes:
  • Leaving numeric values as strings causes config errors
  • Removing valid keys like 'level' mistakenly
  • Changing driver unnecessarily
5. You want to create a custom logging channel in Laravel that writes logs to a daily rotating file and also sends critical errors to Slack. Which configuration correctly combines these requirements?
hard
A. 'custom' => ['driver' => 'stack', 'channels' => ['single'], 'ignore_exceptions' => true], 'single' => ['driver' => 'single', 'path' => storage_path('logs/custom.log'), 'level' => 'debug']
B. 'custom' => ['driver' => 'daily', 'path' => storage_path('logs/custom.log'), 'level' => 'critical', 'days' => 14], 'slack' => ['driver' => 'stack', 'channels' => ['daily'], 'url' => env('LOG_SLACK_WEBHOOK_URL')],
C. 'custom' => ['driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'), 'level' => 'debug'], 'daily' => ['driver' => 'daily', 'path' => storage_path('logs/custom.log'), 'level' => 'critical', 'days' => 7],
D. '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']

Solution

  1. Step 1: Understand requirements for combined logging

    You need a stack channel combining 'daily' file logging and 'slack' for critical errors.
  2. 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.
  3. 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 D
  4. Quick Check:

    Stack channel combines daily + slack with correct levels [OK]
Hint: Use stack driver to combine daily file and Slack channels [OK]
Common Mistakes:
  • Using wrong driver for stack channel
  • Misplacing Slack inside daily channel
  • Setting ignore_exceptions true when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard