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.util.Stringifier; 19 import org.as2lib.util.DateFormatter; 20 import org.as2lib.env.log.LogMessage; 21 import org.as2lib.env.log.LogLevel; 22 23 /** 24 * {@code PatternLogMessageStringifier} stringifies {@link LogMessage} instances 25 * based on configuration and a pattern. 26 * 27 * @author Simon Wacker 28 */ 29 class org.as2lib.env.log.stringifier.PatternLogMessageStringifier extends BasicClass implements Stringifier { 30 31 /** Determines whether to show the level in the string representation. */ 32 private var showLevel:Boolean; 33 34 /** Determines whether to show the name of the logger in the string representation. */ 35 private var showLoggerName:Boolean; 36 37 /** The time formatter to format the time stamp if desired. */ 38 private var timeFormatter:DateFormatter; 39 40 /** 41 * Constructs a new {@code PatternLogMessageStringifier} instance. 42 * 43 * <p>Level and logger name are shown by default. 44 * 45 * <p>If {@code timeFormat} is not passed-in or is {@code null} the date-time is not 46 * shown in the log message. 47 * 48 * @param showLevel determines whether to show levels in the string representation 49 * @param shoLoggerName determines whether to show the logger name in the string 50 * representation 51 * @param timeFormat (optional) the time format pattern used to format the time 52 * stamp 53 * @see org.as2lib.util.DateFormatter 54 */ 55 public function PatternLogMessageStringifier(showLevel:Boolean, showLoggerName:Boolean, timeFormat:String) { 56 this.showLevel = showLevel == null ? true : showLevel; 57 this.showLoggerName = showLoggerName == null ? true : showLoggerName; 58 if (timeFormat != null) this.timeFormatter = new DateFormatter(timeFormat); 59 } 60 61 /** 62 * Returns the string representation of the passed-in {@link LogMessage} instance. 63 * 64 * <p>The returned string is composed as follows: 65 * <pre> 66 * theTime theLogLevel theLoggerName - theMessage 67 * </pre> 68 * 69 * <p>Depending on your custom settings, which information to show and which not, a 70 * few parts may be left out. 71 * 72 * @param target the {@code LogMessage} instance to stringify 73 * @return the string representation of the passed-in {@code target} 74 */ 75 public function execute(target):String { 76 var message:LogMessage = target; 77 var info:String = ""; 78 if (timeFormatter) { 79 var timeStamp:Number = message.getTimeStamp(); 80 if (timeStamp != null) { 81 info += timeFormatter.format(new Date(timeStamp)) + " "; 82 } 83 } 84 if (showLevel) { 85 var level:LogLevel = message.getLevel(); 86 if (level) { 87 info += level.toString() + " "; 88 } 89 } 90 if (showLoggerName) { 91 var name:String = message.getLoggerName(); 92 if (name != null) { 93 info += name + " - "; 94 } 95 } 96 return (info + message.getMessage()); 97 } 98 99 }