Thursday, 12 July 2012

File Logger class using PHP

log file is a recording of everything that goes in and out of a particular server. It is a concept much like the black box of an airplane that records everything going on with the plane in the event of a problem. The information is frequently recorded chronologically, and is located in the root directory, or occasionally in a secondary folder, depending on how it is set up with the server. The only person who has regular access to the log files of a server is the server administrator, and a log file is generally password protected, so that the server administrator has a record of everyone and everything that wants to look at the log files for a specific server.










This is very simple class which is used to create file log.


<?php
/**
 * Trycatch Developers
 *
 *
 * @package File Log
 * @author Shijith.K.M
 * @copyright     Copyright (c)
 * @license
 * @link http://trycatchdev.blogspot.in/
 * @since Version 1.0
 * @filesource
 */




class Log
{


    /**
     * Reading a file
     * @param str $filename The name of the file
     * 
     */
    
    public function Read($filename)
    {
         if(!is_writable($filename))
         {
            echo "Change your CHMOD permission to ".$filename;
            die();
         }
         
         $handle = fopen($filename, 'r');
         
         return file_get_contents($filename);
    }
    
    /**
     * Writing a file
     * @param str $filename The name of the file
     * @param str $strData  Data to be append 
     */
    
    public function Write($filename,$strData)
    {   
         
         
         if(!is_writable($filename))
         {
            echo "Change your CHMOD permission to ".$filename;
            die();
         }
         
         $handle = fopen($filename, 'a+');
         
         fwrite($handle, "\r".$strData);
         
         fclose($handle);
        
    }
        
}






$log = new Log(); 


$log->Write('fee_log.txt','First Log');


echo '<pre>';
echo $log->Read('fee_log.txt');

No comments:

Post a Comment