/* * Copyright (c) 2005 Pablo Costantini (www.luminicbox.com). All rights reserved. * * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.mozilla.org/MPL/MPL-1.1.html * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import LuminicBox.Log.Level; import LuminicBox.Log.IPublisher; import LuminicBox.Log.LogEvent; /** * Main Class.
* This class contains methods for logging messages at differente levels.
* These messages can me basic types (strings, numbers, dates) or complex objects and MovieClips.
* There are also configuration methods. *

Example:

*
* 	import LuminicBox.Log.*;
* var log = new Logger();
* log.addPublisher( new TracePublisher() );
* // ...
* log.debug("debug message");
* log.info("info message");
* // ...
* var xml = new XML("<note><to>John</to><from>Dana</from><heading>Reminder</heading><body>Don´t forget the milk</body></note>");
* log.debug(xml);
*
*/ class LuminicBox.Log.Logger { private var _loggerId:String; private var _publishers:Array; private var _filters:Array; private var _level:Level; /** * Sets the lowest required level for any message.
* Any message that have a level that is lower than the supplied value will be ignored.
* This is the most basic form of filter. */ function setLevel(level:Level):Void { _level = level; } /** * Creates a new Logger instance.
* The logId parameter is optional. It identifies the logger and all messages to the publisher will be sent with this ID. * @param logId String (optional) */ function Logger(logId:String) { if(logId && logId.length > 0) this._loggerId = logId; this._level = Level.LOG; _publishers = new Array(); _filters = new Array(); } /** * Adds a Publisher to the publishers collection. The supplied publisher must implement the IPublisher interface
* There can only be one instance of each Publisher. */ function addPublisher(publisher:IPublisher):Void { if( !_publishers[publisher.toString()] ) _publishers[publisher.toString()] = publisher } /** * Removes a Publisher from the publishers collection. A new instance of that kind of publisher can be supplied. */ function removePublisher(publisher:IPublisher):Void { delete _publishers[publisher.toString()]; } /** * Return the publishers collection. */ function getPublishers():Array { return _publishers; } /*public function addFilter(filter:IFilter):Void { if( !_filters[filter.toString()] ) _filters[filter.toString()] = filter; } public function removeFilter(filter:IFilter):Void { delete _filters[filter.toString()]; }*/ private function publish(argument, level:Level):Void { if( level.getValue() >= _level.getValue() ) { var e:LogEvent = new LogEvent(this._loggerId, argument, level); for(var publisher:String in _publishers) { IPublisher(_publishers[publisher]).publish(e); } } } /* log functions */ /** * Logs an object or message with the LOG level. * @param argument The message or object to inspect. */ function log(argument):Void { publish(argument, Level.LOG); } /** * Logs an object or message with the DEBUG level. * @param argument The message or object to inspect. */ function debug(argument):Void { publish(argument, Level.DEBUG); } /** * Logs an object or message with the INFO level. * @param argument The message or object to inspect. */ function info(argument):Void { publish(argument, Level.INFO); } /** * Logs an object or message with the WARN level. * @param argument The message or object to inspect. */ function warn(argument):Void { publish(argument, Level.WARN); } /** * Logs an object or message with the ERROR level. * @param argument The message or object to inspect. */ function error(argument):Void { publish(argument, Level.ERROR); } /** * Logs an object or message with the FATAL level. * @param argument The message or object to inspect. */ function fatal(argument):Void { publish(argument, Level.FATAL); } //function inspect(argument):Void { publish(argument, Level.INSPECT); } }