Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to create custom logs file in laravel 8 9

How to create custom logs file in laravel 8 / 9 ?

Aman Jain, May 16, 2022May 16, 2022

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 ?

Related

Php Laravel Laravel 9 laravellogsphp

Post navigation

Previous post
Next post

Related Posts

Php html string in blade in laravel

How to Render Html String in Blade in Laravel ?

March 8, 2022December 2, 2023

Blade is a template engine to write the syntax easily and powerfully. To render html string in blade in laravel we use {!! $htmlstring !!}. Using the blade template we can easily print a variable, can create loops and can create directives and components too. Laravel blade template uses filename.blade.php…

Read More
Php get last insert id in laravel

How to get last insert id in laravel with example ?

March 5, 2022February 22, 2024

In this article i will learn you to get last insert id in laravel, In laravel we can execute our queries in two ways first is using the laravel eloquent and other one is DB builder. so in the both pattern we can get the last inserted i in laravel….

Read More
Php How to Get Current Date, Time and Day in Laravel

How to Get Current Date Time and Format in Laravel ?

September 2, 2022March 16, 2024

Laravel has carbon library to get the current date time and format in laravel and also we can use core php date functions to fetch the date and time. After php 5, php also introduced DateTime class to conduct the operations of date time related functionality. Sometimes in our application…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version