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
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'] = 'firstname.lastname@example.org' : 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.
blog comments powered by Disqus