Logging in PHP Applications - The writelog() function (
Page 5 of 5 )
Now, I will walk you through the MyLog :: writelog() function briefly. Once the instance invokes this function with arguments, the function will write the message to the log file specified. But before that it goes through the following steps:
-
Check the total log file size: see the code snippet for logic and implementation.
-
Prepend the log message with unix timestamp:
$logmsg = mktime()." ".$logmsg ;
-
Append file name and line no to the log message :
$logmsg .= " ".$file_name ;
$logmsg .= " ".$line_no ;
-
Append user login to the log message :
$logmsg .= " ".$_SESSION[current_login_id] ;
-
Log the message through Pear::Log::log function
$this->logger->log($logmsg, $priority) ;
-
Send any email if required :
if ($priority <= $myGlobal['log']['email_level'] ){
// send email
$this->sendemail($logmsg, $priority) ;
}
Application level logging is very important and should be adapted to the application. There are no hard and fast rules for how to implement them. Furthermore, logging is an overhead to the application, so it should be implemented in a way which costs the least and is able to give you most of the important information.