DEV Community

Sanchita Paul
Sanchita Paul

Posted on • Updated on

Create Custom Logger In Laravel

In this tutorial we will try to send email using custom logger

First of all we need to create a channel for our custom log in config/logging.php

 'channels' => [

        'email' => [
            'driver' => 'custom',
            'via' => \App\Logging\CustomLogger::class

Enter fullscreen mode Exit fullscreen mode

Once you have configured the custom driver channel, you're ready to define the class that will create your Monolog instance. This class only needs a single __invoke method which should return the Monolog logger instance. The method will receive the channels configuration array as its only argument:

Next we need to create out custom logger and handler. I create a new directory under app called Logging and in there I have CustomLogger.php


namespace App\Logging;

use Monolog\Logger;

class CustomLogger
     * Create a custom Monolog instance.
    public function __invoke(array $config): Logger
        return new Logger(
                new CustomLoggerHandler(),

Enter fullscreen mode Exit fullscreen mode

and CustomLoggerHandler.php


namespace App\Logging;

use Monolog\Handler\AbstractProcessingHandler;
use Snowfire\Beautymail\BeautymailFacade;

class CustomLoggerHandler extends AbstractProcessingHandler
    public function write(array $record)

        BeautymailFacade::send('emails.my_mail', ['record' => $record], function ($message)  {
                ->subject("Email Log");

Enter fullscreen mode Exit fullscreen mode

Now we need a blade file for email. Lett's create a blade file inside resources/views/emails/my_mail.blade.php


    <!-- Your title goes here -->
    <!-- End title -->

  <!-- You can change background colour here -->
  <body style="text-align: center; margin: 0; padding-top: 10px; padding-bottom: 10px; padding-left: 0; padding-right: 0; -webkit-text-size-adjust: 100%;background-color: #f2f4f6; color: #000000" align="center">

  <!-- Fallback force center content -->
  <div style="text-align: center;">

    <h1> {{$record['message']}} </h2>




Enter fullscreen mode Exit fullscreen mode

That's it. Now I can call. For testing purpose I am using api route.

Route::get('/test-email-log', function () {

Enter fullscreen mode Exit fullscreen mode

For more information check


Top comments (1)

david_s_8a48f4a4b90 profile image
David Salazar • Edited

Not sure if you actually tested this but this won't work. You need to add a constructer to your handler otherwise it will throw an exception because that handler is extending an abstract.

Secondly you can't be using the Log facade inside the logger otherwise you may end up in a recursive loop.

Thanks for the article anyway. Helped point me in the right direction.