Home arrow PHP arrow Logging in PHP Applications

Logging in PHP Applications

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.

  1. Logging in PHP Applications
  2. Basic Design
  3. Trace Logs
  4. Code Explained
  5. The writelog() function
By: Shikhar Kumar
Rating: starstarstarstarstar / 14
December 08, 2008

print this article



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.

  1. 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.

  1. Audit logs: Audit logs keep track of logins and logouts of the users. This helps in security breaches.

  1. 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

>>> More PHP Articles          >>> More By Shikhar Kumar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: