Source for file Logger.php
Documentation is available at Logger.php
* LICENSE: 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.
* @author Hector Ortiz (FreeNAC Core Team)
* @copyright 2007 FreeNAC
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License Version 2
* Define the Logger class which is a Singleton which provides for logging facilities.
* This class uses the constants defined by Syslog, which are listed as follows:
* - LOG_EMERG 0 System is unusable
* - LOG_ALERT 1 Action must be taken immediately
* - LOG_CRIT 2 Critical conditions
* - LOG_ERR 3 Error conditions
* - LOG_WARNING 4 Warning conditions
* - LOG_NOTICE 5 Normal, but significant condition
* - LOG_INFO 6 Informational message
* - LOG_DEBUG 7 Debug-level-message
* - $logger=Logger::getInstance(); Needed, before other lines
* - $logger->setDebugLevel(3); Log debug1,2,3 (default is only 1)
* - $logger->setLogToStdErr(); If you don't want to use syslog
* - $logger->logit("Hello world"); Will appear in syslog or stderr
* - $logger->debug("Hello debug world"); Will be prefixed "debug1"
* - $logger->debug("Hello debug world",3); Will be prefixed "debug3"
/* Define some other values for the facility to open
* The ones defined by default are:
* Maximum debugging level
private $debug_level=
NULL; //Current debugging level
private static $instance=
NULL; //Instance of this class
private $identifier=
NULL;
private $stdout_stream =
NULL;
private $stderr_stream =
NULL;
private $httpd_log=
false; //Log to webserver log?
private $email_alert=
false; //Send Err or higher via email
* Open logging facilities and start output buffering
private function __construct($facility=
LOG_DAEMON)
$this->identifier=
basename($_SERVER['SCRIPT_FILENAME']); #Get script's name as identifier
* Close logging facilities and flush output buffering
* Divert logging to Email
* @param boolean $var Activate or deactivate LogToEmail logging. Default is to activate ($var=true)
* @return boolean True if successful, false otherwise
if (is_bool($var) &&
($var===
true))
$this->email_alert=
false;
* Divert logging to httpd
* @param boolean $var Activate or deactivate httpd logging. Default is to activate ($var=true)
if (is_bool($var) &&
($var===
true))
* Divert logging to StdErr
* @param boolean $var Activate or deactivate StdErr logging. Default is to activate ($var=true)
public function setLogToStdErr($var=
true) //Redirect logging to stderr
if (is_bool($var) &&
($var===
true))
* Divert logging to StdOut
* @param boolean $var Activate or deactivate StdOut logging. Default is to activate ($var=true)
public function setLogToStdOut($var=
true) //Redirect logging to stderr
if (is_bool($var) &&
($var===
true))
* Set the script name which will be displayed in syslog
* @param mixed $name Name to set to
* @return boolean True if successful, false otherwise
* The name displayed in syslog
return $this->identifier;
* Get instance of this class
* @return object Current instance
public static function getInstance($facility=
LOG_DAEMON)
if (empty(self::$instance)) //Is there an instance of this class?
self::$instance=
new Logger($facility); //No, then create it
return self::$instance; //Yes, return it
* Prevent clonning the instance
* @throws Exception indicating that copy can't be performed
throw
new Exception("Cannot clone the SysLogger object");
* Log a message to the HTTPD log
* This method is a wrapper around the error_log function
* @return boolean True if successful, false otherwise
* @param mixed $message Message to log
* @param integer $criticality How critical is this message? Default is informational
* @return boolean True if successful, false otherwise
public function logit($message=
'',$criticality=
LOG_INFO)
if (($criticality<
0) ||
($criticality >
7)) #Sanity check, defaults to LOG_INFO if user entered an invalid value
if ($criticality ==
LOG_ERR)
$message=
"ERROR: $message";
else if ($criticality ==
LOG_WARNING)
$message=
"WARNING: $message";
if ($this->stderr) #Should we log to stderr?
if ($this->facility ==
WEB )
$fd =
fopen($this->stderr_stream,'w');
if ($this->facility ==
WEB )
$fd =
fopen($this->stdout_stream,'w');
syslog((int)
$criticality,$message); #Log it through syslog
$this->loghttpd($message); #Should we log to Weblog?
error_log($message,1,"root"); #TBD: Use a variable for email?
# error_log($message,3,"/var/log/mylog"); #TBD: Use a ariable for mail?
* This is a wrapper around the php mail function
* @return boolean True is mail successfully sent, false otherwise
public function mailit($subject,$message,$to=
'root')
return mail($to, $subject, $message);
* Wrapper around the logit method. Log a message only if the specified level for this function
* is less or equal than the current debugging level.
* @param mixed $msg Message to log
* @param integer $to_level Debug level where this message should be displayed. Default 1.
* @return boolean True if successful, false otherwise
public function debug($msg,$to_level=
1)
#Perform sanity checks to see if both the current debugging level and the specified level are valid values
#according to MAX_DEBUG_LEVEL
if ($this->debug_level <=
0)
if ($this->debug_level >
self::MAX_DEBUG_LEVEL)
$this->debug_level=
self::MAX_DEBUG_LEVEL;
if ($to_level >
self::MAX_DEBUG_LEVEL)
$to_level=
self::MAX_DEBUG_LEVEL;
#The specified level falls within our current debugging level?
if ($this->debug_level &&
($to_level<=
$this->debug_level) &&
(strlen($msg)>
0))
$mymsg=
"Debug$to_level: $msg"; #Include debugging level in the message
$this->logit($mymsg,LOG_DEBUG); #Log it
* Get the current debugging level.
* @return integer Current debug level
return $this->debug_level;
* Set debugging level. It will cause to print all debugging messages less or equal than the value we specify in this function.
* @param integer $var Debug level. Default 1. 0 means no debugging
* @return boolean True if successful, false otherwise
$this->logit("Value passed to setDebugLevel is not an integer",LOG_WARNING);
* Wrapper around the logit method.
* This method logs by default to the stdout stream defined by the chosen facility
* @param mixed $error The error message to display
* @return boolean True if successful, false otherwise
* Wrapper around the logit method.
* This method logs by default to the stderr stream defined by the chosen facility
* @param mixed $error The error message to display
* @return boolean True if successful, false otherwise
$this->logit($error, LOG_ERR);
* Open logging facility specified for the user
* @param integer $facility Facility to open. Default is LOG_DAEMON
* @return boolean True if successful, false otherwise
switch($facility) #Sanity checks
$this->facility=
$facility;
$this->stdout_stream =
STDOUT;
$this->stderr_stream =
STDERR;
return openlog($this->identifier,LOG_CONS |
LOG_NDELAY |
LOG_PID, $this->facility);
$this->facility=
$facility;
$this->stdout_stream =
"php://output";
$this->stderr_stream =
"php://stderr";
return openlog($this->identifier,LOG_CONS |
LOG_NDELAY |
LOG_PID, $this->facility);
$this->logit("Value passed to openFacility is not recognized",LOG_WARNING);
Documentation generated on Mon, 17 Nov 2008 01:10:40 +0100 by phpDocumentor 1.4.0