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