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 public $logtype = "display"; // display, file, syslog // can be merged with a pipe : "display|syslog" public $logfile = FALSE; public $timezone = "UTC"; public $loglevelmin = LOG_NOTICE; // See http://fr2.php.net/manual/en/function.openlog.php for $syslogFacility public $syslogFacility = LOG_USER; public $syslogPrefix = FALSE; /* 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 */ 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 */ 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 */ private function logdisplay ($message, $priority) { if (php_sapi_name () !== "cli") echo ""; 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 "
"; echo "\n"; } /** Log $message on syslog */ private function logsyslog ($message, $priority) { openlog ($this->syslogPrefix, NULL, $this->syslogFacility); syslog ($priority, $message); } }