1 4 package org.oddjob.logging; 5 6 import org.apache.log4j.Appender; 7 import org.apache.log4j.Level; 8 import org.apache.log4j.Logger; 9 import org.apache.log4j.PatternLayout; 10 import org.apache.log4j.Priority; 11 12 17 public class Log4jArchiver implements LogArchiver { 18 19 20 public static final String MDC = "ojmdc"; 21 22 private final LogArchiverCache logArchiver; 23 24 private final Appender appender; 25 26 public Log4jArchiver(Object root, String pattern) { 27 logArchiver = new LogArchiverCache(root, 28 new LogArchiverCache.Resolver() { 29 public String loggerFor(Object component) { 30 return LogHelper.getLogger(component); 31 } 32 }); 33 appender = new ArchiveAppender(logArchiver, new PatternLayout(pattern)); 34 appender.setName(this.toString()); 35 Logger.getRootLogger().addAppender(appender); 36 } 37 38 public boolean hasArchive(String archive) { 39 return logArchiver.hasArchive(archive); 40 } 41 44 public long getLastMessageNumber(String archive) { 45 return logArchiver.getLastMessageNumber(archive); 46 } 47 48 51 public int getMaxHistory() { 52 return logArchiver.getMaxHistory(); 53 } 54 55 public void addEvent(String archive, LogLevel level, String message) { 56 logArchiver.addEvent(archive, level, message); 57 } 58 59 public void addLogListener(LogListener l, Object component, 60 LogLevel level, long last, int history) { 61 logArchiver.addLogListener(l, LogHelper.getLogger(component), 62 level, last, history); 63 } 64 65 public void removeLogListener(LogListener l, Object component) { 66 logArchiver.removeLogListener(l, LogHelper.getLogger(component)); 67 } 68 69 public void destroy() { 70 Logger.getRootLogger().removeAppender(appender); 71 logArchiver.destroy(); 72 } 73 74 79 public static LogLevel convertLevel(Level level) { 80 if (level == Level.ALL) { 81 return LogLevel.DEBUG; 82 } 83 else if (level == Level.DEBUG || level == Priority.DEBUG) { 84 return LogLevel.DEBUG; 85 } 86 else if (level == Level.INFO || level == Priority.INFO) { 87 return LogLevel.INFO; 88 } 89 else if (level == Level.WARN || level == Priority.WARN) { 90 return LogLevel.WARN; 91 } 92 else if (level == Level.ERROR || level == Priority.ERROR) { 93 return LogLevel.ERROR; 94 } 95 else if (level == Level.FATAL || level == Priority.FATAL) { 96 return LogLevel.FATAL; 97 } 98 else if (level == Level.OFF) { 99 return LogLevel.FATAL; 100 } 101 else { 102 throw new IllegalArgumentException ("Don't know anything about [" + level + "]"); 103 } 104 } 105 } 106 | Popular Tags |