How to Create Custom Log File in Laravel 8 Tutorial

Inside this article we will see how to create custom log file in laravel 8. We can easily track errors on files after looking into log file. Log files are those files in which application status like errors, information, warnings, etc stored. Log files help application developer to debug applications.

This tutorial will help you to create custom logs for your laravel application. You can store any log type messages like of errors, warnings, etc.

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.

Also for maintaining logs we have a composer package which exactly do the same thing what we are discussing. Click here to learn about LogViewer package and it’s working.

Learn More -

Let’s get started.

Installation of Laravel Application

  • Laravel Installer
  • By using composer

Laravel Installer

$composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command -

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

$composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel -

$php artisan serve

This command outputs -

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.

Add Custom Log Settings

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.

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

Create Test Logs

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

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

[2021-09-18 13:19:19] local.INFO: This is info log level for testing [2021-09-18 13:19:19] local.WARNING: This is warning log level for testing [2021-09-18 13:19:19] local.ERROR: This is error log level for testing [2021-09-18 13:19:19] local.ALERT: This is alert log level for testing [2021-09-18 13:19:19] local.EMERGENCY: This is emergency log level for testing [2021-09-18 13:19:19] 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 8 Tutorial in a very detailed way.

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

Find More on Laravel 8 Articles here

Originally published at https://onlinewebtutorblog.com on September 18, 2021.

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors