There are times when I’m debugging code on a linux box that I want to see what’s being logged by a Web Site or Web Application. What I used to have to do was do something on the website, then open up a terminal in Ubuntu (or any other linux distro for that matter) and view the logfile with either vim, nano, cat | less or gvim.
How I used text editors to do it
I use VIM as my default text editor on Ubuntu or any other linux distros (such as redhat, centos, debian, linux mint) that I work on.
A few weeks from now I used to use vim to open the log file, then the :e (edit) command to reload the new log entries.
Based on the entry, I make adjustments as required, then try again. Back to vim, issue the edit command, review the changes, then run the script again. So on and so forth.
Now this is kind of did what I wanted it to do, but what I really wanted it to do was display the log in realtime so that I can just keep going back to the terminal and see what’s wrong without having to do the additional second step of running the edit command.
The way of opening up a log file in a text editor, then reloading the log file in the editor was very cumbersome. I needed a way of seeing the log file in realtime so that I did not have to keep reloading.
How to read a log file in real time on Linux
Just recently, I was bothered enough to look up how to view a log file in real time and regretted that I did not do this sooner!
Apparently, you can use tail to do this. The tail command has a very simple option -f which means “follow” with this option added when you run tail on your log file, it outputs the data in real time as it is written on the file
$tail -f /var/log/apache2/somelog-errors.log
Of course, all the other tail options are available. If you want it to display more than the last 10 lines, you can use the -n option to increase the number of lines it displays from the file.
$#displays last 100 lines in a log file.
$tail -f -n100 /var/log/apache2/somelog-errors.log
This is pretty handy especially when you’re trying to debug something that’s gone wrong on a remote box or have been handed some nasty procedural script that WSODs somewhere — yeap, I’m talking from real life, painful as f*ck experience here.
Till next time.
- Execute Linux commands in PHP by using backticks
- Importing a SQL file directly from Command Line for MySQL
- Disabling the Laptop TouchPad in Ubuntu Linux
- How to Get Gigabit Speeds from RTL8111/8168B PCI Express Gigabit Ethernet controller on Ubuntu Linux
- Ubuntu Linux: Tweak your network connectivity and/or performance by disabling IPV6