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.env.log.Logger;
    18  import org.as2lib.env.log.LogLevel;
    19  import org.as2lib.env.log.logger.AbstractLogger;
    20  
    21  import ascb.util.logging.Level;
    22  
    23  /**
    24   * {@code AscbLogger} acts as a wrapper for a {@code ascb.util.logging.Logger}
    25   * instance of the ASCB Logging API.
    26   * 
    27   * <p>Configure the ASCB Logging API as normally and just use this class in your
    28   * application to log messages. This enables you to switch between almost every
    29   * available Logging API without having to change the logs in your application but
    30   * just the underlying configuration on startup.
    31   *
    32   * <p>All functionalities that the ASCB Logging API offers are delegated to it.
    33   * Other functionalities are performed by this class directly.
    34   *
    35   * <p>The level functionalitiy of loggers is not supported by the ASCB Logging API.
    36   * This is thus provided by this class and not delegated. The ASCB Logging API
    37   * provides only level functionalitiy for handlers. If you want only the handler
    38   * level functionality to be enabled just do not set a level on this logger.
    39   * 
    40   * @author Simon Wacker
    41   * @see org.as2lib.env.log.handler.AscbHandler
    42   * @see <a href="http://www.person13.com/ascblibrary">ASCB Library</a>
    43   */
    44  class org.as2lib.env.log.logger.AscbLogger extends AbstractLogger implements Logger {
    45  	
    46  	/** Makes the static variables of the super-class accessible through this class. */
    47  	private static var __proto__:Function = AbstractLogger;
    48  	
    49  	/** The {@code Logger} instance of ASCB every task is delegated to. */
    50  	private var logger:ascb.util.logging.Logger;
    51  	
    52  	/** The set level. */
    53  	private var level:LogLevel;
    54  	
    55  	/** The set level as number. */
    56  	private var levelAsNumber:Number;
    57  	
    58  	/**
    59  	 * Constructs a new {@code AscbLogger} instance.
    60  	 *
    61  	 * <p>Gets an ASCB {@code Logger} instance via the
    62  	 * {code ascb.util.logging.Logger.getLogger} method.
    63  	 *
    64  	 * @param name the name of this logger
    65  	 */
    66  	public function AscbLogger(name:String) {
    67  		this.logger = ascb.util.logging.Logger.getLogger(name);
    68  		setLevel(null);
    69  	}
    70  	
    71  	/**
    72  	 * Returns the name of this logger.
    73  	 *
    74  	 * @return the name of this logger
    75  	 */
    76  	public function getName(Void):String {
    77  		return this.logger.getName();
    78  	}
    79  	
    80  	/**
    81  	 * Sets the new level.
    82  	 *
    83  	 * <p>If the passed-in {@code newLevel} is {@code null} or {@code undefined} the
    84  	 * default level {@code ALL} is used instead.
    85  	 *
    86  	 * <p>The level determines which messages are logged and which are not.
    87  	 *
    88  	 * @param newLevel the new level
    89  	 */
    90  	public function setLevel(newLevel:LogLevel):Void {
    91  		if (newLevel) {
    92  			this.level = newLevel;
    93  		} else {
    94  			this.level = ALL;
    95  		}
    96  		this.levelAsNumber = this.level.toNumber();
    97  	}
    98  	
    99  	/**
   100  	 * Returns the set or default level.
   101  	 *
   102  	 * @return the set or default level
   103  	 */
   104  	public function getLevel(Void):LogLevel {
   105  		return this.level;
   106  	}
   107  	
   108  	/**
   109  	 * Checks if this logger is enabled for debug level log messages.
   110  	 *
   111  	 * @return {@code true} if debug messages are logged
   112  	 * @see #debug
   113  	 */
   114  	public function isDebugEnabled(Void):Boolean {
   115  		return (levelAsNumber >= debugLevelAsNumber);
   116  	}
   117  	
   118  	/**
   119  	 * Checks if this logger is enabled for info level log messages.
   120  	 *
   121  	 * @return {@code true} if info messages are logged
   122  	 * @see #info
   123  	 */
   124  	public function isInfoEnabled(Void):Boolean {
   125  		return (levelAsNumber >= infoLevelAsNumber);
   126  	}
   127  	
   128  	/**
   129  	 * Checks if this logger is enabled for warning level log messages.
   130  	 *
   131  	 * @return {@code true} if warning messages are logged
   132  	 * @see #warning
   133  	 */
   134  	public function isWarningEnabled(Void):Boolean {
   135  		return (levelAsNumber >= warningLevelAsNumber);
   136  	}
   137  	
   138  	/**
   139  	 * Checks if this logger is enabled for error level log messages.
   140  	 *
   141  	 * @return {@code true} if error messages are logged
   142  	 * @see #error
   143  	 */
   144  	public function isErrorEnabled(Void):Boolean {
   145  		return (levelAsNumber >= errorLevelAsNumber);
   146  	}
   147  	
   148  	/**
   149  	 * Checks if this logger is enabled for fatal level log messages.
   150  	 *
   151  	 * @return {@code true} if fatal messages are logged
   152  	 * @see #fatal
   153  	 */
   154  	public function isFatalEnabled(Void):Boolean {
   155  		return (levelAsNumber >= fatalLevelAsNumber);
   156  	}
   157  	
   158  	/**
   159  	 * Logs the message object to ASCB {@code Logger} at debug level.
   160  	 *
   161  	 * @param message the message object to log
   162  	 * @see #isDebugEnabled
   163  	 */
   164  	public function debug(message):Void {
   165  		if (isDebugEnabled()) {
   166  			logger.debug(message);
   167  		}
   168  	}
   169  	
   170  	/**
   171  	 * Logs the message object to ASCB {@code Logger} at info level.
   172  	 *
   173  	 * @param message the message object to log
   174  	 * @see #isInfoEnabled
   175  	 */
   176  	public function info(message):Void {
   177  		if (isInfoEnabled()) {
   178  			logger.info(message);
   179  		}
   180  	}
   181  	
   182  	/**
   183  	 * Logs the message object to ASCB {@code Logger} at warning level.
   184  	 *
   185  	 * @param message the message object to log
   186  	 * @see #isWarningEnabled
   187  	 */
   188  	public function warning(message):Void {
   189  		if (isWarningEnabled()) {
   190  			logger.warning(message);
   191  		}
   192  	}
   193  	
   194  	/**
   195  	 * Logs the message object to ASCB {@code Logger} at severe level.
   196  	 *
   197  	 * @param message the message object to log
   198  	 * @see #isErrorEnabled
   199  	 */
   200  	public function error(message):Void {
   201  		if (isErrorEnabled()) {
   202  			logger.severe(message);
   203  		}
   204  	}
   205  	
   206  	/**
   207  	 * Logs the message object to ASCB {@code Logger} at all level.
   208  	 *
   209  	 * @param message the message object to log
   210  	 * @see #isFatalEnabled
   211  	 */
   212  	public function fatal(message):Void {
   213  		if (isFatalEnabled()) {
   214  			logger.log(Level.ALL, message);
   215  		}
   216  	}
   217  	
   218  }