001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.logging;
009:
010: import java.net.URL;
011: import javax.management.MBeanRegistration;
012: import javax.management.ObjectName;
013: import javax.management.MBeanServer;
014: import org.apache.log4j.Level;
015: import org.apache.log4j.ConsoleAppender;
016: import org.apache.log4j.PatternLayout;
017: import org.apache.log4j.PropertyConfigurator;
018: import org.apache.log4j.Appender;
019: import org.apache.log4j.Layout;
020:
021: /**
022: *
023: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
024: */
025:
026: public class LogManager implements LogManagerMBean, MBeanRegistration {
027: private static org.apache.log4j.Logger rootLogger = org.apache.log4j.LogManager
028: .getRootLogger();
029: private static final String configFile = "log4j.properties";
030:
031: static {
032: _init();
033: }
034:
035: private static void _init() {
036: URL url = Thread.currentThread().getContextClassLoader()
037: .getResource(configFile);
038: if (url != null) {
039: PropertyConfigurator.configureAndWatch(configFile, 1000);
040: } else {
041: rootLogger.addAppender(new ConsoleAppender(
042: new PatternLayout(
043: PatternLayout.TTCC_CONVERSION_PATTERN)));
044: }
045: rootLogger.info(LogManager.class.getName() + " initialized.");
046: }
047:
048: public ObjectName preRegister(MBeanServer mBeanServer,
049: ObjectName objectName) throws Exception {
050: if (objectName == null) {
051: objectName = new ObjectName(LogManagerMBean.OBJECT_NAME);
052: }
053: return objectName;
054: }
055:
056: public void postRegister(Boolean aBoolean) {
057:
058: }
059:
060: public void preDeregister() throws Exception {
061:
062: }
063:
064: public void postDeregister() {
065: org.apache.log4j.LogManager.shutdown();
066: }
067:
068: public void reset() {
069: org.apache.log4j.LogManager.resetConfiguration();
070: _init();
071: }
072:
073: public void setLevel(String level) {
074: rootLogger.setLevel(Level.toLevel(level));
075: }
076:
077: public String getLevel() {
078: return rootLogger.getLevel().toString();
079: }
080:
081: public void setPattern(String appenderName, String pattern) {
082: Appender appender = rootLogger.getAppender(appenderName);
083: if (appender != null) {
084: appender.setLayout(new PatternLayout(pattern));
085: }
086: }
087:
088: public String getPattern(String appenderName) {
089: Appender appender = rootLogger.getAppender(appenderName);
090: if (appender != null) {
091: Layout layout = appender.getLayout();
092: if ((layout != null) && (layout instanceof PatternLayout)) {
093: return ((PatternLayout) layout).getConversionPattern();
094: }
095: }
096: return "";
097:
098: }
099:
100: }
|