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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: