Skip to content

Instantly share code, notes, and snippets.

@Martyr2 Martyr2/logger.php
Created Sep 25, 2017

Embed
What would you like to do?
Simple static logger class for logging errors. Provides convenience logging methods, date stamping the log file, getting call source and specifying log location.
<?php
class Logger {
private static $timestamp = 'Y-m-d H:i:s';
private static $logFileNameFormat = 'Y-m-d';
private static $logDirLocation = __DIR__;
private static $logLevels = ['DEBUG', 'INFO', 'WARN', 'ERROR'];
private static $minLogLevel = 'INFO';
private function __construct() {
// Prevent instantiation
}
/**
* Easy accessor functions
*/
public static function logDebug($message) {
self::log($message, self::$logLevels[0]);
}
public static function logInfo($message) {
self::log($message, self::$logLevels[1]);
}
public static function logWarning($message) {
self::log($message, self::$logLevels[2]);
}
public static function logError($message) {
self::log($message, self::$logLevels[3]);
}
/**
* Get the name of the file that is calling on the log functionality and return it.
* This might directly calling the log() function or one of the helper methods so we have
* to look for the first file that is not the local file.
*
* @return string
*/
private static function getCallSource() {
$backtrace = debug_backtrace(FALSE, 3);
$levelsDeep = count($backtrace);
if ($levelsDeep > 0) {
foreach ($backtrace as $traceLevel) {
if (isset($traceLevel['file']) && strtolower($traceLevel['file']) != strtolower(__FILE__)) {
return $traceLevel['file'];
}
}
}
return '';
}
private static function getLogLevel($level) {
if (($level != null) && in_array(strtoupper($level), self::$logLevels)) {
return $level;
} else {
return self::$minLogLevel;
}
}
private static function isLocationWritable() {
return is_writable(self::$logDirLocation);
}
public static function setDirectoryLocation($dirPath) {
if (is_string($dirPath)) {
$path = realpath($dirPath);
if (is_dir($path)) {
self::$logDirLocation = $path;
return true;
}
}
return false;
}
private static function openLog($filePath) {
$f = fopen($filePath, 'a');
if ($f !== false) {
return $f;
}
return false;
}
public static function log($message, $level = null) {
if (is_string($message) && (trim($message) != '')) {
if (self::isLocationWritable()) {
$logTimestamp = date(self::$timestamp);
$callingSource = self::getCallSource();
$logLevel = self::getLogLevel($level);
$filePath = self::$logDirLocation . '/' . date(self::$logFileNameFormat) . '.log';
$handle = self::openLog($filePath);
if ($handle !== false) {
fwrite($handle, "[{$logLevel}]\t[{$logTimestamp}] {$message} - Source: {$callingSource}" . PHP_EOL);
fclose($handle);
} else {
throw new Exception('Unable to open log file for writing.');
}
} else {
throw new Exception('Unable to write to log file. Write permission to this file location is denied.');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.