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.
0
0
Logging configuration in Laravel
Introduction
When you want to record errors to find and fix bugs.
When you need to track user actions for security or auditing.
When you want to monitor app performance or unusual activity.
When debugging issues in development or production environments.
When you want to save logs to different places like files, databases, or external services.
Syntax
Laravel
config/logging.php
This file controls how Laravel logs messages.
You can set log channels, formats, and storage locations here.
Examples
Sets the default log channel. 'stack' means it uses multiple channels together.
Laravel
'default' => env('LOG_CHANNEL', 'stack'),
Defines a single file log channel that writes all logs to one file.
Laravel
'channels' => [ 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', ], ],
Defines a daily log channel that creates a new log file each day and keeps logs for 14 days.
Laravel
'channels' => [ 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'info', 'days' => 14, ], ],
Sample Program
This config sets the default log to 'stack' which sends logs to both a single file and Slack for critical errors.
Laravel
<?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', ], ], ];
OutputSuccess
Important Notes
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.
Summary
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.