How To Create Custom Log File in Laravel 10 Tutorial

Reading Time: 4 minutes
617 Views

In Laravel 10, managing logs is critical for monitoring application operations, resolving issues, and obtaining important insights into your system’s behaviour. While Laravel includes a robust logging system, there may be times when you need to create custom log files to separate and categorise certain sorts of log information.

In this comprehensive article, we will walk you through the process of generating custom log files in Laravel 10. By creating your own log channels and setting their behaviour, you may easily separate logs depending on criteria such as application modules, environments, or specialised functionality.

Read More: How To Create Custom Route File in Laravel 10 Tutorial

Log files are records that record events or actions that take place within a system or programme. Errors, warnings, user activity, and other critical system events are examples of such events.

Laravel by default provide /storage/logs/laravel.log file location where it stores application logs. But sometime we may need to create log file with specific task. For example, if someone works with payment task and need all logs at a fixed place, so this article will help you.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Custom Log Settings

Open logging.php file from /config folder. Inside logging.php search for channels.

Inside channels array, add your settings for custom log.

'channels' => [

    ...       

    'webtutorlog' => [
        'driver' => 'single',
        'path' => storage_path('logs/applicationlog.log'),
        'level' => 'info',
    ],

],

webtutorlog is channel name. applicationlog.log is custom log file name where we will store our custom log messages.

Read More: How To Create Custom Artisan Command in Laravel 10

storage_path(‘logs/applicationlog.log’) It returns path as /storage/logs/applicationlog.log

Create Test Logs

Open web.php from /routes folder. Add this route into it.

//...

use Illuminate\Support\Facades\Log;

Route::get('create-log', function () {
  
    Log::channel('webtutorlog')->info('This is info log level for testing');
    Log::channel('webtutorlog')->warning('This is warning log level for testing');
    Log::channel('webtutorlog')->error('This is error log level for testing');
    Log::channel('webtutorlog')->alert('This is alert log level for testing');
    Log::channel('webtutorlog')->emergency('This is emergency log level for testing');
    Log::channel('webtutorlog')->notice('This is notice log level for testing');
    
    dd('done');
     
});

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/create-log

You will get applicationlog.log file inside /storage/logs folder. When you open, you should see these lines added into it.

[2023-04-08 10:48:06] local.INFO: This is info log level for testing  
[2023-04-08 10:48:06] local.WARNING: This is warning log level for testing  
[2023-04-08 10:48:06] local.ERROR: This is error log level for testing  
[2023-04-08 10:48:06] local.ALERT: This is alert log level for testing  
[2023-04-08 10:48:06] local.EMERGENCY: This is emergency log level for testing  
[2023-04-08 10:48:06] local.NOTICE: This is notice log level for testing  

We hope this article helped you to learn How To Create Custom Log File in Laravel 10 Tutorial in a very detailed way.

Read More: How To Generate Unique Slug in Laravel 10 Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.