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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: