<?php
@author pear@laurent-laville.org @access @license http://www.php.net/license/3_0.txt class myPHPLogger
{
var $_display;
var $_log;
function myPHPLogger($displayConf = array(), $logConf = array())
{
$displayDefault = array(
'lineFormat' => '<b>%1$s</b>: %2$s %3$s <br/>',
'contextFormat' => ' in <b>%3$s</b> (file <b>%1$s</b> at line <b>%2$s</b>)'
);
$this->_display = array_merge($displayDefault, $displayConf);
$logDefault = array(
'eol' => "\n",
'lineFormat' => '%1$s %2$s [%3$s] %4$s %5$s',
'timeFormat' => '%b %d %H:%M:%S',
'ident' => $_SERVER['REMOTE_ADDR'],
'message_type' => 3,
'destination' => 'myerrors.log',
'extra_headers' => ''
);
$this->_log = array_merge($logDefault, $logConf);
}
function log($err)
{
if (isset($err['context'])) {
$context = $err['context'];
} else {
$context = false;
}
if ($context) {
$file = $context['file'];
$line = $context['line'];
if (isset($context['class'])) {
$func = $context['class'];
$func .= $context['type'];
$func .= $context['function'];
} elseif (isset($context['function'])) {
$func = $context['function'];
} else {
$func = '';
}
}
$display = $display_errors = ini_get('display_errors');
$log = $log_errors = ini_get('log_errors');
if ($display) {
$lineFormat = $this->_display['lineFormat'];
if (substr($lineFormat, -1) != "\n") {
$lineFormat .= "\n";
}
$contextFormat = $this->_display['contextFormat'];
if (!$context) {
$contextExec = '';
} else {
$contextExec = sprintf($contextFormat, $file, $line, $func);
}
printf($lineFormat, ucfirst($err['level']), $err['message'], $contextExec);
echo '<br/><hr/>';
}
if ($log) {
$message_type = $this->_log['message_type'];
$destination = '';
$extra_headers = '';
$send = true;
switch ($message_type) {
case 0: break;
case 1: $destination = $this->_log['destination'];
$extra_headers = $this->_log['extra_headers'];
break;
case 3: $destination = $this->_log['destination'];
break;
default:
$send = false;
}
if ($send) {
$contextExec = sprintf($this->_display['contextFormat'], $file, $line, $func);
$message = sprintf($this->_log['lineFormat'] . $this->_log['eol'],
strftime($this->_log['timeFormat'], $err['time']),
$this->_log['ident'],
$err['level'],
$err['message'],
$contextExec);
error_log(strip_tags($message), $message_type, $destination, $extra_headers);
}
}
}
}
$defLog = new myPHPLogger();
$displayConfig = array(
'lineFormat' => '<b>%1$s</b>: %2$s <br/>%3$s<br/>',
'contextFormat' => ' <i>File:</i> %1$s <br />'
. ' <i>Line:</i> %2$s <br />'
. ' <i>Function:</i> %3$s '
);
$customLog = new myPHPLogger($displayConfig);
?> |