DEV Community

Sanchita Paul
Sanchita Paul

Posted on • Edited 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


<?php

namespace App\Logging;

use Monolog\Logger;

class CustomLogger
{
    /**
     * Create a custom Monolog instance.
     */
    public function __invoke(array $config): Logger
    {
        return new Logger(
            env('APP_NAME'),
            [
                new CustomLoggerHandler(),
            ]
        );
    }
}

Enter fullscreen mode Exit fullscreen mode

and CustomLoggerHandler.php


<?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)  {
            $message->from('privacy@gmail.com')
                ->to("hello@gmail.com")
                ->subject("Email Log");
        });
        \Log::info("yoyo");
        \Log::info($record['message']);
        \Log::info(json_encode($record));
        \Log::info("yoyo");
    }
}

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


<html>

<head>
    <!-- Your title goes here -->
    <title>Email</title>
    <!-- End title -->
</head>

  <!-- 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>

  </div>

  </body>

</html>

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 () {
    Log::channel('email')->info('Bug');
});

Enter fullscreen mode Exit fullscreen mode

For more information check

https://laravel.com/docs/10.x/logging#monolog-channel-customization

https://laracasts.com/discuss/channels/laravel/logging-to-mattermost-custom-driver

Thanks

Top comments (1)

Collapse
 
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.