Home arrow PHP arrow Page 5 - Logging in PHP Applications

The writelog() function - 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

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:


  1. Check the total log file size: see the code snippet for logic and implementation.


  1. Prepend the log message with unix timestamp:
    $logmsg = mktime()." ".$logmsg ;


  1. Append file name and line no to the log message :
    $logmsg .= " ".$file_name ;
    $logmsg .= " ".$line_no ;


  1. Append user login to the log message :
    $logmsg .= " ".$_SESSION[current_login_id] ;


  1. Log the message through Pear::Log::log function
    $this->logger->log($logmsg, $priority) ;


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



 
 
>>> More PHP Articles          >>> More By Shikhar Kumar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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