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  /**
    21   * {@code ZtorLog4fLogger} delegates all messages to the {@code Log4f.log} method of
    22   * the ZTOR Log4f project.
    23   * 
    24   * <p>Using this class instead of the {@code Log4f} class in your application
    25   * directly enables you to switch between almost every available Logging API without
    26   * having to change the logging calls in your application but just the configuration
    27   * on startup.
    28   *
    29   * <p>Every global configuration must be done via the static methods on the
    30   * {@code Log4f} class itself.
    31   *
    32   * @author Simon Wacker
    33   * @see org.as2lib.env.log.handler.ZtorLog4fHandler
    34   * @see <a href="http://www.ztor.com/index.php4?ln=&g=comp&d=log4f">ZTOR Log4f</a>
    35   */
    36  class org.as2lib.env.log.logger.ZtorLog4fLogger extends BasicClass implements Logger {
    37  	
    38  	/** All level. */
    39  	public static var ALL:Number = -1;
    40  	
    41  	/** ZTOR Log4f debug level. */
    42  	public static var DEBUG:Number = Log4f.DEBUG;
    43  	
    44  	/** ZTOR Log4f info level. */
    45  	public static var INFO:Number = Log4f.INFO;
    46  	
    47  	/** ZTOR Log4f warn level. */
    48  	public static var WARN:Number = Log4f.WARN;
    49  	
    50  	/** ZTOR Log4f error level. */
    51  	public static var ERROR:Number = Log4f.ERROR;
    52  	
    53  	/** ZTOR Log4f fatal level. */
    54  	public static var FATAL:Number = Log4f.FATAL;
    55  	
    56  	/** ZTOR Log4f log4f level. */
    57  	public static var LOG4F:Number = Log4f.LOG4F;
    58  	
    59  	/** None level. */
    60  	public static var NONE:Number = 6;
    61  	
    62  	/** The current log level. */
    63  	private var level:Number;
    64  	
    65  	/** ZTOR Log4f debug level. */
    66  	private var debugLevel:Number;
    67  	
    68  	/** ZTOR Log4f info level. */
    69  	private var infoLevel:Number;
    70  	
    71  	/** ZTOR Log4f warn level. */
    72  	private var warnLevel:Number;
    73  	
    74  	/** ZTOR Log4f error level. */
    75  	private var errorLevel:Number;
    76  	
    77  	/** ZTOR Log4f fatal level. */
    78  	private var fatalLevel:Number;
    79  	
    80  	/**
    81  	 * Constructs a new {@code ZtorLog4fLogger} instance.
    82  	 * 
    83  	 * <p>The level is by default set to {@link #ALL}. All messages regardless of
    84  	 * their type are logged then.
    85  	 * 
    86  	 * @param level (optional) the level of this logger
    87  	 */
    88  	public function ZtorLog4fLogger(level:Number) {
    89  		setLevel(level);
    90  		this.debugLevel = DEBUG;
    91  		this.infoLevel = INFO;
    92  		this.warnLevel = WARN;
    93  		this.errorLevel = ERROR;
    94  		this.fatalLevel = FATAL;
    95  	}
    96  	
    97  	/**
    98  	 * Sets the log level.
    99  	 *
   100  	 * <p>The log level determines which messages are logged and which are not.
   101  	 *
   102  	 * <p>A level of value {@code null} or {@code undefined} is interpreted as level
   103  	 * {@code ALL} which is also the default level.
   104  	 * 
   105  	 * @param level the new log level
   106  	 */
   107  	public function setLevel(level:Number):Void {
   108  		if (level == null) {
   109  			this.level = ALL;
   110  		} else {
   111  			this.level = level;
   112  		}
   113  	}
   114  	
   115  	/**
   116  	 * Returns the set level.
   117  	 *
   118  	 * @return the set level
   119  	 */
   120  	public function getLevel(Void):Number {
   121  		return this.level;
   122  	}
   123  	
   124  	/**
   125  	 * Checks if this logger is enabled for debug level messages.
   126  	 *
   127  	 * @return {@code true} if debug messages are logged
   128  	 * @see #debug
   129  	 */
   130  	public function isDebugEnabled(Void):Boolean {
   131  		return (this.level <= this.debugLevel);
   132  	}
   133  	
   134  	/**
   135  	 * Checks if this logger is enabled for info level messages.
   136  	 *
   137  	 * @return {@code true} if info messages are logged
   138  	 * @see #info
   139  	 */
   140  	public function isInfoEnabled(Void):Boolean {
   141  		return (this.level <= this.infoLevel);
   142  	}
   143  	
   144  	/**
   145  	 * Checks if this logger is enabled for warning level messages.
   146  	 *
   147  	 * @return {@code true} if warning messages are logged
   148  	 * @see #warning
   149  	 */
   150  	public function isWarningEnabled(Void):Boolean {
   151  		return (this.level <= this.warnLevel);
   152  	}
   153  	
   154  	/**
   155  	 * Checks if this logger is enabled for error level messages.
   156  	 *
   157  	 * @return {@code true} if error messages are logged
   158  	 * @see #error
   159  	 */
   160  	public function isErrorEnabled(Void):Boolean {
   161  		return (this.level <= this.errorLevel);
   162  	}
   163  	
   164  	/**
   165  	 * Checks if this logger is enabled for fatal level messages.
   166  	 *
   167  	 * @return {@code true} if fatal messages are logged
   168  	 * @see #fatal
   169  	 */
   170  	public function isFatalEnabled(Void):Boolean {
   171  		return (this.level <= this.fatalLevel);
   172  	}
   173  	
   174  	/**
   175  	 * Logs the passed-in {@code message} at debug level.
   176  	 *
   177  	 * <p>The {@code message} is only logged when the level is set to debug or a level
   178  	 * above.
   179  	 *
   180  	 * <p>The {@code message} is logged using the {@code Log4f.log} method, passing
   181  	 * the debug level number and the message as header.
   182  	 *
   183  	 * @param message the message object to log
   184  	 * @see #isDebugEnabled
   185  	 */
   186  	public function debug(message):Void {
   187  		if (isDebugEnabled()) {
   188  			Log4f.log(this.debugLevel, message, "");
   189  		}
   190  	}
   191  	
   192  	/**
   193  	 * Logs the passed-in {@code message} at info level.
   194  	 *
   195  	 * <p>The {@code message} is only logged when the level is set to info or a level
   196  	 * above.
   197  	 *
   198  	 * <p>The {@code message} is logged using the {@code Log4f.log} method, passing
   199  	 * the info level number and the message as header.
   200  	 *
   201  	 * @param message the message object to log
   202  	 * @see #isInfoEnabled
   203  	 */
   204  	public function info(message):Void {
   205  		if (isInfoEnabled()) {
   206  			Log4f.log(this.infoLevel, message, "");
   207  		}
   208  	}
   209  	
   210  	/**
   211  	 * Logs the passed-in {@code message} at warning level.
   212  	 *
   213  	 * <p>The {@code message} is only logged when the level is set to warning or a
   214  	 * level above.
   215  	 *
   216  	 * <p>The {@code message} is logged using the {@code Log4f.log} method, passing
   217  	 * the warn level number and the message as header.
   218  	 *
   219  	 * @param message the message object to log
   220  	 * @see #isWarningEnabled
   221  	 */
   222  	public function warning(message):Void {
   223  		if (isWarningEnabled()) {
   224  			Log4f.log(this.warnLevel, message, "");
   225  		}
   226  	}
   227  	
   228  	/**
   229  	 * Logs the passed-in {@code message} at error level.
   230  	 *
   231  	 * <p>The {@code message} is only logged when the level is set to error or a level
   232  	 * above.
   233  	 *
   234  	 * <p>The {@code message} is logged using the {@code Log4f.log} method, passing
   235  	 * the error level number and the message as header.
   236  	 *
   237  	 * @param message the message object to log
   238  	 * @see #isErrorEnabled
   239  	 */
   240  	public function error(message):Void {
   241  		if (isErrorEnabled()) {
   242  			Log4f.log(this.errorLevel, message, "");
   243  		}
   244  	}
   245  	
   246  	/**
   247  	 * Logs the passed-in {@code message} at fatal level.
   248  	 *
   249  	 * <p>The {@code message} is only logged when the level is set to fatal or a level
   250  	 * above.
   251  	 *
   252  	 * <p>The {@code message} is logged using the {@code Log4f.log} method, passing
   253  	 * the fatal level number and the message as header.
   254  	 *
   255  	 * @param message the message object to log
   256  	 * @see #isFatalEnabled
   257  	 */
   258  	public function fatal(message):Void {
   259  		if (isFatalEnabled()) {
   260  			Log4f.log(this.fatalLevel, message, "");
   261  		}
   262  	}
   263  	
   264  }