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.LogHandler; 19 import org.as2lib.env.log.LogMessage; 20 import org.as2lib.env.log.LogLevel; 21 import org.as2lib.env.log.level.AbstractLogLevel; 22 23 import ascb.util.logging.LogManager; 24 import ascb.util.logging.Level; 25 26 /** 27 * {@code AscbHandler} delegates the log message to the {@code LogManager.log} 28 * method of the ASCB Logging API. 29 * 30 * @author Simon Wacker 31 * @see org.as2lib.env.log.logger.AscbLogger 32 * @see <a href="http://www.person13.com/ascblibrary">ASCB Library</a> 33 */ 34 class org.as2lib.env.log.handler.AscbHandler extends BasicClass implements LogHandler { 35 36 /** Holds a ascb handler. */ 37 private static var ascbHandler:AscbHandler; 38 39 /** 40 * Returns an instance of this class. 41 * 42 * <p>This method always returns the same instance. 43 * 44 * @return a ascb handler 45 */ 46 public static function getInstance(Void):AscbHandler { 47 if (!ascbHandler) ascbHandler = new AscbHandler(); 48 return ascbHandler; 49 } 50 51 /** The ASCB {@code LogManager} log messages are delegated to. */ 52 private var manager:LogManager; 53 54 /** 55 * Constructs a new {@code AscbHandler} instance. 56 * 57 * <p>You can use one and the same instance for multiple loggers. So think about 58 * using the handler returned by the static {@link #getInstance} method. Using this 59 * instance prevents the instantiation of unnecessary ascb handlers and saves 60 * storage. 61 */ 62 public function AscbHandler(Void) { 63 this.manager = LogManager.getLogManager(); 64 } 65 66 /** 67 * Converts the passed-in {@code message} to a format that is expected formatters 68 * of the ASCB Logging API and passes the converted message to the 69 * {@code LogManager.log} method of the ASCB Logging API. 70 * 71 * <p>The converted object has the following variables: 72 * <dl> 73 * <dt>level</dt> 74 * <dd> 75 * ASCB Logging API level number of the message. The former As2lib 76 * {@code LogLevel} returned by {@code LogMessage.getLevel} was converted to 77 * this level number by the {@link #convertLevel} method. 78 * </dd> 79 * <dt>message</dt> 80 * <dd> 81 * The actual message to log. That is the message returned by the 82 * {@code LogMessage.getMessage} method. 83 * </dd> 84 * <dt>name</dt> 85 * <dd> 86 * The name of the logger returned by the {@code LogMessage.getLoggerName} 87 * method. 88 * </dd> 89 * <dt>time</dt> 90 * <dd> 91 * The time when the logging took place. This is a {@code Date} instance 92 * configured with the timestamp returned by the method 93 * {@code LogMessage.getTimeStamp}. 94 * </dd> 95 * <dt>logMessage</dt> 96 * <dd> 97 * The passed-in {@code message} instance. This variable may be used by your 98 * own formatter if you want to take advantage of the stringifier of the 99 * {@code LogMessage}. 100 * </dd> 101 * </dl> 102 * 103 * @param message the message to log 104 */ 105 public function write(message:LogMessage):Void { 106 var logRecord = new Object(); 107 logRecord.logMessage = message; 108 logRecord.level = convertLevel(message.getLevel()); 109 logRecord.message = message.getMessage(); 110 logRecord.name = message.getLoggerName(); 111 logRecord.time = message.getTimeStamp(); 112 manager.log(logRecord); 113 } 114 115 /** 116 * Converts the As2lib {@code LogLevel} into a ASCB level. 117 * 118 * <dl> 119 * <dt>ALL</dt> 120 * <dd>Is converted to {@code Level.ALL}.</dd> 121 * <dt>DEBUG</dt> 122 * <dd>Is converted to {@code Level.DEBUG}.</dd> 123 * <dt>INFO</dt> 124 * <dd>Is converted to {@code Level.INFO}.</dd> 125 * <dt>WARNING</dt> 126 * <dd>Is converted to {@code Level.WARNING}.</dd> 127 * <dt>ERROR</dt> 128 * <dd>Is converted to {@code Level.SEVERE}.</dd> 129 * <dt>FATAL</dt> 130 * <dd>Is converted to {@code Level.ALL}.</dd> 131 * <dt>NONE</dt> 132 * <dd>Is converted to {@code Level.OFF}.</dd> 133 * </dl> 134 * 135 * <p>If the passed-in {@code level} is none of the above, {@code null} is returned. 136 * 137 * @param level the As2lib log level to convert 138 * @return the equivalent ASCB level number or {@code null} 139 */ 140 private function convertLevel(level:LogLevel):Number { 141 switch (level) { 142 case AbstractLogLevel.ALL: 143 return Level.ALL; 144 case AbstractLogLevel.DEBUG: 145 return Level.DEBUG; 146 case AbstractLogLevel.INFO: 147 return Level.INFO; 148 case AbstractLogLevel.WARNING: 149 return Level.WARNING; 150 case AbstractLogLevel.ERROR: 151 return Level.SEVERE; 152 case AbstractLogLevel.FATAL: 153 return Level.ALL; 154 case AbstractLogLevel.NONE: 155 return Level.OFF; 156 default: 157 return null; 158 } 159 } 160 161 }