Skip to content

Instantly share code, notes, and snippets.

@dz1984
Last active August 29, 2015 13:56
Show Gist options
  • Save dz1984/9181586 to your computer and use it in GitHub Desktop.
Save dz1984/9181586 to your computer and use it in GitHub Desktop.
Tiny Log helper.
<?php
/**
* Log helper functions
*
* This class can log error messages to a file or the console.
*
* It can take an error message and format it to include the date and the severity level.
*
* The message can be appended to a file or outputted to the console if the severity level is above the minimum severity threshold.
*
* @license MIT
* @package helpers
* @author Donald Zhan
*
* @example
*
* // setup the threshold of level
* LoggerHelper::setThreshold(LoggerHelper::ALL);
*
* // choice the console output
* LoggerHelper::setMode('console');
*
* LoggerHelper::info("It is a info level message");
*
* LoggerHelper::error("WTF! Appear the error");
*
*/
class LoggerHelper
{
// output mode
const FILE = 'file';
const CONSOLE = 'console';
// log level list
const ERROR = 1;
const DEBUG = 2;
const WARN = 3;
const INFO = 4;
const ALL = 5;
private static $_threshold = 1;
private static $_mode = self::CONSOLE;
private static $_output = null;
private static $_levels = array(
'ERROR' => self::ERROR,
'DEBUG' => self::DEBUG,
'WARN' => self::WARN,
'INFO' => self::INFO,
'ALL' => self::ALL
);
/**
* log the message to output stream
*
* @param string $msg
* @param string $level
* @param boolean $newline
* @return boolean
*/
public static function log($msg, $level = 'MSG', $newline = true)
{
if (self::$_levels[$level] > self::$_threshold) {
return false;
}
$output = self::_getOutput();
$message = self::format($msg, $level, $newline);
if (!$fp = @fopen($output, 'ab')) {
return false;
}
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
/**
* handle the message format.
*
* @param string $msg
* @param string $level
* @param boolean $newline
* @return boolean
*/
public static function format($msg, $level = 'MSG', $newline = true)
{
$datetime = date("Y-m-d H:i:s");
$message = "$datetime [$level] $msg";
if ($newline) {
$message = $message . ((self::$_mode == self::CONSOLE) ? '<br/>' : PHP_EOL);
}
return $message;
}
/**
* Log the infomation message
*
* @param type $msg
* @param type $newline
*/
public static function info($msg, $newline = true)
{
self::log($msg, 'INFO', $newline);
}
/**
* Log the error message
*
* @param string $msg
* @param boolean $newline
*/
public static function error($msg, $newline = true)
{
self::log($msg, 'ERROR', $newline);
}
/**
* Log the warning message.
*
* @param string $msg
* @param boolean $newline
*/
public static function warn($msg, $newline = true)
{
self::log($msg, 'WARN', $newline);
}
/**
* Log the debug message
*
* @param string $msg
* @param boolean $newline
*/
public static function debug($msg, $newline = true)
{
self::log($msg, 'DEBUG', $newline);
}
/**
* set the threshold value of log level
*
* @param type $threshold
*/
public static function setThreshold($threshold)
{
self::$_threshold = $threshold;
}
/**
* set the output mode
*
* @param type $mode
* @throws Exception
*/
public static function setMode($mode)
{
if (self::_isInValidMode($mode)) {
throw new Exception('The logger mode type is error.');
}
self::$_mode = $mode;
}
/**
* return the output stream string
*
* @return string
*/
private static function _getOutput()
{
if (is_null(self::$_output)) {
switch (self::$_mode) {
case self::CONSOLE:
self::$_output = 'php://output';
break;
case self::FILE:
self::$_output = LOG_PATH . DIRECTORY_SEPARATOR . self::_getLogName();
break;
}
}
return self::$_output;
}
/**
* return the log file name
*
* @return string
*/
private static function _getLogName()
{
$datetime = date("Y-m-d");
$ext = '.log';
$file_name = "$datetime" . $ext;
return $file_name;
}
/**
* check the mode is INVALID
*
* @param string $mode
* @return boolean
*/
private static function _isInValidMode($mode)
{
return !self::_isValidMode($mode);
}
/**
* check the mode is VALID
*
* @param string $mode
* @return boolean
*/
private static function _isValidMode($mode)
{
$_cls = new ReflectionClass(__CLASS__);
$_mode_val = array_values($_cls->getConstants());
$result = in_array($mode, $_mode_val);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment