I recently took over a product stream whose code was approximately two years old. I was shocked to see that it didnít have any logging mechanism in place, which scared the hell out of me. In this article, I'll explain why it scared me, and the best ways to implement logging in PHP applications.
If there is no logging mechanism, then if there's a goof-up in a production environment, you have absolutely no idea what went wrong. The only thing which a support developer can do in this case is to reproduce the issue at the developer end, which sometimes work and sometimes don't.
In a Lamp stack (PHP environment), logs are being generated at many places. Apache creates its logs, PHP creates its logs and MySQL also creates its logs. You can get a lot of information from these logs, especially when there are errors. The problem with these logs is that they are server-specific rather than application-specific, so they would not log anything specific to the application.
Application logs are very important, and they must be implemented in even trivial applications. Some of the very basic reasons for which an application log is important are debugging during development, troubleshooting in a production environment, gathering statistics, fixing security loopholes, and so forth.
Basically, application logs can be divided into the following categories.
Trace logs: Trace logs are for troubleshooting. They contain the traces of what was executed when from which page and line. So trace logs are meant for developers and can be technical in nature. An example would look like this:
02:49:06 05/07/08 [critical] 1210108746 Could not create instance with dsn mysql://:@/ C:xampphtdocsicore4includeISms.class.php 43 superadmin
In this log the values are in the following order: server time, date, priority, timestamp, log message, filename, line number and the user who issued this.
Audit logs: Audit logs keep track of logins and logouts of the users. This helps in security breaches.
User logs / History: User logs are meant for end user consumption. These logs are not for troubleshooting and generally don't have priority attached to them. They serve as a history for the application. These logs should not be technical in nature. A typical example for this would be:
02:49:06 05/07/08 A new user foo was created by superadmin