[jboss-svn-commits] JBoss Common SVN: r4847 - common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Sep 21 10:50:58 EDT 2010
Author: david.lloyd at jboss.com
Date: 2010-09-21 10:50:58 -0400 (Tue, 21 Sep 2010)
New Revision: 4847
Added:
common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/LogRecordImpl.java
Modified:
common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java
Log:
JBLOGGING-46: Calculate proper line numbers
Modified: common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java
===================================================================
--- common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java 2010-09-21 14:43:03 UTC (rev 4846)
+++ common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/JDK14LoggerPlugin.java 2010-09-21 14:50:58 UTC (rev 4847)
@@ -49,10 +49,9 @@
}
private void doLog(Level level, Object message, Throwable t) {
- LogRecord record = new LogRecord(level, message.toString());
+ LogRecord record = new LogRecordImpl(level, message.toString());
record.setLoggerName(name);
record.setThrown(t);
- record.setSourceMethodName(null); // prevent expensive, yet pointless, lookup
log.log(record);
}
Added: common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/LogRecordImpl.java
===================================================================
--- common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/LogRecordImpl.java (rev 0)
+++ common-logging-jdk/branches/2.1/src/main/java/org/jboss/logging/jdk/LogRecordImpl.java 2010-09-21 14:50:58 UTC (rev 4847)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.logging.jdk;
+
+import org.jboss.logging.Logger;
+
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * A log record which knows how to calculate its source method and class name.
+ *
+ * @author <a href="mailto:david.lloyd at redhat.com">David M. Lloyd</a>
+ */
+final class LogRecordImpl extends LogRecord {
+
+ private static final long serialVersionUID = 542119905844866161L;
+ private boolean resolved;
+ private static final String LOGGER_CLASS_NAME = Logger.class.getName();
+
+ LogRecordImpl(final Level level, final String msg) {
+ super(level, msg);
+ }
+
+ public String getSourceClassName() {
+ if (! resolved) {
+ resolve();
+ }
+ return super.getSourceClassName();
+ }
+
+ public void setSourceClassName(final String sourceClassName) {
+ resolved = true;
+ super.setSourceClassName(sourceClassName);
+ }
+
+ public String getSourceMethodName() {
+ if (! resolved) {
+ resolve();
+ }
+ return super.getSourceMethodName();
+ }
+
+ public void setSourceMethodName(final String sourceMethodName) {
+ resolved = true;
+ super.setSourceMethodName(sourceMethodName);
+ }
+
+ 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 (! LOGGER_CLASS_NAME.equals(className)) {
+ setSourceClassName(className);
+ setSourceMethodName(element.getMethodName());
+ return;
+ }
+ } else {
+ found = LOGGER_CLASS_NAME.equals(className);
+ }
+ }
+ setSourceClassName("<unknown>");
+ setSourceMethodName("<unknown>");
+ }
+
+ 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;
+ }
+
+}
More information about the jboss-svn-commits
mailing list