[jboss-svn-commits] JBoss Common SVN: r3271 - in jboss-logmanager-log4j/trunk: src and 8 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jun 16 22:40:23 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-06-16 22:40:22 -0400 (Tue, 16 Jun 2009)
New Revision: 3271

Added:
   jboss-logmanager-log4j/trunk/src/
   jboss-logmanager-log4j/trunk/src/main/
   jboss-logmanager-log4j/trunk/src/main/java/
   jboss-logmanager-log4j/trunk/src/main/java/org/
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/ConvertedLoggingEvent.java
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/filters/
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/filters/Log4jFilter.java
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jAppenderHandler.java
   jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jJDKLevel.java
Log:
Initial import of log4j integration code

Added: jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/ConvertedLoggingEvent.java
===================================================================
--- jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/ConvertedLoggingEvent.java	                        (rev 0)
+++ jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/ConvertedLoggingEvent.java	2009-06-17 02:40:22 UTC (rev 3271)
@@ -0,0 +1,94 @@
+/*
+ * 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.logmanager.log4j;
+
+import java.util.IdentityHashMap;
+import java.util.Map;
+import org.jboss.logmanager.ExtLogRecord;
+import org.jboss.logmanager.log4j.handlers.Log4jJDKLevel;
+
+import java.util.logging.Level;
+import org.apache.log4j.Category;
+import org.apache.log4j.spi.LocationInfo;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.spi.ThrowableInformation;
+
+/**
+ * A log4j logging event which was converted from a JBoss LogManager {@link org.jboss.logmanager.ExtLogRecord ExtLogRecord}.
+ */
+public final class ConvertedLoggingEvent extends LoggingEvent {
+
+    private static final long serialVersionUID = -2741722431458191906L;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param rec the log record
+     */
+    public ConvertedLoggingEvent(final ExtLogRecord rec) {
+        super(rec.getLoggerClassName(),
+                new DummyCategory(rec.getLoggerName()),
+                rec.getMillis(),
+                getPriorityFor(rec.getLevel()),
+                rec.getMessage(),
+                rec.getThreadName(),
+                rec.getThrown() == null ? null : new ThrowableInformation(rec.getThrown()),
+                rec.getNdc(),
+                new LocationInfo(new Throwable(), rec.getLoggerClassName()),
+                null);
+    }
+
+    private static final Map<Level, org.apache.log4j.Level> priorityMap;
+
+    static {
+        final Map<Level, org.apache.log4j.Level> map = new IdentityHashMap<Level, org.apache.log4j.Level>();
+        map.put(Level.SEVERE, Log4jJDKLevel.SEVERE);
+        map.put(Level.WARNING, Log4jJDKLevel.WARNING);
+        map.put(Level.CONFIG, Log4jJDKLevel.CONFIG);
+        map.put(Level.INFO, Log4jJDKLevel.INFO);
+        map.put(Level.FINE, Log4jJDKLevel.FINE);
+        map.put(Level.FINER, Log4jJDKLevel.FINER);
+        map.put(Level.FINEST, Log4jJDKLevel.FINEST);
+
+        map.put(org.jboss.logmanager.Level.FATAL, org.apache.log4j.Level.FATAL);
+        map.put(org.jboss.logmanager.Level.ERROR, org.apache.log4j.Level.ERROR);
+        map.put(org.jboss.logmanager.Level.WARN, org.apache.log4j.Level.WARN);
+        map.put(org.jboss.logmanager.Level.INFO, org.apache.log4j.Level.INFO);
+        map.put(org.jboss.logmanager.Level.DEBUG, org.apache.log4j.Level.DEBUG);
+        map.put(org.jboss.logmanager.Level.TRACE, org.apache.log4j.Level.TRACE);
+
+        priorityMap = map;
+    }
+
+    static org.apache.log4j.Level getPriorityFor(Level level) {
+        final org.apache.log4j.Level p;
+        return (p = priorityMap.get(level)) == null ? org.apache.log4j.Level.DEBUG : p;
+    }
+
+    private static final class DummyCategory extends Category {
+
+        protected DummyCategory(String name) {
+            super(name);
+        }
+    }
+}

