1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  import org.as2lib.env.log.Logger;
    19  
    20  import logging.IPublisher;
    21  import logging.IFilter;
    22  import logging.Level;
    23  import logging.util.List;
    24  
    25  /**
    26   * {@code AudiofarmLogger} acts as a wrapper for a {@code logging.Logger} instance
    27   * of the Logging Framework for ActionScript 2 (as2logger) from Ralf Siegel.
    28   * 
    29   * <p>Configure the as2logger API as normally and just use this class in your
    30   * application to log messages. This enables you to switch between almost every
    31   * available Logging API without having to change the logs in your application but
    32   * just the underlying configuration on startup.
    33   * 
    34   * @author Simon Wacker
    35   * @see <a href="http://code.audiofarm.de/Logger/">as2logger - Logging Framework for ActionScript 2</a>
    36   */
    37  class org.as2lib.env.log.logger.AudiofarmLogger extends BasicClass implements Logger {
    38  	
    39  	/**
    40  	 * Indicates that all messages shall be logged. This level is equivalent to the
    41  	 * as2logger {@code ALL} level.
    42  	 */
    43  	public static var ALL:Level = Level.ALL;
    44  	
    45  	/**
    46  	 * Indicates that all messages at debug and higher levels shall be logged. This
    47  	 * level is equivalent to the as2logger {@code CONFIG} level.
    48  	 */
    49  	public static var DEBUG:Level = Level.CONFIG;
    50  	
    51  	/**
    52  	 * Indicates that all messages at info and higher levels shall be logged. This
    53  	 * level is equivalent to the as2logger {@code INFO} level.
    54  	 */
    55  	public static var INFO:Level = Level.INFO;
    56  	
    57  	/**
    58  	 * Indicates that all messages at warning and higher levels shall be logged. This
    59  	 * level is equivalent to the as2logger {@code WARNING} level.
    60  	 */
    61  	public static var WARNING:Level = Level.WARNING;
    62  	
    63  	/**
    64  	 * Indicates that all messages at error and higher levels shall be logged. This
    65  	 * level is equivalent to the as2logger {@code SEVERE} level.
    66  	 */
    67  	public static var ERROR:Level = Level.SEVERE;
    68  	
    69  	/**
    70  	 * Indicates that all messages at fatal and higher levels shall be logged. This
    71  	 * level is equivalent to the as2logger {@code SEVERE} level.
    72  	 */
    73  	public static var FATAL:Level = Level.SEVERE;
    74  	
    75  	/**
    76  	 * Indicates that no messages shall be logged; logging shall be turned off. This
    77  	 * level is equivalent to the as2logger {@code OFF} level.
    78  	 */
    79  	public static var NONE:Level = Level.OFF;
    80  	
    81  	/** The {@code Logger} instance of as2logger every task is delegated to. */
    82  	private var logger:logging.Logger;
    83  	
    84  	/** Debug level. */
    85  	private var debugLevel:Level;
    86  	
    87  	/** Info level. */
    88  	private var infoLevel:Level;
    89  	
    90  	/** Warning level. */
    91  	private var warningLevel:Level;
    92  	
    93  	/** Error level. */
    94  	private var errorLevel:Level;
    95  	
    96  	/** Fatal level. */
    97  	private var fatalLevel:Level;
    98  	
    99  	/**
   100  	 * Constructs a new {@code AudiofarmLogger} instance.
   101  	 * 
   102  	 * <p>Gets an as2logger {@code Logger} instance via the
   103  	 * {@code logging.Logger.getLogger} method.
   104  	 *
   105  	 * @param name the name of this logger
   106  	 */
   107  	public function AudiofarmLogger(name:String) {
   108  		this.logger = logging.Logger.getLogger(name);
   109  		this.debugLevel = DEBUG;
   110  		this.infoLevel = INFO;
   111  		this.warningLevel = WARNING;
   112  		this.errorLevel = ERROR;
   113  		this.fatalLevel = FATAL;
   114  	}
   115  	
   116  	/**
   117  	 * Returns the name of this logger.
   118  	 *
   119  	 * @return the name of this logger
   120  	 */
   121  	public function getName(Void):String {
   122  		return this.logger.getName();
   123  	}
   124  	
   125  	/**
   126  	 * Returns the parent for this logger.
   127  	 * 
   128  	 * <p>This method returns the nearest extant parent in the namespace. Thus if a
   129  	 * logger is called "a.b.c.d", and a logger called "a.b" has been created but no
   130  	 * logger "a.b.c" exists, then a call of {@code getParent} on the logger "a.b.c.d"
   131  	 * will return the logger "a.b".
   132  	 * 
   133  	 * <p>The parent for the anonymous logger is always the root (global) logger.
   134  	 * 
   135  	 * <p>The result will be {@code undefined} if it is called on the root (global) logger
   136  	 * in the namespace.
   137  	 * 
   138  	 * @return the parent of this logger
   139  	 */
   140  	public function getParent(Void):logging.Logger {
   141  		return this.logger.getParent();
   142  	}
   143  	
   144  	/**
   145  	 * Returns a list with publishers associated with this logger.
   146  	 * 
   147  	 * @return a list with publishers that are associated with this logger
   148  	 */
   149  	public function getPublishers(Void):List {
   150  		return this.logger.getPublishers();
   151  	}
   152  	
   153  	/**
   154  	 * Adds a new publisher to this logger.
   155  	 * 
   156  	 * @param publisher the publisher to add
   157  	 * @return {@code true} if the {@code publisher} was added successfully else
   158  	 * {@code false}
   159  	 */
   160  	public function addPublisher(publisher:IPublisher):Boolean {
   161  		return this.logger.addPublisher(publisher);
   162  	}
   163  	
   164  	/**
   165  	 * Removes the given {@code publisher} from this logger.
   166  	 * 
   167  	 * @param publisher the publisher to remove
   168  	 * @return {@code true} if the {@code publisher} was removed successfully else
   169  	 * {@code false}
   170  	 */
   171  	public function removePublisher(publisher:IPublisher):Boolean {
   172  		return this.logger.removePublisher(publisher);
   173  	}
   174  	
   175  	/**
   176  	 * Returns the current filter for this logger.
   177  	 * 
   178  	 * @return this logger's current filter or {@code undefined}
   179  	 */
   180  	public function getFilter(Void):IFilter {
   181  		return this.logger.getFilter();
   182  	}
   183  	
   184  	/**
   185  	 * Sets a new filter for this logger.
   186  	 * 
   187  	 * @param filter the new filter to set
   188  	 */
   189  	public function setFilter(filter:IFilter):Void {
   190  		this.logger.setFilter(filter);
   191  	}
   192  	
   193  	/**
   194  	 * Returns the log level specified for this logger. The result may be undefined,
   195  	 * which means that this logger's effective level will be inherited from its
   196  	 * parent. 
   197  	 * 
   198  	 * @return this logger's level
   199  	 */
   200  	public function getLevel(Void):Level {
   201  		return this.logger.getLevel();
   202  	}
   203  	
   204  	/**
   205  	 * Set the log level specifying which messages at which levels will be logged by
   206  	 * this logger.
   207  	 * 
   208  	 * <p>Message levels lower than this value will be discarded. The level
   209  	 * value {@link OFF} can be used to turn off logging.
   210  	 * 
   211  	 * <p>If the new level is {@code undefined}, it means that this node should inherit
   212  	 * its level from its nearest ancestor with a specific (non-undefined) level value.
   213  	 * 
   214  	 * @param level the new level
   215  	 */
   216  	public function setLevel(level:Level):Void {
   217  		this.logger.setLevel(level);
   218  	}
   219  	
   220  	/**
   221  	 * Checks if this logger is enabled for debug level log messages.
   222  	 *
   223  	 * @return {@code true} if debug messages are logged
   224  	 * @see #debug
   225  	 * @see AudiofarmLogger#DEBUG
   226  	 */
   227  	public function isDebugEnabled(Void):Boolean {
   228  		return this.logger.isLoggable(this.debugLevel);
   229  	}
   230  	
   231  	/**
   232  	 * Checks if this logger is enabled for info level log messages.
   233  	 *
   234  	 * @return {@code true} if info messages are logged
   235  	 * @see #info
   236  	 * @see AudiofarmLogger#INFO
   237  	 */
   238  	public function isInfoEnabled(Void):Boolean {
   239  		return this.logger.isLoggable(this.infoLevel);
   240  	}
   241  	
   242  	/**
   243  	 * Checks if this logger is enabled for warning level log messages.
   244  	 *
   245  	 * @return {@code true} if warning messages are logged
   246  	 * @see #warning
   247  	 * @see AudiofarmLogger#WARNING
   248  	 */
   249  	public function isWarningEnabled(Void):Boolean {
   250  		return this.logger.isLoggable(this.warningLevel);
   251  	}
   252  	
   253  	/**
   254  	 * Checks if this logger is enabled for error level log messages.
   255  	 *
   256  	 * @return {@code true} if error messages are logged
   257  	 * @see #error
   258  	 * @see AudiofarmLogger#ERROR
   259  	 */
   260  	public function isErrorEnabled(Void):Boolean {
   261  		return this.logger.isLoggable(this.errorLevel);
   262  	}
   263  	
   264  	/**
   265  	 * Checks if this logger is enabled for fatal level log messages.
   266  	 *
   267  	 * @return {@code true} if fatal messages are logged
   268  	 * @see #fatal
   269  	 * @see AudiofarmLogger#FATAL
   270  	 */
   271  	public function isFatalEnabled(Void):Boolean {
   272  		return this.logger.isLoggable(this.fatalLevel);
   273  	}
   274  	
   275  	/**
   276  	 * Logs the message object to wrapped as2logger {@code Logger} at debug level.
   277  	 * 
   278  	 * <p>The debug level is equivalent to the fine level of as2logger.
   279  	 *
   280  	 * @param message the message object to log
   281  	 * @see #isDebugEnabled
   282  	 * @see AudiofarmLogger#DEBUG
   283  	 */
   284  	public function debug(message):Void {
   285  		if (isDebugEnabled()) {
   286  			this.logger.fine(message);
   287  		}
   288  	}
   289  	
   290  	/**
   291  	 * Logs the message object to wrapped as2logger {@code Logger} at info level.
   292  	 *
   293  	 * @param message the message object to log
   294  	 * @see #isInfoEnabled
   295  	 * @see AudiofarmLogger#INFO
   296  	 */
   297  	public function info(message):Void {
   298  		if (isInfoEnabled()) {
   299  			this.logger.info(message);
   300  		}
   301  	}
   302  	
   303  	/**
   304  	 * Logs the message object to wrapped as2logger {@code Logger} at warning level.
   305  	 *
   306  	 * @param message the message object to log
   307  	 * @see #isWarningEnabled
   308  	 * @see AudiofarmLogger#WARNING
   309  	 */
   310  	public function warning(message):Void {
   311  		if (isWarningEnabled()) {
   312  			this.logger.warning(message);
   313  		}
   314  	}
   315  	
   316  	/**
   317  	 * Logs the message object to wrapped as2logger {@code Logger} at error level.
   318  	 * 
   319  	 * <p>The error level is equivalent to the severe level of as2logger.
   320  	 *
   321  	 * @param message the message object to log
   322  	 * @see #isErrorEnabled
   323  	 * @see AudiofarmLogger#ERROR
   324  	 */
   325  	public function error(message):Void {
   326  		if (isErrorEnabled()) {
   327  			this.logger.severe(message);
   328  		}
   329  	}
   330  	
   331  	/**
   332  	 * Logs the message object to wrapped as2logger {@code Logger} at fatal level.
   333  	 * 
   334  	 * <p>The fatal level is equivalent to the severe level of as2logger.
   335  	 *
   336  	 * @param message the message object to log
   337  	 * @see #isFatalEnabled
   338  	 * @see AudiofarmLogger#FATAL
   339  	 */
   340  	public function fatal(message):Void {
   341  		if (isFatalEnabled()) {
   342  			this.logger.severe(message);
   343  		}
   344  	}
   345  	
   346  }