KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > logging > Log4jArchiver


1 /*
2  * (c) Rob Gordon 2005
3  */

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 /**
13  * A LogArchiver which archives using Log4j. This Archiver works by
14  * adding a Log4j appender to all Components in the hierarchy using their
15  * logger property.
16  */

17 public class Log4jArchiver implements LogArchiver {
18
19     /** The MDC to use */
20     public static final String JavaDoc MDC = "ojmdc";
21     
22     private final LogArchiverCache logArchiver;
23
24     private final Appender appender;
25     
26     public Log4jArchiver(Object JavaDoc root, String JavaDoc pattern) {
27         logArchiver = new LogArchiverCache(root,
28                 new LogArchiverCache.Resolver() {
29                     public String JavaDoc loggerFor(Object JavaDoc 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 JavaDoc archive) {
39         return logArchiver.hasArchive(archive);
40     }
41     /* (non-Javadoc)
42      * @see org.oddjob.logging.FunctionalLogArchiver#getLastMessageNumber(java.lang.String)
43      */

44     public long getLastMessageNumber(String JavaDoc archive) {
45         return logArchiver.getLastMessageNumber(archive);
46     }
47     
48     /* (non-Javadoc)
49      * @see org.oddjob.logging.FunctionalLogArchiver#getMaxHistory()
50      */

51     public int getMaxHistory() {
52         return logArchiver.getMaxHistory();
53     }
54     
55     public void addEvent(String JavaDoc archive, LogLevel level, String JavaDoc message) {
56         logArchiver.addEvent(archive, level, message);
57     }
58     
59     public void addLogListener(LogListener l, Object JavaDoc 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 JavaDoc 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     /**
75      * Utility function to convert Log4j log level.
76      * @param level The log4j level.
77      * @return The Oddjob level.
78      */

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 JavaDoc("Don't know anything about [" + level + "]");
103         }
104     }
105 }
106
Popular Tags