Added: jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/filters/Log4jFilter.java
===================================================================
--- jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/filters/Log4jFilter.java	                        (rev 0)
+++ jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/filters/Log4jFilter.java	2009-06-17 02:40:22 UTC (rev 3271)
@@ -0,0 +1,68 @@
+/*
+ * 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.logmanager.log4j.filters;
+
+import org.jboss.logmanager.ExtLogRecord;
+import org.jboss.logmanager.handlers.log4j.ConvertedLoggingEvent;
+
+import java.util.logging.LogRecord;
+import org.apache.log4j.spi.Filter;
+
+/**
+ * A bridge filter to a log4j filter chain.
+ */
+public final class Log4jFilter implements java.util.logging.Filter {
+    private final Filter filterChain;
+    private final boolean defaultResult;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filterChain the log4j filter chain
+     * @param defaultResult the result to use if the filter chain returns {@link Filter#NEUTRAL}
+     */
+    public Log4jFilter(final Filter filterChain, final boolean defaultResult) {
+        this.filterChain = filterChain;
+        this.defaultResult = defaultResult;
+    }
+
+    /**
+     * Determine if the record is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if it is loggable
+     */
+    public boolean isLoggable(final LogRecord record) {
+        final ExtLogRecord extRec = (record instanceof ExtLogRecord) ? (ExtLogRecord)record : new ExtLogRecord(record, java.util.logging.Logger.class.getName());
+        Filter filter = filterChain;
+        while (filter != null) {
+            final int result = filter.decide(new ConvertedLoggingEvent(extRec));
+            switch (result) {
+                case Filter.DENY: return false;
+                case Filter.ACCEPT: return true;
+            }
+            filter = filter.getNext();
+        }
+        return defaultResult;
+    }
+}

Added: jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jAppenderHandler.java
===================================================================
--- jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jAppenderHandler.java	                        (rev 0)
+++ jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jAppenderHandler.java	2009-06-17 02:40:22 UTC (rev 3271)
@@ -0,0 +1,83 @@
+/*
+ * 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.logmanager.log4j.handlers;
+
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.jboss.logmanager.ExtLogRecord;
+import org.jboss.logmanager.LogManager;
+import org.jboss.logmanager.log4j.ConvertedLoggingEvent;
+import org.jboss.logmanager.handlers.ExtHandler;
+
+import org.apache.log4j.Appender;
+import org.apache.log4j.spi.LoggingEvent;
+
+/**
+ * A handler which delegates to a log4j appender.
+ */
+public final class Log4jAppenderHandler extends ExtHandler {
+    private volatile Appender appender = null;
+
+    private static final AtomicReferenceFieldUpdater<Log4jAppenderHandler, Appender> appenderUpdater = AtomicReferenceFieldUpdater.newUpdater(Log4jAppenderHandler.class, Appender.class, "appender");
+
+    /**
+     * Construct a new instance.
+     *
+     * @param appender the appender to delegate to
+     */
+    public Log4jAppenderHandler(final Appender appender) {
+        appenderUpdater.set(this, appender);
+    }
+
+    /**
+     * Publish a log record.
+     *
+     * @param record the log record to publish
+     */
+    public void publish(final ExtLogRecord record) {
+        final Appender appender = this.appender;
+        if (appender == null) {
+            throw new IllegalStateException("Appender is closed");
+        }
+        final LoggingEvent event = new ConvertedLoggingEvent(record);
+        appender.doAppend(event);
+    }
+
+    /**
+     * Do nothing (there is no equivalent method on log4j appenders).
+     */
+    public void flush() {
+    }
+
+    /**
+     * Close the handler and its corresponding appender.
+     *
+     * @throws SecurityException if you are not allowed to close a handler
+     */
+    public void close() throws SecurityException {
+        LogManager.getLogManager().checkAccess();
+        final Appender appender = appenderUpdater.getAndSet(this, null);
+        if (appender != null) {
+            appender.close();
+        }
+    }
+}

Added: jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jJDKLevel.java
===================================================================
--- jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jJDKLevel.java	                        (rev 0)
+++ jboss-logmanager-log4j/trunk/src/main/java/org/jboss/logmanager/log4j/handlers/Log4jJDKLevel.java	2009-06-17 02:40:22 UTC (rev 3271)
@@ -0,0 +1,113 @@
+/*
+ * 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.logmanager.log4j.handlers;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Level;
+
+/**
+ * Log4j equivalents for JDK logging levels.  For configuration file usage.
+ */
+public final class Log4jJDKLevel extends Level {
+
+    private static final long serialVersionUID = -2456662804627419121L;
+
+    /**
+     * Instantiate a Level object.
+     */
+    protected Log4jJDKLevel(int level, String levelStr, int syslogEquivalent) {
+        super(level, levelStr, syslogEquivalent);
+    }
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#SEVERE SEVERE} level; numerically
+     * equivalent to log4j's {@link org.apache.log4j.Level#ERROR ERROR} level.
+     */
+    public static final Level SEVERE = new Log4jJDKLevel(Level.ERROR_INT, "SEVERE", 3);
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#WARNING WARNING} level; numerically
+     * equivalent to log4j's {@link org.apache.log4j.Level#WARN WARN} level.
+     */
+    public static final Level WARNING = new Log4jJDKLevel(Level.WARN_INT, "WARNING", 4);
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#INFO INFO} level; numerically
+     * equivalent to log4j's {@link org.apache.log4j.Level#INFO INFO} level.
+     */
+    public static final Level INFO = new Log4jJDKLevel(Level.INFO_INT, "INFO", 5);
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#CONFIG CONFIG} level; numerically
+     * falls between log4j's {@link org.apache.log4j.Level#INFO INFO} and {@link org.apache.log4j.Level#DEBUG DEBUG} levels.
+     */
+    public static final Level CONFIG = new Log4jJDKLevel(Level.INFO_INT - 5000, "CONFIG", 6);
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#FINE FINE} level; numerically
+     * equivalent to log4j's {@link org.apache.log4j.Level#DEBUG DEBUG} level.
+     */
+    public static final Level FINE = new Log4jJDKLevel(Level.DEBUG_INT, "FINE", 7);
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#FINER FINER} level; numerically
+     * falls between log4j's {@link org.apache.log4j.Level#DEBUG DEBUG} and {@link org.apache.log4j.Level#TRACE TRACE} levels.
+     */
+    public static final Level FINER = new Log4jJDKLevel(Level.DEBUG_INT - 2500, "FINER", 7);
+
+    /**
+     * A mapping of the JDK logging {@link java.util.logging.Level#FINEST FINEST} level; numerically
+     * equivalent to log4j's {@link org.apache.log4j.Level#TRACE TRACE} level.
+     */
+    public static final Level FINEST = new Log4jJDKLevel(Level.TRACE_INT, "FINEST", 7);
+
+    private static final Map<String, Level> levelMapping = new HashMap<String, Level>();
+
+    private static void add(Level lvl) {
+        levelMapping.put(lvl.toString(), lvl);
+    }
+
+    static {
+        add(SEVERE);
+        add(WARNING);
+        add(INFO);
+        add(CONFIG);
+        add(FINE);
+        add(FINER);
+        add(FINEST);
+    }
+
+    /**
+     * Get the level for the given name.  If the level is not one of the levels defined in this class,
+     * this method delegates to {@link org.apache.log4j.Level#toLevel(String) toLevel(String)} on the superclass.
+     *
+     * @param name the level name
+     * @return the equivalent level
+     */
+    public static Level toLevel(String name) {
+        final Level level = levelMapping.get(name.trim().toUpperCase());
+        return level != null ? level : Level.toLevel(name);
+    }
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list