PHP: Easy to use logging class for your scripts.

Often for my work I am always having to add logging to most of my scripts so I had to create a simple plug and play script that will simply things to do this for me. This script below is rather clean and easy to handle your logging needs. The class also handles timestamps when submitting data to write to the logfile, and the logfile itself extends the actual date, so you get a new log file each day. Very clean and easy to manage!

The script:

class Logger {
	private $lName = null;  
	private $handle = null;  
 
	public function __construct($logName = null) {
		if ($logName) $this->lName = $logName; //Define Log Name!
		else $this->lName = "Log"; //Default name
		$this->logOpen(); //Begin logging.
	}
 
	function __destruct() {
	       fclose($this->handle); //Close when php script ends (always better to be proper.)
	}
 
	//Open Logfile
	private function logOpen(){  
		$today = date('Y-m-d'); //Current Date
		$this->handle = fopen($this->lName . '_' . $today, 'a') or exit("Can't open " . $this->lName . "_" . $today); //Open log file for writing, if it does not exist, create it.
  	} 
 
  	//Write Message to Logfile
  	public function logWrite($message){  
		$time = date('m-d-Y @ H:i:s -'); //Grab Time
		fwrite($this->handle, $time . " " . $message . "\n"); //Output to logfile
  	}
 
  	//Clear Logfile
  	public function logClear(){  
		ftruncate($this->handle, 0);
	}
}

Often the idea is to add the above coding to a loggerclass.php script or something similar, and use <?php include "loggerclass.php" ?> at the top of your scripts to include the logger class into any of your scripts. You can also just copy paste this to your current script without using an include. A few things to note:

-You don’t need to define the log file name, if you don’t, the name will be “Log_DATE” by default.
-There are two functions that you can use after the logger is defined, which is write to logfile, and truncate the logfile. The file is opened when you call the class and closed when the php script is complete, so you don’t need to handle this!
-If the logfile exists, it will append to the bottom of the logfile, you do not need to worry about it erasing automatically!
-If you are getting permission issues, chmod the directory where the logs are kept to 0777!

So now, if you wanted to use the logger class, an example of using it would be:

$log = new Logger("MyLog");  
$log->logWrite("This is a test message!");

This would create a logfile named “MyLog_Date” if it doesn’t exist, and add the message “This is a test message!” to it, with the timestamp. It would look something like this in If you do want to clear the logfile for some reason, you can do this with logClear(), and it will truncate the logfile for you! So the resulting output in the logfile, which would be named “MyLog_09-22-2010″ would be: “09-22-2010 @ 15:49:55 – This is a test message!”… Simple, right?

This should handle your logging needs, and keeps your coding short and clean! One line to open and start logging, and one line per message, which includes the timestamp automatically, nice!

Share

Related posts:

  1. PHP: How to get a random image to display from a specific folder
  2. PHP: GIF animation splitter (Split any GIF animiations – frame by frame properly!)
  3. VB.NET/CSV File – How to add ‘Double quote text qualifiers’ quickly and easily!
  4. PHP: Randomizing a text file using PHP + MYSQL, quickly & efficient

About the Author

I mainly focus on Javascript/PHP/C++/.NET applications for everyday and work. I also am working on a remake of Stellar Frontier, an old 2D top down space battle game with a few fellow programmers.