📖 Scenario: You are building a Laravel web application that needs to keep track of important events and errors. Proper logging helps developers find and fix problems quickly.
🎯 Goal: Configure Laravel logging to write logs to a single file and then customize the log level to only record warnings and errors.
📋 What You'll Learn
Create a logging configuration array in config/logging.php with a single channel named single that writes to storage/logs/laravel.log
Add a variable logLevel set to warning to control the minimum log level
Modify the single channel to use the logLevel variable for its level setting
Print the current log channel and log level to verify the configuration
💡 Why This Matters
🌍 Real World
Logging is essential in real applications to track errors and important events. Laravel uses configuration files to manage logging behavior.
💼 Career
Understanding logging configuration helps developers and DevOps engineers monitor applications and troubleshoot issues efficiently.
Progress0 / 4 steps
1
Create basic logging configuration
Create a PHP array called loggingConfig with a key channels that contains a single channel. The single channel should have driver set to single and path set to storage/logs/laravel.log.
Laravel
Hint
Use a PHP array with nested arrays for channels and the single channel configuration.
2
Add log level configuration variable
Add a variable called logLevel and set it to the string 'warning'.
Laravel
Hint
Declare a simple string variable named logLevel with value 'warning'.
3
Set log level in the single channel
Add a level key to the single channel inside $loggingConfig and set its value to the variable $logLevel.
Laravel
Hint
Inside the single array, add 'level' => $logLevel.
4
Print the current log channel and level
Write two print statements to display the current log channel name 'single' and the log level stored in $logLevel.
Laravel
Hint
Use print("Current log channel: single\n") and print("Log level: $logLevel\n").
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
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 B
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?
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
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 A
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?