[jboss-svn-commits] JBoss Common SVN: r3853 - common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 14 18:04:45 EST 2009
Author: david.lloyd at jboss.com
Date: 2009-12-14 18:04:44 -0500 (Mon, 14 Dec 2009)
New Revision: 3853
Added:
common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JBossLogRecord.java
Modified:
common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPluginInstance.java
Log:
Fix class and method name resolution
Added: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JBossLogRecord.java
===================================================================
--- common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JBossLogRecord.java (rev 0)
+++ common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JBossLogRecord.java 2009-12-14 23:04:44 UTC (rev 3853)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.logging.jdk;
+
+import java.util.logging.LogRecord;
+import java.util.logging.Level;
+
+/**
+ * An implementation of {@code LogRecord} which correctly supports getting the source class and method names.
+ */
+class JBossLogRecord extends LogRecord {
+
+ private static final long serialVersionUID = 2590176069006850090L;
+
+ private final String loggerClassName;
+ private boolean resolved;
+
+ /**
+ * Construct a new instance.
+ *
+ * @param level the level
+ * @param msg the message
+ * @param loggerClassName the frontend logger class name
+ */
+ JBossLogRecord(final Level level, final String msg, final String loggerClassName) {
+ super(level, msg);
+ this.loggerClassName = loggerClassName;
+ }
+
+ /** {@inheritDoc} */
+ public String getSourceClassName() {
+ if (! resolved) {
+ resolve();
+ }
+ return super.getSourceClassName();
+ }
+
+ /** {@inheritDoc} */
+ public void setSourceClassName(final String sourceClassName) {
+ resolved = true;
+ super.setSourceClassName(sourceClassName);
+ }
+
+ /** {@inheritDoc} */
+ public String getSourceMethodName() {
+ if (! resolved) {
+ resolve();
+ }
+ return super.getSourceMethodName();
+ }
+
+ /** {@inheritDoc} */
+ public void setSourceMethodName(final String sourceMethodName) {
+ resolved = true;
+ super.setSourceMethodName(sourceMethodName);
+ }
+
+ /**
+ * Resolve the caller class name.
+ */
+ private void resolve() {
+ resolved = true;
+ final StackTraceElement[] stack = new Throwable().getStackTrace();
+ boolean found = false;
+ for (StackTraceElement element : stack) {
+ final String className = element.getClassName();
+ if (found) {
+ if (! loggerClassName.equals(className)) {
+ setSourceClassName(className);
+ setSourceMethodName(element.getMethodName());
+ return;
+ }
+ } else {
+ found = loggerClassName.equals(className);
+ }
+ }
+ setSourceClassName("<unknown>");
+ setSourceMethodName("<unknown>");
+ }
+
+ /**
+ * Serialize to the standard JDK {@code LogRecord} instance, with class and method names fully resolved.
+ *
+ * @return the replacement log record
+ */
+ protected Object writeReplace() {
+ final LogRecord replacement = new LogRecord(getLevel(), getMessage());
+ replacement.setResourceBundle(getResourceBundle());
+ replacement.setLoggerName(getLoggerName());
+ replacement.setMillis(getMillis());
+ replacement.setParameters(getParameters());
+ replacement.setResourceBundleName(getResourceBundleName());
+ replacement.setSequenceNumber(getSequenceNumber());
+ replacement.setSourceClassName(getSourceClassName());
+ replacement.setSourceMethodName(getSourceMethodName());
+ replacement.setThreadID(getThreadID());
+ replacement.setThrown(getThrown());
+ return replacement;
+ }
+}
Modified: common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPluginInstance.java
===================================================================
--- common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPluginInstance.java 2009-12-14 22:51:19 UTC (rev 3852)
+++ common-logging-jdk/trunk/src/main/java/org/jboss/logging/jdk/JDK14LoggerPluginInstance.java 2009-12-14 23:04:44 UTC (rev 3853)
@@ -32,7 +32,6 @@
import java.util.Map;
import java.util.EnumMap;
-import java.util.ResourceBundle;
/**
* An example LoggerPlugin which uses the JDK java.util.logging framework.
@@ -71,23 +70,19 @@
}
public void log(final org.jboss.logging.Logger.Level level, final String loggerFqcn, final Object message, final Object[] params, final Throwable t) {
- LogRecord record = new LogRecord(LEVEL_MAP.get(level), String.valueOf(message));
+ LogRecord record = new JBossLogRecord(LEVEL_MAP.get(level), String.valueOf(message), loggerFqcn);
record.setParameters(params);
record.setLoggerName(name);
record.setThrown(t);
- record.setSourceMethodName(null); // prevent expensive, yet guaranteed to be incorrect lookup
- final ResourceBundle bundle = log.getResourceBundle();
- if (bundle != null) record.setResourceBundle(bundle);
+ record.setResourceBundle(log.getResourceBundle());
log.log(record);
}
protected void log(final org.jboss.logging.Logger.Level level, final String loggerFqcn, final String message, final Throwable t) {
- LogRecord record = new LogRecord(LEVEL_MAP.get(level), message);
+ LogRecord record = new JBossLogRecord(LEVEL_MAP.get(level), message, loggerFqcn);
record.setLoggerName(name);
record.setThrown(t);
- record.setSourceMethodName(null); // prevent expensive, yet guaranteed to be incorrect lookup
- final ResourceBundle bundle = log.getResourceBundle();
- if (bundle != null) record.setResourceBundle(bundle);
+ record.setResourceBundle(log.getResourceBundle());
log.log(record);
}
}
More information about the jboss-svn-commits
mailing list