Location: PHPKode > scripts > Log4Pi > log4pi/Logger.php
<?php
/*
	+-----------------------------------------------------------------------+
	| Log4Pi version 1.0                                                 	|
	+-----------------------------------------------------------------------+
    | Copyright (C) 2009  Thomas Wehner <hide@address.com>		|
	|																		|
    | This program is free software: you can redistribute it and/or modify	|
    | it under the terms of the GNU General Public License as published by	|
    | the Free Software Foundation, either version 3 of the License, or		|
    | (at your option) any later version.									|
	| 																		|
    | This program is distributed in the hope that it will be useful,		|
    | but WITHOUT ANY WARRANTY; without even the implied warranty of		|
    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			|
    | GNU General Public License for more details.							|
	| 																		|
    | You should have received a copy of the GNU General Public License		|
    | along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 	+-----------------------------------------------------------------------+
*/

/**
 * Log4Pi - class for doeing simple log-output.
 * @version 1.0
*/
class Logger {
	
	protected $sClass = null;
	protected $objErrorFH = null;
	protected $objDebugFH = null;
	
	/**
	 * Constructor - needs the caller's filename with php-extension.
	 * @param String
	 * @throws LoggerException
	*/
	public function Logger($sFileName)
	{
		$this->sClass = $sFileName;
		$sRoot = str_replace($sFileName, "", $_SERVER['SCRIPT_FILENAME']);
		$sDebugLogName = "log_" . date("Y-m-d", time()) . ".log";
		$sErrorLogName = "log_" . date("Y-m-d", time()) . ".log.err";
		
		if(!$this->objDebugFH = fopen($sRoot . $sDebugLogName, "a+"))
		{
			throw new LoggerException("Could not open debug-logfile: " . $sDebugLogName,
				LoggerException::DEBUGFILE_ACCESS_DENIED);
		}
		
		if(!$this->objErrorFH = fopen($sRoot . $sErrorLogName, "a+"))
		{
			throw new LoggerException("Could not open error-logfile " . $sErrorLogName,
				LoggerException::ERRORFILE_ACCESS_DENIED);
		}
	}
	
	/**
	 * Writes an debug-message to the debug-file.
	 * @param String
	 * @param int
	 * @throws LoggerException
	*/
	public function debug($sMsg, $iLine)
	{
		if($this->objDebugFH == null)
		{
			throw new LoggerException("Debug-logfile is not opened.",
				LoggerException::DEBUGFILE_NOT_OPENED);
		}
		
		$sFormattedMsg = date("Y-m-d H:i:s", time()) . " " . $this->sClass
			. " (".$iLine."): ".$sMsg."\n";
		fwrite($this->objDebugFH, $sFormattedMsg);
	}
	
	/**
	 * Writes an error-message to the error-file.
	 * @param String
	 * @param int
	 * @throws LoggerException
	*/
	public function error($sMsg, $iLine)
	{
		if($this->objErrorFH == null)
		{
			throw new LoggerException("Error-logfile is not opened.",
				LoggerException::ERRORFILE_NOT_OPENED);
		}
		
		$sFormattedMsg = date("Y-m-d H:i:s", time()) . " " . $this->sClass
			. " (".$iLine."): ".$sMsg."\n";
		fwrite($this->objErrorFH, $sFormattedMsg);
	}
	
	public function __destruct()
	{
		if($this->objErrorFH != NULL)
		{
			fclose($this->objErrorFH);
		}

		if($this->objDebugFH != null)
		{
			fclose($this->objDebugFH);
		}
		
		$this->objErrorFH = null;
		$this->objDebugFH = null;
	}
}

/**
 * Exception class for Logger extends Exception.
 * @version 1.0
*/
class LoggerException extends Exception {

	const DEBUGFILE_ACCESS_DENIED = 100;
	const ERRORFILE_ACCESS_DENIED = 101; 
	const DEBUGFILE_NOT_OPENED = 110;
	const ERRORFILE_NOT_OPENED = 111;
	
	/**
	 * Constructor - calls the parent-class Exception.
	 * @param String
	 * @param int
	*/
	public function LoggerException($sErrMsg, $iErrCode)
	{
		parent::__construct($sErrMsg, $iErrCode);
	}
}
?>
Return current item: Log4Pi