Article

How-To: Tail Apache logs to Ubuntu Notifications

Like many other developers I aspire to write clean, efficient code and while I don't always achieve that goal I am always on the lookout for ways to help me in that pursuit.

It always makes me a little sad when I look at an error log file to try and diagnose a problem only to find it bloated with deprecation and notice warnings which are easily preventable with a little care and attention. I know you can turn off logging for these types of errors but I feel it is better to fix these issues rather than ignore them.

To that end, I have created a little utility script to tail the apache error log and trigger an Ubuntu Notification each time the error log is updated. This allows me to be aware of, and resolve these errors during the development phase so they do not make it through to the live environment.

error-notification-pic.jpg

Create the script

Create a file called error-notifications.sh with the following contents (paths to the error log might vary depending on your configuration).

#!/bin/sh

apache_log() {
  while inotifywait -e modify /var/log/apache2/error.log; do
    line=$(tail -n1 /var/log/apache2/error.log | cut -c 81-);
    notify-send "$line"; 
  done
}

apache_log &

I'm using "cut -c 81-" to remove the first 81 characters as these are not particularly relevant to the issue (timestamp, pid, client id etc) so I want to remove that and just see the error messages.  

Start the script

You can start the script running by

sh error-log-notifications.sh > /dev/null 2>&1 &

Watch multiple files

If you wish to tail multiple logs at the same time i.e. the apache error log and the mail log at the same time you can achieve this by modifying the script to something like :-

#!/bin/sh

apache_log() {
  while inotifywait -e modify /var/log/apache2/error.log; do
    line=$(tail -n1 /var/log/apache2/error.log | cut -c 81-);
    notify-send "$line"; 
  done
}


mail_log() {
  while inotifywait -e modify /var/log/mail.log; do
    line=$(tail -n1 /var/log/mail.log);
    notify-send "$line"; 
  done
}

apache_log &
mail_log &

If you simply want to watch multiple apache error logs (i.e. you have a different error log file for each vhost, you can simply change the original script to (assuming filename syntax is in the form of [SITENAME].error.log):-

#!/bin/sh

apache_log() {
  while inotifywait -e modify /var/log/apache2/*.error.log; do
    line=$(tail -n1 /var/log/apache2/*.error.log | cut -c 81-);
    notify-send "$line"; 
  done
}

apache_log &

Do you have any tips and tricks to help you write better code? Leave a comment below and let us know.

me.jpeg

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for the The Drum in Glasgow, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Chris's Reading List

Medium

Medium

How a single change boosted organic traffic by 40%

How a single change boosted organic traffic by 40%

The SEO world can sometimes be a confusing maze of contradictions. Read more

verraes.net

verraes.net

Sensible Interfaces

Sensible Interfaces

How did we get from 'Program to an interface, not to an implementation' to 'Just slap an interface on there, it's the fashionable thing to do'? Read more

Moz

Moz

The Linkbait Bump: How Viral Content Creates Long-Term Lift in Organic Traffic - Whiteboard Friday

The Linkbait Bump: How Viral Content Creates Long-Term Lift in Organic Traffic - Whiteboard Friday

A fantastic piece of content can lift your traffic curves to a "new normal." In today's Whiteboard Friday, Rand talks about exactly how that happens, and how you can create content that has that effect. Read more

Loige

Loige

6 Rules of thumb to build blazing fast web applications (server side)

6 Rules of thumb to build blazing fast web applications (server side)

In this post are described 6 extremely important rules you should take under consideration when writing your next web application Read more

SitePoint

SitePoint

Console Wars - PHP CLI Libraries

Console Wars - PHP CLI Libraries

Nicola Pietroluongo compares three popular PHP console libraries, building the same functionality in each. Wondering which one is right for you? Find out! Read more

TechCrunch

TechCrunch

Servers.com Brings Its Bare-Metal Servers To The US

Servers.com Brings Its Bare-Metal Servers To The US

Servers.com, a hosting company with a focus on dedicated bare-metal servers that launched in Europe in 2005, today announced the opening of its first U.S... Read more

Advertisement

Digital Ocean