Laravel stores its log files within the storage/logs
directory of your application. By default, a single log file named laravel.log
is created within this directory.
Key aspects of Laravel’s logging:
- Monolog Integration:Laravel’s logging facilities are built on top of the powerful Monolog library, providing a flexible and robust logging solution.
- Log Channels:Laravel utilizes “channels” to define how log information is written. Examples include:
- Single: Writes all logs to a single file (
laravel.log
). - Daily: Creates a new log file for each day, preventing a single file from becoming excessively large.
- Stack: Aggregates multiple log channels into one, allowing logs to be sent to various destinations simultaneously (e.g., file and Slack).
- Single: Writes all logs to a single file (
- Log Levels:Monolog supports various log severity levels (debug, info, notice, warning, error, critical, alert), allowing you to categorize and filter log messages based on their importance.
- Configuration:The
config/logging.php
file controls all logging behavior, including channel definitions and their respective options. You can configure the default log channel, specify the minimum log level for different environments, and customize how log messages are handled. - Writing to Logs:You can write messages to the log using the
Log
facade, utilizing methods corresponding to the different log levels (e.g.,Log::info()
,Log::error()
). Contextual data can also be passed as an array to these methods.
Example of writing to the log:
Code
<?php
use Illuminate\Support\Facades\Log;
Log::info('This is an informational message.');
Log::warning('Something might be going wrong here.');
Log::error('An error occurred during processing.', ['user_id' => 123, 'request_id'