Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Created January 3, 2019 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-rai/72def0de447c4f1bb7d67e8d9abf5616 to your computer and use it in GitHub Desktop.
Save pete-rai/72def0de447c4f1bb7d67e8d9abf5616 to your computer and use it in GitHub Desktop.
A simple PHP logger class with timestamps and trace support
<?php
/*
* A simple PHP logger class with timestamps and trace support
*
* Released with the karmaware tag - https://pete-rai.github.io/karmaware
*
* Website : http://www.rai.org.uk
* GitHub : https://github.com/pete-rai
* LinkedIn : https://uk.linkedin.com/in/raipete
* NPM : https://www.npmjs.com/~peterai
*
*/
date_default_timezone_set ('UTC'); // important - do not remove and leave as first line or move to somewhere in calling code
// --- a simple logging class
class Log
{
protected static $seperator = ' | '; // column seperator
protected static $maxSize = 256; // max column size
protected static $lineSize = 80; // size of a seperator line
protected static $header = true; // want log headers
protected static $tracer = false; // want trace output
public static function header ($header) { self::$header = $header; }
public static function tracer ($tracer) { self::$tracer = $tracer; }
public static function trace (/* varg list*/) { if (self::$tracer) self::out ('---', func_get_args ()); }
public static function info (/* varg list*/) { self::out (' ', func_get_args ()); }
public static function warn (/* varg list*/) { self::out ('!!!', func_get_args ()); }
public static function error (/* varg list*/) { self::out ('***', func_get_args ()); }
public static function line () { echo str_repeat ('-', self::$lineSize)."\n"; }
protected static function tidy ($text)
{
if (is_array ($text)) $text = implode (',', $text);
$text = substr ($text, 0, self::$maxSize); // truncate too long text
$text = str_replace ("\n", " [newline] ", $text); // tidy up unprintables
$text = str_replace ("\r", " [return] " , $text);
$text = str_replace ("\t", " [tab] " , $text);
return $text;
}
protected static function out ($class, $args)
{
$msg = array_shift ($args);
$text = implode (self::$seperator, array_map ('self::tidy', $args));
$now = date ('YmdHis');
$head = self::$header ? "$now - $class - " : '';
echo "$head $msg".($text ? " - $text" : '')."\n";
}
}
/*
// --- test code only - leave commented out once working
Log::line ();
Log::Info ('test1', 123, ['foo', 'bar']); // data types, objects and arrays are handled
Log::warn ('test2', 456, 'catflap', ["gusset\nspoon"]); // \n will be replaced with [newline]
Log::error ('test3', 789, 'plinth', "dollop\tjuice"); // \t will be replaced with [tab]
Log::line ();
Log::header (false);
Log::Info ('test1', 123, ['foo', 'bar']); // outputs without a header - ugly!
Log::header (true);
Log::line ();
Log::trace (__FILE__, __LINE__); // no trace output by default
Log::tracer (true); // switch on trace output
Log::trace (__FILE__, __LINE__); // now outputs trace messages
Log::line ();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment