git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1246 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
131 lines
4.6 KiB
PHP
131 lines
4.6 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"*/
|
|
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
|
|
@param string $message Message to log
|
|
@param integer|null $priority Priority to use */
|
|
public function log ($message, $priority=LOG_NOTICE)
|
|
{
|
|
if ($this->loglevelmin < $priority)
|
|
return;
|
|
$back = reset (debug_backtrace());
|
|
$message .= " [".basename ($back["file"]).":".$back["line"]."]";
|
|
$logsType = explode ("|", $this->logtype);
|
|
if (in_array ("display", $logsType))
|
|
$this->logdisplay ($message, $priority);
|
|
if (in_array ("file", $logsType))
|
|
$this->logfile ($message, $priority);
|
|
if (in_array ("syslog", $logsType))
|
|
$this->logsyslog ($message, $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");
|
|
}
|
|
|
|
if (!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);
|
|
}
|
|
}
|