Files
DomFramework/logger.php
2015-01-29 13:08:15 +00:00

169 lines
5.5 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
/** The logger class permit to log the informations from the soft
It allow to debug too */
class logger
{
/* The logger class can be used with :
$d=new logger;
$d->timezone = "Europe/Paris";
$d->logtype="display|file";
$d->logfile="/tmp/toto.log";
$d->logwrite ("Super log default");
$d->logwrite ("Super log DEBUG", $d::DEBUG);
$d->logwrite ("Super log NOTICE", $d::NOTICE); */
// TODO : Add SQL support
/** The method to log.
Can be display, file, syslog
Can be merged with a pipe : "display|syslog|file"*/
public $logtype = "display";
/** For logtype=file, the filename to use */
public $logfile = FALSE;
/** Timezone use to save the logs */
public $timezone = "UTC";
/** Minimum log level in the logs */
public $loglevelmin = LOG_NOTICE;
/** In Syslog mode, the facility to use
See http://fr2.php.net/manual/en/function.openlog.php for $syslogFacility */
public $syslogFacility = LOG_USER;
/** In Syslog, prefix the log by the text */
public $syslogPrefix = FALSE;
/** The priorities which can be used in the priorities
LOG_EMERG system is unusable
LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions
LOG_WARNING warning conditions
LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message */
private $priorities = array (LOG_EMERG => "EMERG",
LOG_ALERT => "ALERT",
LOG_CRIT => "CRITICAL",
LOG_ERR => "ERROR",
LOG_WARNING => "WARNING",
LOG_NOTICE => "NOTICE",
LOG_INFO => "INFO",
LOG_DEBUG => "DEBUG");
/** Store a new message log in the log manager defined by $logtype
The message can be multiple types. An array will be stored in textual form
but it will accept only one depth.
@param integer|null $priority Priority to use
@param mixed ...$message Message to log */
public function log ($priority=LOG_NOTICE, ...$message)
{
if ($this->loglevelmin < $priority)
return;
$backtrace = debug_backtrace();
$back = reset ($backtrace);
$msg = "";
if (isset ($_SERVER["REMOTE_ADDR"]))
{
$msg .= "[".$_SERVER["REMOTE_ADDR"]."] ";
}
// Convert each part of message to text
foreach ($message as $m)
{
if (is_array ($m))
{
$msg .= "[";
foreach ($m as $key=>$val)
{
if (is_array ($val))
{
foreach ($val as $key2=>$val2)
{
$msg .= "$key2=>$val2,";
}
}
else
{
$msg .= "$key=>$val,";
}
}
$msg .= "]";
}
else
{
$msg .= $m;
}
}
// Add the filename which generate the error
$msg .= " [".basename ($back["file"]).":".$back["line"]."]";
$logsType = explode ("|", $this->logtype);
if (in_array ("display", $logsType))
$this->logdisplay ($msg, $priority);
if (in_array ("file", $logsType))
$this->logfile ($msg, $priority);
if (in_array ("syslog", $logsType))
$this->logsyslog ($msg, $priority);
}
/** Log $message on file
@param string $message Message to log
@param integer|null $priority Priority to use */
private function logfile ($message, $priority)
{
if ($this->logfile === FALSE)
throw new Exception ("Undefined file where logging");
if (!file_exists ($this->logfile))
{
if (! is_dir (dirname ($this->logfile)))
throw new Exception ("You must create the ".dirname ($this->logfile).
"directory");
if (! is_writable (dirname ($this->logfile)))
throw new Exception ("The directory ".dirname ($this->logfile)." must"
." be writable to create log file");
}
elseif (!is_writable ($this->logfile))
{
if (function_exists ("posix_geteuid"))
$user = posix_geteuid ();
elseif (getenv('USERNAME') !== FALSE)
$user = getenv('USERNAME');
else
$user = "";
throw new Exception ("Logfile $this->logfile is not writable for user ".
$user);
}
ini_set ("date.timezone", $this->timezone);
$message = date ("Y/m/d H:i:s")." [".$this->priorities[$priority]."] ".
$message."\n";
file_put_contents ($this->logfile, $message, FILE_APPEND);
}
/** Log $message on screen with adding date. Add the HTML flags if not in CLI.
@param string $message Message to log
@param integer|null $priority Priority to use */
private function logdisplay ($message, $priority)
{
if (php_sapi_name () !== "cli")
echo "<tt>";
ini_set ("date.timezone", $this->timezone);
$message = date ("Y/m/d H:i:s")." [".$this->priorities[$priority]."] ".
$message;
echo "$message";
if (php_sapi_name () !== "cli")
echo "</tt><br/>";
echo "\n";
}
/** Log $message on syslog
@param string $message Message to log
@param integer|null $priority Priority to use */
private function logsyslog ($message, $priority)
{
openlog ($this->syslogPrefix, NULL, $this->syslogFacility);
syslog ($priority, $message);
}
}