Last updated on February 17, 2018
In this post, I am going show logging arrays and viewing logged items in the terminal in a very generic way. For this tutorial purpose, we are using laravel 5 and you can use these techniques with any framework.
Logging an array is not so straightforward though because PHP will not automatically convert an array to a string. Therefore you will need to use Log in conjunction with the print_r function.
Adding a second parameter to print_r will return a string with a printed variable.
Log::info(print_r($array, true));
This technique will be very useful when debugging an application in production.In fact, you use this technique with any framework. The Laravel logging facilities provide a simple layer on top of the powerful Monolog library. By default, Laravel is configured to create daily log files for your application which are stored in the storage/logs directory.
If you want to view the lasted logged items in the terminal window, you can use the following command –
$ tail -f storage/logs/laravel.log
tail
is used to display the end of a text file or piped data.tail
has a special command line option -f (follow) that allows a file to be monitored. Instead of just displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages.