Home arrow PHP arrow Page 4 - Logging in PHP Applications

Code Explained - 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
 

Here, we used a custom class MyLog which uses Pear::Log package (http://pear.php.net/package/Log/) through aggregation.

We used MyLog because we might want to further customize the class according to our logging needs; in fact, we did.

We used a global variable to configure several parameters; this global variable could be your application's global variable. The variables are:

$myGlobal['log']['logfile'] : specifies the log file location like "f:trace.log"

$myGlobal['log']['level'] : specifies the log level above which the logging should be done. The levels are :

0 System is unusable

1 Immediate action required

2 Critical conditions

3 Error conditions

4 Warning conditions

5 Normal but significant

6 Informational

7 Debug-level messages

These levels are from Pear::Log library.


$myGlobal['log']['size']: specifies the total log size. For example if you specify 32, it means 32 Mb can be the total size of the log files. Our class works by making two files of 16 MB each; once the second file nears completion, it deletes the first one. In this way, continuity is maintained in the logs.


$ myGlobal ['log']['email_address'] = 'info@example.com' : critical log messages will be sent to this mail id.


$ myGlobal ['log']['email_level'] = 1: log level above which (inclusive) the messages will be mailed to the email_address specified above.


In the application, you will have to invoke the MyLog instance before using it:


// logging object

$objMyLog = new MyLog() ;


Now, with this object you can log information in the application anywhere. Do make sure that the scope of this instance is global.


// logging in a script

$GLOBALS['objMyLog']->writelog("Could not create instance with dsn $dsn", 2, __FILE__, __LINE__) ;


In this statement the first argument is the log message; the second argument is the priority, which is 2 here; and the third and fourth arguments are filename and line number respectively.



 
 
>>> 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 10 - Follow our Sitemap

Dev Shed Tutorial Topics: