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 watch-apache-log.sh with the following contents (paths to the error log might vary depending on your configuration).

Github Gist Placeholder

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

Github Gist Placeholder

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 :-

Github Gist Placeholder

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):-

Github Gist Placeholder

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!

Want more like this?

Keep up-to-date with latest news from Inside the Agency using any of the following services

Advertisement

Digital Ocean

Chris's Reading List

www.thoughtworks.com

BFF @ SoundCloud

Our friends at SoundCloud embarked on a journey a few years ago that changed their system and moved it from a monolithic Rails application towards an ecosystem of microservices. Read more

The Drum

The Drum

Google'€™s AMP is bringing some much needed speed to mobile but the transition won'€™t be easy for publishers

A week after launching, the Google-led Accelerated Mobile Pages (AMP) initiative has raised more questions than answers for publishers... Read more

SitePoint

SitePoint

Quick Tip: How to Permanently Change SQL Mode in MySQL

MySQL 5.7 introduced some awkward changes for older codebases and tends to break apps. Here's how to temporarily (or permanently) lower the strictness level Read more

SitePoint

SitePoint

How to Build Your Own Dependency Injection Container

Andrew Carter builds a Dependency Injection container from scratch, showing you how that rocket science is really just fireworks strapped to lego figures! Read more

AuthorityLabs

AuthorityLabs

13 Reasons Your Organic Traffic is in Decline | It's Not a Penalty

The landscape of search engine optimization has changed drastically in the last 3-4 years. Even as it changes, a lot stays the same. It has been very challenging to see a multitude of friends and colleagues approach me with problems that often seem beyond repair with clients that they have worked with for years. Read more

TechCrunch

TechCrunch

WordPress Sites Now Support Google’s AMP To Make Mobile Pages Load Much Faster

Google has some big plans when it comes to making the web faster on your mobile phone. The company just added AMP-enabled pages in its mobile search results... Read more