Importance of logging in web development

Software development is an exercise in managing complexity, and the more tools and methods we have at our disposal the easier it gets. One of my favorites is logging – recording important code execution points that I think are important, especially during debugging. Whenever developing functions or class methods I make it a point to add logging capabilities to the same. Logging helps one to quickly tracks bugs and evaluate the health of the system under development.

But what information does one log and what does one keep out. This usually depends on what kind of application you are developing. While developing database intensive applications I prefer to log every query execution state – the whole CRUD things. This helps me to quickly pinpoint any query errors and the sequence in which the queries are run.

Take an example class method addUser() shown below. When the query is executed the result is logged in a file with the function name and a user_id or a serialized error string in case of an error. Whenever the application is in a testing phase, the error logs are an immense help in diagnosing error points. This is better than a splattering of print_r statements all around the code.

define("LOG",  true);

/** Add new user to the system
 *  @param array $user_data
 *  @return boolean
public function  addUser($user_data)
    if(!$sql->execute()) {
        if(LOG) {
            $err_string = serialize($sql->errorInfo());
            $this->_log->LogError("addUserd | error | " . $err_string);
        return 0;
    } else {
        if(LOG) {
            $this->_log->LogInfo("addUser| no. | " . $user_id);
        return 1;

The contents of a sample log file are shown below.

2010-07-31 4:52:58 - INFO  | addUser | no. | h1280551978 
2010-07-31 4:53:04 - INFO  | user edit | no. | 1 
2010-07-31 4:53:06 - INFO  | user edit | no. | 1 
2010-07-31 4:53:07 - INFO  | addUser| no. | 1 
2010-07-31 4:53:09 - ERROR | addUser| error | 
a:3:{i:0;s:5:"23";i:1;i:148;i:2;s:30:"Column 'fileno' cannot be null";} 

Although logs can add some overhead to your code, it is a small price to pay for the benefits it provides. And if you do not want the logging feature you can easily disable it by setting the LOG constant to ‘false’ in the above code.

Which logger to use

Although there a variety of libraries, I prefer KLogger as it has a small footprint and is easy to use. Other more advanced contender is Log4PHP, a PHP port of log4j, a Java logger. Zend_Log also looks nice but I am yet to give it a try.

Analyzing logs

For big, frequently used applications, log files can quickly get quite huge. The easy way to analyze them is to use GUI tools like LogMX and Apache Chainsaw. But if you just need to get some simple statistics from your logs, you could use the trusty old grep and other *nix utilities. For example to count the total error lines having the string ‘mysql’ you could use the following code.

$ grep 'ERROR ' activity.log | grep 'mysql' | wc -l

The advantage of scripts over GUI tools is that you can automatically process them using CRON jobs, e.g – emailing the admin if some particular type of errors in a log reaches some critical limit.

In conclusion, logs are one of the simplest ways of keeping track of your code problems or to check the execution sequence of your statements. This can be immensely helpful during development and for tracking down bugs.

4 thoughts to “Importance of logging in web development”

  1. Some things I like about Zend_Log:

    * easy syntax: $log->info(‘my message’); $log->warn(‘uh oh’);
    * one input, many “writers” (output channels): mail, log file, database entry etc
    * bind many writers to same log instance, but filter writers by level of log message (for example, really bad stuff might be sms:ed (not out of the box) or emailed (out of the box :)))

  2. I use log4net and love it for the flexibility in its configuration. You can dynamically change up the logging so that in “standard” mode it is logging only the error and fatal messages, and then switch it over to “detailed” mode to get all of your nitty gritty debug details when something goes wrong.

  3. I completely agree that logs are a dev’s best tool. However, it’s really important that any log contains enough information to correctly diagnose a problem, but not too much information that searching through it becomes difficult.

    For errors of the nature you describe, I actually much prefer an email log. This allows enough information to be added to the log to actually recreate the request. You can include full GET and POST, plus a stack trace that’ll hopefully show what went wrong along the way.

Leave a Reply

Your email address will not be published.