Home arrow PHP arrow Page 2 - Logging in PHP Applications

Basic Design - PHP

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



Before we move on to how we can implement logs, let's take a look at what should be the basic design consideration for logs.

  1. File system or database: To store logs, a database as well as a file can be used. The disadvantage with using a database is that the write time is longer, causing an undue load on the database server. The disadvantage with a file system is that the read and searching time is longer than the database server.

    The advantage of using a database server is better organization of the logs and better sorting, searching and display capability. So, the rule of thumb is that if the logging is being done excessively, then use a file system to log it; otherwise, you can use a database. Generally, trace logs are more frequently logged, so it would be a good idea to log them to a file system. Audit logs and user logs can be logged to a database.

  1. Priority: There should be an option for logging according to the priority, and this priority should be configurable. Sometimes logging slows the application. In that case, there should be an option of restricting the logs to higher priority issues or stop the application from logging altogether.

  1. Size limit: There should be an option to restrict the total size of the logs, otherwise they may flood the disk (in the worst case). I have seen one application which only had an option to restrict the logs by the number of days -- i.e., you could have logs for 1 day , 2 days ... n days. This application always had this problem in which it would flood the disk with logs and slow the application, because sometimes even when using a one-day window, gigabytes of logs would get created. So there has to be a configurable upper size limit if you're using a file system and a maximum number of rows if you're using a database to contain the total size of logs.

  1. Log file location: The log file location should be configurable, because in some cases you want the log to be in other drives or some location which is other than the install location.

  1. Timestamp: Log messages must contain a timestamp even though they contain the server date time. In some cases you will need to compare the logs of two application instances whose server time could be different from each other.

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