The many ways to tail the application log in Laravel
Jeffery Way recently tweeted about how he wants a php artisan tail
command to see the real-time application logs in Laravel. And the replies to this tweet were quite interesting. People have shared their ways of tailing the Laravel log. So, I thought to compile them all in one place.
The good old tail command
The first and the most obvious way to tail the Laravel log is by using the tail
command. You can use it like so.
tail -f storage/logs/laravel.log
This will show you the live log of your Laravel application. But, this is not the most convenient way to tail the log. You have to open a new terminal window, type the command, and then you can see the log.
Alias the tail command
To make the above command more convenient, you can alias it in your .bashrc
file like so.
alias tail-laravel="tail -f storage/logs/laravel.log"
Now, you can simply type tail-laravel
in your terminal and it will start tailing the log.
Composer Script
You can also create a composer script to tail the log. For this, you need to add the following script to your composer.json
file.
"scripts": {
"tail": "tail -f storage/logs/laravel.log"
}
Now, you can simply run composer tail
in your terminal and it will start tailing the log.
Spatie’s Laravel Tail Package
And if you want something advanced, you can use Spatie’s Laravel Tail package which provides you with a php artisan tail
command to tail the log.
The package gives you a few more features like filtering, using custom log files, or even using it on remote servers.
To install the package, you can use the following command.
composer require spatie/laravel-tail
Once installed, you can use the php artisan tail
command to tail the log straight away.
But if you want to use custom log files, you can do so by passing the --file
option like so.
php artisan tail --file="storage/logs/my-custom-log.log"
Or if you want to filter the logs with specific keywords, you can do so by passing the --grep
option like so.
php artisan tail --grep="error"
This will only show the logs that contain the word error
.
And lastly, you can start the output with displaying the last lines in the log by using the lines-option.
php artisan tail --lines=50
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.