Skip to content

Instantly share code, notes, and snippets.

@barbuza
Created January 24, 2010 20:57
Show Gist options
  • Save barbuza/285444 to your computer and use it in GitHub Desktop.
Save barbuza/285444 to your computer and use it in GitHub Desktop.
package ru.barbuza {
import flash.external.ExternalInterface;
public class Logger {
public static const DEBUG:Number = 5;
public static const INFO:Number = 4;
public static const WARNING:Number = 3;
public static const ERROR:Number = 2;
public static const OFF:Number = 1;
private var name:String;
private var level:Number = 1;
private static var defaultLevel:Number;
private static var loggers:Vector.<Logger> = new Vector.<Logger>();
public function Logger(value:String) {
name = value;
level = defaultLevel;
}
public static function basicConfig(level:Number):void {
defaultLevel = level;
}
public static function getLogger(name:String):Logger {
for (var i:Number=0; i<loggers.length; i++) {
if (loggers[i].name == name) {
return loggers[i];
}
}
var logger:Logger = new Logger(name);
loggers.push(logger);
return logger;
}
public function setLevel(value:Number):void {
level = value;
}
public function debug(message:String):void {
if (level >= DEBUG) {
log('debug', message);
}
}
public function info(message:String):void {
if (level >= INFO) {
log('info', message);
}
}
public function warn(message:String):void {
if (level >= WARNING) {
log('warn', message);
}
}
public function error(message:String):void {
if (level >= ERROR) {
log('error', message);
}
}
private function log(method:String, message:String):void {
message = name.toUpperCase() + ': ' + message;
ExternalInterface.call('console.' + method, message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment