[jboss-svn-commits] JBoss Common SVN: r3033 - jboss-logbridge/trunk/src/main/java/org/jboss/logbridge.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Mar 7 21:52:12 EST 2009
Author: david.lloyd at jboss.com
Date: 2009-03-07 21:52:12 -0500 (Sat, 07 Mar 2009)
New Revision: 3033
Added:
jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogNotificationListener.java
Removed:
jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeHierarchy.java
jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeLogger.java
jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeRootLogger.java
Modified:
jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogBridgeHandler.java
Log:
Undo the idea of hooking into log4j due to disappearing log message problem; instead, use a JMX notification listener to learn when the log4j config changes, and rescan and remap all levels from log4j loggers to JDK loggers at that time
Deleted: jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeHierarchy.java
===================================================================
--- jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeHierarchy.java 2009-03-07 11:19:43 UTC (rev 3032)
+++ jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeHierarchy.java 2009-03-08 02:52:12 UTC (rev 3033)
@@ -1,61 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logbridge;
-
-import org.apache.log4j.Hierarchy;
-import org.apache.log4j.Logger;
-import org.apache.log4j.spi.LoggerFactory;
-
-public final class BridgeHierarchy extends Hierarchy {
-
- private final LogBridgeHandler logBridgeHandler;
- private final Factory factory = new Factory();
-
- public BridgeHierarchy(Logger logger, final LogBridgeHandler handler) {
- super(logger);
- logBridgeHandler = handler;
- }
-
- public Logger getRootLogger() {
- return super.getRootLogger();
- }
-
- public Logger getLogger(final String s, final LoggerFactory factory) {
- return super.getLogger(s, factory);
- }
-
- public Logger getLogger(final String s) {
- return super.getLogger(s, factory);
- }
-
- LogBridgeHandler getLogBridgeHandler() {
- return logBridgeHandler;
- }
-
- private class Factory implements LoggerFactory {
-
- public Logger makeNewLoggerInstance(final String s) {
- return new BridgeLogger(s);
- }
- }
-}
Deleted: jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeLogger.java
===================================================================
--- jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeLogger.java 2009-03-07 11:19:43 UTC (rev 3032)
+++ jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeLogger.java 2009-03-08 02:52:12 UTC (rev 3033)
@@ -1,69 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logbridge;
-
-import org.apache.log4j.Logger;
-import org.apache.log4j.Level;
-import org.apache.log4j.Priority;
-
-public class BridgeLogger extends Logger {
-
- protected BridgeLogger(String s) {
- super(s);
- }
-
- @SuppressWarnings({ "NonConstantLogger" })
- private java.util.logging.Logger jdkLogger;
-
- public void setLevel(final Level level) {
- super.setLevel(level);
- syncLevels();
- }
-
- @SuppressWarnings({ "deprecation" })
- @Deprecated
- public void setPriority(final Priority priority) {
- super.setPriority(priority);
- syncLevels();
- }
-
- private void syncLevels() {
- synchronized (this) {
- if (jdkLogger == null) {
- jdkLogger = java.util.logging.Logger.getLogger(name);
- }
- final BridgeHierarchy bridgeHierarchy = (BridgeHierarchy) repository;
- if (bridgeHierarchy == null) {
- // not fully initialized yet; bail out
- return;
- }
- final LevelMapper mapper = bridgeHierarchy.getLogBridgeHandler().getLevelMapper();
- final Level ourLevel = getLevel();
- if (ourLevel == null) {
- jdkLogger.setLevel(null);
- } else {
- jdkLogger.setLevel(mapper.getSourceLevelForTargetLevel(ourLevel));
- }
- }
- }
-}
Deleted: jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeRootLogger.java
===================================================================
--- jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeRootLogger.java 2009-03-07 11:19:43 UTC (rev 3032)
+++ jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/BridgeRootLogger.java 2009-03-08 02:52:12 UTC (rev 3033)
@@ -1,40 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logbridge;
-
-import org.apache.log4j.Level;
-
-public final class BridgeRootLogger extends BridgeLogger {
-
- protected BridgeRootLogger(Level level) {
- super("root");
- if (level == null) {
- throw new NullPointerException("level is null");
- }
- setLevel(level);
- }
-
- public void setLevel(final Level level) {
- if (level != null) super.setLevel(level);
- }
-}
Modified: jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogBridgeHandler.java
===================================================================
--- jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogBridgeHandler.java 2009-03-07 11:19:43 UTC (rev 3032)
+++ jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogBridgeHandler.java 2009-03-08 02:52:12 UTC (rev 3033)
@@ -22,7 +22,9 @@
package org.jboss.logbridge;
+import java.util.Map;
import java.util.Collections;
+import java.util.WeakHashMap;
import java.util.Enumeration;
import java.util.logging.Handler;
@@ -31,10 +33,7 @@
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
-import org.apache.log4j.LogManager;
-import org.apache.log4j.Appender;
import org.apache.log4j.spi.LoggerRepository;
-import org.apache.log4j.spi.RepositorySelector;
/**
*
@@ -42,9 +41,18 @@
public final class LogBridgeHandler extends Handler {
private final LevelMapper levelMapper = new LevelMapper();
+
@SuppressWarnings({ "NonConstantLogger" })
private final java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
+ /**
+ * A weak-key map of log4j loggers to JDK loggers. Keep this map so that the configuration setting is
+ * retained even if the JDK logger is otherwise unreferenced.
+ */
+ private final Map<Logger, java.util.logging.Logger> loggerMap = Collections.synchronizedMap(new WeakHashMap<Logger,java.util.logging.Logger>());
+
+ private static final Logger log = Logger.getLogger(LogBridgeHandler.class);
+
public void setFilter(final Filter newFilter) throws SecurityException {
}
@@ -70,38 +78,9 @@
public void start() {
rootLogger.addHandler(this);
- final LoggerRepository newRepos = new BridgeHierarchy(new BridgeRootLogger(org.apache.log4j.Level.DEBUG), this);
- final LoggerRepository oldRepos = LogManager.getLoggerRepository();
- // Copy over all the logger data, with appenders.
- final Enumeration le = oldRepos.getCurrentLoggers();
- while (le.hasMoreElements()) {
- final Logger oldLogger = (Logger) le.nextElement();
- final String name = oldLogger.getName();
- final Logger newLogger = newRepos.getLogger(name);
- copyAppenders(oldLogger, newLogger);
- final org.apache.log4j.Level oldLevel = oldLogger.getLevel();
- if (oldLevel != null) {
- newLogger.setLevel(oldLevel);
- }
- newLogger.setAdditivity(oldLogger.getAdditivity());
- newLogger.setResourceBundle(oldLogger.getResourceBundle());
- }
- copyAppenders(oldRepos.getRootLogger(), newRepos.getRootLogger());
- LogManager.setRepositorySelector(new RepositorySelector() {
- public LoggerRepository getLoggerRepository() {
- return newRepos;
- }
- }, null);
+ updateLoggers();
}
- private static void copyAppenders(final Logger oldLogger, final Logger newLogger) {
- final Enumeration ae = oldLogger.getAllAppenders();
- while (ae.hasMoreElements()) {
- final Appender appender = (Appender) ae.nextElement();
- newLogger.addAppender(appender);
- }
- }
-
public void stop() {
rootLogger.removeHandler(this);
}
@@ -109,4 +88,38 @@
LevelMapper getLevelMapper() {
return levelMapper;
}
+
+ public void updateLoggers() {
+ log.trace("Syncing up JDK logger levels from Log4j");
+ final Map<Logger, java.util.logging.Logger> loggerMap = this.loggerMap;
+ final LevelMapper levelMapper = this.levelMapper;
+ loggerMap.clear();
+ final Logger rootLogger = Logger.getRootLogger();
+ final LoggerRepository repository = rootLogger.getLoggerRepository();
+ final Enumeration loggers = repository.getCurrentLoggers();
+ while (loggers.hasMoreElements()) {
+ final Logger logger = (Logger) loggers.nextElement();
+ final String name = logger.getName();
+ final java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(name);
+ final org.apache.log4j.Level targetLevel = logger.getLevel();
+ if (targetLevel == null) {
+ if (log.isTraceEnabled()) {
+ log.trace("Remapping logger \"" + name + "\" with null level");
+ }
+ jdkLogger.setLevel(null);
+ } else {
+ final Level sourceLevel = levelMapper.getSourceLevelForTargetLevel(targetLevel);
+ if (log.isTraceEnabled()) {
+ log.trace("Remapping logger \"" + name + "\" to JDK level \"" + sourceLevel + "\"");
+ }
+ loggerMap.put(logger, jdkLogger);
+ jdkLogger.setLevel(sourceLevel);
+ }
+ }
+ final Level sourceLevel = levelMapper.getSourceLevelForTargetLevel(rootLogger.getLevel());
+ if (log.isTraceEnabled()) {
+ log.trace("Remapping root logger to JDK level \"" + sourceLevel + "\"");
+ }
+ this.rootLogger.setLevel(sourceLevel);
+ }
}
Added: jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogNotificationListener.java
===================================================================
--- jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogNotificationListener.java (rev 0)
+++ jboss-logbridge/trunk/src/main/java/org/jboss/logbridge/LogNotificationListener.java 2009-03-08 02:52:12 UTC (rev 3033)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logbridge;
+
+import javax.management.NotificationListener;
+import javax.management.Notification;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.InstanceNotFoundException;
+import javax.management.ListenerNotFoundException;
+import javax.management.MalformedObjectNameException;
+
+import org.apache.log4j.Logger;
+
+public final class LogNotificationListener implements NotificationListener {
+ private LogBridgeHandler logBridgeHandler;
+ private MBeanServer mBeanServer;
+ private ObjectName loggingMBeanName;
+
+ private static final Logger log = Logger.getLogger(LogNotificationListener.class);
+
+ public LogBridgeHandler getLogBridgeHandler() {
+ return logBridgeHandler;
+ }
+
+ public void setLogBridgeHandler(final LogBridgeHandler logBridgeHandler) {
+ this.logBridgeHandler = logBridgeHandler;
+ }
+
+ public MBeanServer getMBeanServer() {
+ return mBeanServer;
+ }
+
+ public void setMBeanServer(final MBeanServer mBeanServer) {
+ this.mBeanServer = mBeanServer;
+ }
+
+ public String getLoggingMBeanName() {
+ return loggingMBeanName.toString();
+ }
+
+ public void setLoggingMBeanName(final String loggingMBeanName) throws MalformedObjectNameException {
+ this.loggingMBeanName = new ObjectName(loggingMBeanName);
+ }
+
+ public void handleNotification(final Notification notification, final Object handback) {
+ logBridgeHandler.updateLoggers();
+ }
+
+ public void start() throws InstanceNotFoundException {
+ log.info("Adding notification listener for logging mbean \"" + loggingMBeanName + "\" to server " + mBeanServer);
+ mBeanServer.addNotificationListener(loggingMBeanName, this, null, null);
+ logBridgeHandler.updateLoggers();
+ }
+
+ public void stop() throws ListenerNotFoundException, InstanceNotFoundException {
+ mBeanServer.removeNotificationListener(loggingMBeanName, this);
+ }
+}
More information about the jboss-svn-commits
mailing list