In this article i will show you to use and create custom logs file in laravel. Logging is an important aspect when you want to debug your application or you want to monitor the user activities on the application. Laravel itself provides a robust library to create logging on daily basis, stack, slack, single file etc. Sometimes in our application we want to create a separate log file for specific kind of request or log. For example creating a separate log file for payment transactions.
Laravel have different logs level to emergency, alert, critical, error, warning, notice, info, and debug.
Laravel uses channels to log the logs for example single channel store logs in single file in storage logs folders and slacks send logs information to slack provider.
Configuration file for custom laravel logs
You can configure the laravel logs configuration in config/logging.php
file. here is the look of logging.php
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/
'default' => env('LOG_CHANNEL', 'stack'),
/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog",
| "custom", "stack"
|
*/
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => env('LOG_LEVEL', 'critical'),
],
'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => SyslogUdpHandler::class,
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
],
],
'stderr' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
'syslog' => [
'driver' => 'syslog',
'level' => env('LOG_LEVEL', 'debug'),
],
'errorlog' => [
'driver' => 'errorlog',
'level' => env('LOG_LEVEL', 'debug'),
],
'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],
];
If you look above file default log channel is 'default' => env('LOG_CHANNEL', 'stack')
stack and add a new channel for example named as readerstacks
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'readerstacks' => [
'driver' => 'single',
'path' => storage_path('logs/readerstack.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
Logging the logs
To log the information we use use Illuminate\Support\Facades\Log
class as below and channel method to define our channel readerstacks
use Illuminate\Support\Facades\Log;
Log::channel("readerstacks")->emergency($message);
Log::channel("readerstacks")->alert($message);
Log::channel("readerstacks")->critical($message);
Log::channel("readerstacks")->error($message);
Log::channel("readerstacks")->warning($message);
Log::channel("readerstacks")->notice($message);
Log::channel("readerstacks")->info($message);
Log::channel("readerstacks")->debug($message);
Example to use:
use Illuminate\Support\Facades\Log;
Log::channel("readerstacks")->info("Simple Log",["info"=>"This is simple log by readerstacks"]);
So, each method accepts two parameters first as string and another one as array as above. Make sure second parameter is array otherwise it will throw exception.
Let’s understand create logs file in laravel 8 / 9 with example
Step 1: Create a laravel project
First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations
composer create-project laravel/laravel example-app
Step 4 : Create a controller
Create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.
php artisan make:controller ArticleController
Now, add the controller logic for pagination and Ajax
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Article;
use Illuminate\Support\Facades\Log;
class ArticleController extends Controller
{
public function index(Request $request)
{
$articles = Article::paginate(5);
Log::info("Articles data" , ["data"=>$articles]);
$type="ALERT";
Log::channel("readerstacks")->alert("Articles alert ".$type." Error") ;
return $articles;
}
}
Step 3 : Create Route
Now, create a route and log the information
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;
Route::get('/log-info',[ArticleController::class, 'index']);
Location of custom logs file in laravel
Above configuration will create the log at storage/logs/readerstack.log
in case of single file.
Also Read : How to create logs file in laravel 8 / 9 ?