
<?php
/*
* Licence: NONE, Use it as you like it, no guarantee, no warantee, use it at you own risk.
* Version 2006-09-10
* Author: Daniel Golesny, www.golesny.de
* Description:
* A very very simple library to make the logging easier in PHP.
* I don't need a big log4php (that seems to be dead) just a very
* little library, that is very fast and don't need to be configured much.
* ***
* Features:
* - Three log level: debug, warn, error
* - Switches depending on the HTTP_HOST the appender and loglevel
* on localhost it creates HTML-Output with debug, warn and error
* on the real server it sends a mail, if warn or error has occured
* be free to write to file.
* - For HTML-Output you have following Css-Styles to customize output:
* pre.log, .logDEBUG, .logWARN, .logERROR
* Example:
* pre.log {font-size: 10px; color: black; background-color:white; border: 1px solid black;}
* .logERROR {background-color: #ff0000;}
* .logWARN {background-color: #ff9900;}
* ***
* Usage:
* require_once("inc/log.inc.php");
* if (isDebugEnabled()) debug(__LINE__, __FILE__, "a debug message");
* error(__LINE__, __FILE__, "an error message");
* // End of the page, before closing body-tag
* processLogs();
*/
define("DEBUG", 2);
define("WARN", 1);
define("ERROR", 0);
define("LOGLEVEL", $_SERVER["HTTP_HOST"]=="localhost"?DEBUG:WARN);
define("APPENDER", $_SERVER["HTTP_HOST"]=="localhost"?"html":"mail"); // html or mail
define("APPENDER_MAIL_ADDR", "put.your@email.here");
$log = array();
function isDebugEnabled() {
return LOGLEVEL >= DEBUG;
}
/**
* @param level as String
* @param LINE
* @param FILE
* @param message
*/
function append($level, $line, $file, $message) {
$GLOBALS["log"][] = sprintf("[%s]%17s:%3d %s",
$level, basename($file), $line, $message);
}
/**
* @param LINE
* @param FILE
* @param message
*/
function debug($line, $file, $msg) {
append("DEBUG", $line, $file, $msg);
}
/**
* @param LINE
* @param FILE
* @param message
*/
function warn($line, $file, $msg) {
append("WARN ",$line, $file, $msg);
}
/**
* @param LINE
* @param FILE
* @param message
*/
function error($line, $file, $msg) {
append("ERROR", $line, $file, $msg);
}
/**
*
*/
function processLogs() {
if (sizeof ($GLOBALS["log"]) > 0) {
$msghead = date("Y-m-d H:m:s") . "\n";
$msghead .= "\nIP: ". $_SERVER["REMOTE_ADDR"]."\n";
if (APPENDER == "html") {
echo "<pre class=\"log\">";
echo $msghead;
foreach ($GLOBALS["log"] as $line) {
echo "<span class=\"log".trim(substr($line,1,5))."\">".$line."</span>\n";
}
echo "</pre>";
} else if (APPENDER == "mail") {
$msg = join($GLOBALS["log"], "\n");
$msg = $msghead . $msg;
// send mail
mail(APPENDER_MAIL_ADDR, "Log Output", $msg);
}
}
}
?>