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.

  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



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