KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > logger > LoggerMananger


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6 package org.jfox.ioc.logger;
7
8 import java.io.IOException JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.apache.log4j.Appender;
16 import org.apache.log4j.ConsoleAppender;
17 import org.apache.log4j.DailyRollingFileAppender;
18 import org.apache.log4j.FileAppender;
19 import org.apache.log4j.Layout;
20 import org.apache.log4j.Level;
21 import org.apache.log4j.LogManager;
22 import org.apache.log4j.PatternLayout;
23 import org.apache.log4j.PropertyConfigurator;
24 import org.apache.log4j.RollingFileAppender;
25
26 /**
27  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
28  */

29
30 public class LoggerMananger {
31     private static org.apache.log4j.Logger rootLogger = LogManager.getRootLogger();
32     private static final String JavaDoc configFile = "log4j.properties";
33
34     static {
35         _init();
36     }
37
38     static void _init() {
39         URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(configFile);
40         if(url != null) {
41             PropertyConfigurator.configureAndWatch(configFile, 1000);
42         }
43         else {
44             Appender app = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
45             app.setName("console");
46             rootLogger.addAppender(app);
47         }
48 // rootLogger.setLevel(Level.INFO);
49
rootLogger.info(LoggerMananger.class.getName() + " initialized.");
50     }
51
52     public void mx_reset() {
53         org.apache.log4j.LogManager.resetConfiguration();
54         _init();
55     }
56
57     public void mx_setLevel(String JavaDoc loggerName, String JavaDoc level) {
58         org.apache.log4j.Logger logger = getLogger(loggerName);
59         logger.setLevel(Level.toLevel(level));
60     }
61
62     public String JavaDoc mx_getLevel(String JavaDoc loggerName) {
63         org.apache.log4j.Logger logger = getLogger(loggerName);
64
65         Level level = logger.getLevel();
66         if(level == null) {
67             return null;
68         }
69         else {
70             return level.toString();
71         }
72     }
73
74     public void mx_setPattern(String JavaDoc loggerName, String JavaDoc appenderName, String JavaDoc pattern) {
75         org.apache.log4j.Logger logger = getLogger(loggerName);
76         Appender appender = logger.getAppender(appenderName);
77         if(appender != null) {
78             appender.setLayout(new PatternLayout(pattern));
79         }
80     }
81
82     public String JavaDoc mx_getPattern(String JavaDoc loggerName, String JavaDoc appenderName) {
83         org.apache.log4j.Logger logger = getLogger(loggerName);
84         Appender appender = logger.getAppender(appenderName);
85         if(appender != null) {
86             Layout layout = appender.getLayout();
87             if((layout != null) && (layout instanceof PatternLayout)) {
88                 return ((PatternLayout) layout).getConversionPattern();
89             }
90         }
91         return "";
92     }
93
94     public void mx_addFileAppender(String JavaDoc loggerName, String JavaDoc appenderName, String JavaDoc pattern, String JavaDoc filename) throws IOException JavaDoc {
95         org.apache.log4j.Logger logger = getLogger(loggerName);
96         FileAppender fa = new FileAppender(new PatternLayout(pattern), filename);
97 // fa.setBufferedIO(true);
98
fa.setName(appenderName);
99         logger.addAppender(fa);
100     }
101
102     public void mx_addRollingFileAppender(String JavaDoc loggerName, String JavaDoc appenderName, String JavaDoc pattern, String JavaDoc filename, String JavaDoc maxFileSize) throws IOException JavaDoc {
103         org.apache.log4j.Logger logger = getLogger(loggerName);
104         RollingFileAppender rfa = new RollingFileAppender(new PatternLayout(pattern), filename);
105 // fa.setBufferedIO(true);
106
rfa.setName(appenderName);
107         rfa.setMaxFileSize(maxFileSize);
108         logger.addAppender(rfa);
109     }
110
111     public void mx_addDailyRollingFileAppender(String JavaDoc loggerName, String JavaDoc appenderName, String JavaDoc pattern, String JavaDoc filename) throws IOException JavaDoc {
112         org.apache.log4j.Logger logger = getLogger(loggerName);
113         DailyRollingFileAppender drfa = new DailyRollingFileAppender(new PatternLayout(pattern), filename, "'.'yyyy-MM");
114         drfa.setName(appenderName);
115         logger.addAppender(drfa);
116     }
117
118
119     public void mx_removeAppender(String JavaDoc loggerName, String JavaDoc appenderName) {
120         org.apache.log4j.Logger logger = getLogger(loggerName);
121         logger.removeAppender(appenderName);
122     }
123
124
125     public List JavaDoc mx_getAllApenders(String JavaDoc loggerName) {
126         org.apache.log4j.Logger logger = getLogger(loggerName);
127         Enumeration JavaDoc enu = logger.getAllAppenders();
128         List JavaDoc appenders = new ArrayList JavaDoc();
129         while(enu.hasMoreElements()) {
130             appenders.add(enu.nextElement());
131         }
132         return appenders;
133     }
134
135     public Properties JavaDoc getOperationDescriptions() {
136         Properties JavaDoc prop = new Properties JavaDoc();
137         prop.setProperty("mx_getPattern", " mx_getPattern(String loggerName,String appenderName), parameter example: root, console");
138         prop.setProperty("mx_getLevel", "get level by Logger name, exsample: root");
139         prop.setProperty("mx_setLevel", "mx_setLevel(String loggerName, String level)");
140         prop.setProperty("mx_addFileAppender", "mx_addFileAppender(String loggerName,String appenderName, String pattern, String filename)");
141         prop.setProperty("mx_addRollingFileAppender", "mx_addRollingFileAppender(String loggerName,String appenderName, String pattern, String filename,String maxFileSize)");
142         prop.setProperty("mx_addDailyRollingFileAppender", "mx_addDailyRollingFileAppender(String loggerName,String appenderName, String pattern, String filename)");
143         prop.setProperty("mx_removeAppender", "mx_removeAppender(String loggerName, String appenderName)");
144         prop.setProperty("mx_getAllApenders", "mx_getAllApenders(String loggerName)");
145         prop.setProperty("mx_setPattern", "mx_setPattern(String loggerName, String appenderName, String pattern)");
146         return prop;
147     }
148
149     public Object JavaDoc getSource() {
150         return this;
151     }
152
153     public String JavaDoc getMxDescription() {
154         return "Logger Manager";
155     }
156
157     private org.apache.log4j.Logger getLogger(String JavaDoc name) {
158         org.apache.log4j.Logger logger;
159         if(name.equals("root")) {
160             logger = rootLogger;
161         }
162         else {
163             logger = org.apache.log4j.Logger.getLogger(name);
164         }
165         return logger;
166     }
167
168 }
169
170
Popular Tags