Changeset 676 for trunk/quest-runner/src/main/java/de
- Timestamp:
- 08/28/12 16:56:29 (12 years ago)
- Location:
- trunk/quest-runner/src/main/java/de/ugoe/cs/quest
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-runner/src/main/java/de/ugoe/cs/quest/log4j/Log4JLogger.java
r633 r676 1 1 2 package de.ugoe.cs.quest.log4j; 2 3 … … 20 21 * @version 1.0 21 22 */ 22 public class Log4JLogger implements IErrorListener, ITraceListener, 23 IExceptionListener, ICommandListener { 23 public class Log4JLogger implements IErrorListener, ITraceListener, IExceptionListener, 24 ICommandListener 25 { 24 26 25 26 27 28 29 30 27 /** 28 * <p> 29 * Reference to the logger. 30 * </p> 31 */ 32 Logger logger; 31 33 32 /** 33 * <p> 34 * Constructor. Creates a new Log4JLogger and registers the implemented 35 * listener with the {@link Console}. 36 * </p> 37 */ 38 public Log4JLogger() { 39 PropertyConfigurator.configure("data/log4j.properties"); 40 logger = Logger.getLogger("de.ugoe.cs.quest"); 41 Console.getInstance().registerErrorListener(this); 42 Console.getInstance().registerTraceListener(this); 43 Console.getInstance().registerExceptionListener(this); 44 Console.getInstance().registerCommandListener(this); 45 } 34 /** 35 * <p> 36 * This is the trace level according to the Java logger API. We use this instead of the log4j 37 * levels themselves for the logging. 38 * </p> 39 */ 40 Level traceLevel; 46 41 47 /* 48 * (non-Javadoc) 49 * 50 * @see 51 * de.ugoe.cs.util.console.listener.ICommandListener#commandNotification 52 * (java.lang.String) 53 */ 54 @Override 55 public void commandNotification(String command) { 56 logger.info("Command executed: " + command); 57 } 42 /** 43 * <p> 44 * Constructor. Creates a new Log4JLogger and registers the implemented listener with the 45 * {@link Console}. 46 * </p> 47 * @param traceLevel tracing level 48 */ 49 public Log4JLogger(Level traceLevel) { 50 PropertyConfigurator.configure("data/log4j.properties"); 51 logger = Logger.getLogger("de.ugoe.cs.quest"); 52 logger.setLevel(convertToLog4JLevel(traceLevel)); 53 this.traceLevel = traceLevel; 54 Console.getInstance().registerErrorListener(this); 55 Console.getInstance().registerTraceListener(this); 56 Console.getInstance().registerExceptionListener(this); 57 Console.getInstance().registerCommandListener(this); 58 } 58 59 59 /* 60 * (non-Javadoc) 61 * 62 * @see 63 * de.ugoe.cs.util.console.listener.IExceptionListener#printStacktrace(java 64 * .lang.Exception) 65 */ 66 @Override 67 public void logException(Exception e) { 68 logger.error("", e); 69 } 60 /* 61 * (non-Javadoc) 62 * 63 * @see de.ugoe.cs.util.console.listener.ICommandListener#commandNotification (java.lang.String) 64 */ 65 @Override 66 public void commandNotification(String command) { 67 logger.info("Command executed: " + command); 68 } 70 69 71 /* 72 * (non-Javadoc) 73 * 74 * @see 75 * de.ugoe.cs.util.console.listener.ITraceListener#traceMsg(java.lang.String 76 * ) 77 */ 78 @Override 79 public void traceMsg(String traceMessage, Level level) { 80 logger.log(convertToLog4JLevel(level), traceMessage); 81 } 70 /* 71 * (non-Javadoc) 72 * 73 * @see de.ugoe.cs.util.console.listener.IExceptionListener#printStacktrace(java 74 * .lang.Exception) 75 */ 76 @Override 77 public void logException(Exception e) { 78 logger.error("", e); 79 } 82 80 83 /* 84 * (non-Javadoc) 85 * 86 * @see 87 * de.ugoe.cs.util.console.listener.IErrorListener#errorMsg(java.lang.String 88 * ) 89 */ 90 @Override 91 public void errorMsg(String errMessage) { 92 logger.error(errMessage); 93 } 94 95 /** 96 * <p> 97 * Converts the log level described by {@link Level} into a {@link org.apache.log4j.Level}. 98 * </p> 99 * 100 * @param level java.util.logger.Level severity 101 * @return org.apache.log4j.Level severity 102 */ 103 private org.apache.log4j.Level convertToLog4JLevel(Level level) { 104 if(level==Level.OFF) { 105 return org.apache.log4j.Level.OFF; 106 } 107 if(level==Level.SEVERE) { 108 return org.apache.log4j.Level.FATAL; 109 } 110 if(level==Level.WARNING) { 111 return org.apache.log4j.Level.WARN; 112 } 113 if(level==Level.INFO || level==Level.CONFIG) { 114 return org.apache.log4j.Level.INFO; 115 } 116 // remaining levels: FINE, FINER, FINEST, ALL 117 return org.apache.log4j.Level.ALL; 118 } 81 /* 82 * (non-Javadoc) 83 * 84 * @see de.ugoe.cs.util.console.listener.ITraceListener#traceMsg(java.lang.String ) 85 */ 86 @Override 87 public void traceMsg(String traceMessage, Level level) { 88 if( level.intValue()>=traceLevel.intValue()) { 89 logger.log(convertToLog4JLevel(level), traceMessage); 90 } 91 } 92 93 /* 94 * (non-Javadoc) 95 * 96 * @see de.ugoe.cs.util.console.listener.IErrorListener#errorMsg(java.lang.String ) 97 */ 98 @Override 99 public void errorMsg(String errMessage) { 100 logger.error(errMessage); 101 } 102 103 /** 104 * <p> 105 * Converts the log level described by {@link Level} into a {@link org.apache.log4j.Level}. 106 * </p> 107 * 108 * @param level 109 * java.util.logger.Level severity 110 * @return org.apache.log4j.Level severity 111 */ 112 private org.apache.log4j.Level convertToLog4JLevel(Level level) { 113 if (level == Level.OFF) { 114 return org.apache.log4j.Level.OFF; 115 } 116 if (level == Level.SEVERE) { 117 return org.apache.log4j.Level.FATAL; 118 } 119 if (level == Level.WARNING) { 120 return org.apache.log4j.Level.WARN; 121 } 122 if (level == Level.INFO || level == Level.CONFIG) { 123 return org.apache.log4j.Level.INFO; 124 } 125 // remaining levels: FINE, FINER, FINEST, ALL 126 return org.apache.log4j.Level.ALL; 127 } 119 128 } -
trunk/quest-runner/src/main/java/de/ugoe/cs/quest/ui/Runner.java
r674 r676 36 36 }; 37 37 38 public enum LOG4JTYPE {39 enable, disable40 }41 42 38 /** 43 39 * <p> … … 65 61 66 62 OptionParser parser = new OptionParser(); 67 OptionSpec<L OG4JTYPE> log4j =68 parser.accepts("log4j", "Allowed values: enable, disable").withRequiredArg()69 .ofType(L OG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);63 OptionSpec<Level> log4j = 64 parser.accepts("log4j", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg() 65 .ofType(Level.class).defaultsTo(Level.INFO); 70 66 OptionSpec<UITYPE> ui = 71 67 parser.accepts("ui", "Allowed values: text, swt").withRequiredArg() … … 78 74 List<String> startupCommands = options.nonOptionArguments(); 79 75 try { 80 switch (options.valueOf(log4j)) 81 { 82 case enable: 83 new Log4JLogger(); 84 break; 85 case disable: 86 // do nothing 87 break; 88 default: 89 throw new AssertionError("reached source code that should be unreachable"); 76 if(options.valueOf(log4j)!=Level.OFF) { 77 new Log4JLogger(options.valueOf(log4j)); 90 78 } 91 79
Note: See TracChangeset
for help on using the changeset viewer.