topical media & game development
professional-php-11-class.Logger.php / php
<?php
//Log Levels. The higher the number, the less severe the message
//Gaps are left in the numbering to allow for other levels
//to be added later
define('LOGGER_DEBUG', 100);
define('LOGGER_INFO', 75);
define('LOGGER_NOTICE', 50);
define('LOGGER_WARNING', 25);
define('LOGGER_ERROR', 10);
define('LOGGER_CRITICAL', 5);
class Logger {
private logLevel;
//Note: private constructor. Class uses the singleton pattern
private function __construct() {
global this->logLevel = logFilePath = logFilePath)) {
throw new Exception('No log file path was specified ' .
'in the system configuration.');
}
//Open a handle to the log file. Suppress PHP error messages.
//We'll deal with those ourselves by throwing an exception.
logFilePath, 'a+');
if(! is_resource(logFilePath " .
'could not be opened or created for ' .
'writing. Check file permissions.');
}
}
public function __destruct() {
if(is_resource(this->hLogFile);
}
}
public static function getInstance() {
static objLog)) {
objLog;
}
public function logMessage(logLevel = LOGGER_INFO, logLevel <= time = strftime('\%x \%X', time());
msg);
msg);
this->levelToString(module)) {
module);
module);
}
//logs: date/time loglevel message modulename
//separated by tabs, new line delimited
time\tmsg\tthis->hLogFile, logLevel) {
switch (
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.