static logging class for Code Igniter

Here’s a little time waster that makes logging a bit more pleasurable in Code Igniter:

/**
 * Log class to enable logging directly to a level
 *
 * static examples:
 *
 * Log::info('message');
 * Log::write('message', 'error');
 * Log::write('message'); // default level is 'ERROR'
 * Log::setDefaultLevel('info');
 *
 * instance examples:
 *
 * $log = new Log();
 * $log->info("message");
 *
 * $log2 = new Log('debug'); // sets default level
 * $log2->write("message");
 *
 * Note: static use of DefaultLevel is not thread safe
 */
class Log {

	public static $Level = array('NONE', 'ERROR', 'DEBUG',  'INFO', 'ALL');
	public static $DefaultLevel = 'ERROR';

	public static function write($message, $level=null)
	{
		if (! isset($level) )
		{
			$level = self::$DefaultLevel;
		}

		log_message($level, $message);
	}

	public static function setDefaultLevel($default_level)
	{
		$default_level = strtoupper($default_level);

		if ( in_array($default_level, self::$Level) )
		{
			self::$DefaultLevel = $default_level;
		}
		else
		{
			// fail silently for invalid log level
		}
	}

	public static function error($message)
	{
		log_message('ERROR', $message);
	}

	public static function info($message)
	{
		log_message('INFO', $message);
	}

	public static function debug($message)
	{
		log_message('DEBUG', $message);
	}

	function __construct($default_level = null)
	{
		$default_level = strtoupper($default_level);

		if ( in_array($default_level, self::$Level) )
		{
			self::$DefaultLevel = $default_level;
		}
		else
		{
			// fail silently for invalid log level
		}
	}

	function __call($method, $args)
	{
		$level = strtoupper($method);

		if ( in_array($level, self::$Level) )
		{
			if ( isset($args) )
			{
				$log_message($level, $args);
			}
		}
		else
		{
			// fail silently for invalid log level
		}
	}
}

And here are some tests:

log_message('info', 'testing log_message()');

Log::Info('testing static Log::Info');
Log::deBUG('testing static Log::deBUG');
Log::error('testing static Log::error');
Log::Write('testing static Log::Write with level', 'info');
Log::WRITE('testing static Log::WRITE with no level specified');

$log = new Log('debug');
$log->info('testing instantiated $l->info()');
$log->DEBUG('testing instantiated $log->DEBUG()');
$log->Error('testing instantiated $log->Error()');
$log->write('testing instantiated $log->write() with level', 'info');
$log->write('testing instantiated $log->write() with no level specified');
Log::setDefaultLevel('INFO');
$log->write('testing instantiated $log->write() with no level specified after changing default level');
Advertisements

One thought on “static logging class for Code Igniter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s