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