Log Laravel

You can easily log your data in Larave, all you have to do is to use Log, and then log whatever you want, here is an example

namespace App\Http\Controllers;
use Log;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
     * Show the profile for the given user.
     * @param  int  $id
     * @return Response
    public function showProfile($id)
        Log::info('User ID: '.$id);
        return view('user.profile', ['user' => User::findOrFail($id)]);

You can also use an array of contextual data

Log::info('User failed to login.', ['id' => $user->id]);

Laravel provides 8 levels of logging


You can view the log file at: storage/laravel.log but you may find a huge file with thousands of line as this is the main file where all the logs are saved.

If you want to create your own log file, you will need to add a new channel in the config/logging.php

'channels' => [
        'myLog' => [
                    'driver' => 'single',
                    'path' => storage_path('logs/myLog.log'),
                    'level' => 'info',

where myLog.log is the file name you want to save your logs in

To write to your custom log file, you have to specify the channel you just created

Log::channel('myLog')->info('This is testing for my log file.');

ps. Don’t forget to empty the config cache for the updates to be active

from your terminal, you write: php artisan cache:clear

Finally, when you open myLog.log file, you will see something like this:

[2022-08-25 09:13:20] local.INFO: This is testing for my log file.

Leave a Reply

Your email address will not be published. Required fields are marked *