[jboss-svn-commits] JBoss Common SVN: r3851 - jboss-logmanager/trunk/src/main/java/org/jboss/logmanager.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 14 14:23:44 EST 2009
Author: david.lloyd at jboss.com
Date: 2009-12-14 14:23:43 -0500 (Mon, 14 Dec 2009)
New Revision: 3851
Added:
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WrappedExtLogRecord.java
Modified:
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtFormatter.java
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/Logger.java
Log:
Better solution to wrapping JDK LogRecords which does not involve reflection or other trickery
Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtFormatter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtFormatter.java 2009-12-14 15:51:06 UTC (rev 3850)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtFormatter.java 2009-12-14 19:23:43 UTC (rev 3851)
@@ -22,8 +22,6 @@
package org.jboss.logmanager;
-import org.jboss.logmanager.ExtLogRecord;
-
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
@@ -32,11 +30,9 @@
*/
public abstract class ExtFormatter extends Formatter {
- private static final String LOGGER_CLASS_NAME = org.jboss.logmanager.Logger.class.getName();
-
/** {@inheritDoc} */
public final String format(final LogRecord record) {
- return format((record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME));
+ return format(ExtLogRecord.wrap(record));
}
/**
Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java 2009-12-14 15:51:06 UTC (rev 3850)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java 2009-12-14 19:23:43 UTC (rev 3851)
@@ -36,7 +36,6 @@
*/
public abstract class ExtHandler extends Handler implements FlushableCloseable {
- private static final String LOGGER_CLASS_NAME = org.jboss.logmanager.Logger.class.getName();
private static final Permission CONTROL_PERMISSION = new LoggingPermission("control", null);
private volatile boolean autoFlush;
@@ -55,8 +54,7 @@
/** {@inheritDoc} */
public final void publish(final LogRecord record) {
if (record != null && isLoggable(record)) {
- final ExtLogRecord extRecord = (record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME);
- doPublish(extRecord);
+ doPublish(ExtLogRecord.wrap(record));
}
}
Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java 2009-12-14 15:51:06 UTC (rev 3850)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java 2009-12-14 19:23:43 UTC (rev 3851)
@@ -28,9 +28,6 @@
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
-import java.lang.reflect.Field;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import java.util.logging.LogRecord;
@@ -77,24 +74,21 @@
* @param loggerClassName the name of the logger class
*/
public ExtLogRecord(final java.util.logging.Level level, final String msg, final FormatStyle formatStyle, final String loggerClassName) {
- this(level, msg, formatStyle, loggerClassName, NDC.get());
- }
-
- private ExtLogRecord(final java.util.logging.Level level, final String msg, final FormatStyle formatStyle, final String loggerClassName, final String ndc) {
super(level, msg);
this.formatStyle = formatStyle == null ? FormatStyle.MESSAGE_FORMAT : formatStyle;
this.loggerClassName = loggerClassName;
- this.ndc = ndc;
+ ndc = NDC.get();
threadName = Thread.currentThread().getName();
}
/**
- * Construct a new instance by copying an original record.
+ * Make a copy of a log record.
*
- * @param original the record to copy
+ * @param original the original
*/
- public ExtLogRecord(final LogRecord original, final String loggerClassName) {
- this(original.getLevel(), original.getMessage(), FormatStyle.MESSAGE_FORMAT, loggerClassName, original instanceof ExtLogRecord ? ((ExtLogRecord)original).ndc : NDC.get());
+ public ExtLogRecord(final ExtLogRecord original) {
+ super(original.getLevel(), original.getMessage());
+ // LogRecord fields
setLoggerName(original.getLoggerName());
setMillis(original.getMillis());
setParameters(original.getParameters());
@@ -103,33 +97,35 @@
setSequenceNumber(original.getSequenceNumber());
setThreadID(original.getThreadID());
setThrown(original.getThrown());
- if (original instanceof ExtLogRecord) {
- final ExtLogRecord extLogRecord = (ExtLogRecord) original;
- if (! extLogRecord.calculateCaller) {
- setSourceClassName(extLogRecord.getSourceClassName());
- setSourceMethodName(extLogRecord.getSourceMethodName());
- setSourceFileName(extLogRecord.sourceFileName);
- setSourceLineNumber(extLogRecord.sourceLineNumber);
- formatStyle = extLogRecord.formatStyle;
- final Map<String, String> mdcCopy = extLogRecord.mdcCopy;
- if (mdcCopy != null) {
- this.mdcCopy = mdcCopy;
- }
- threadName = extLogRecord.threadName;
- }
+ if (!original.calculateCaller) {
+ setSourceClassName(original.getSourceClassName());
+ setSourceMethodName(original.getSourceMethodName());
+ sourceFileName = original.sourceFileName;
+ sourceLineNumber = original.sourceLineNumber;
+ }
+ formatStyle = original.formatStyle;
+ mdcCopy = original.mdcCopy;
+ ndc = original.ndc;
+ loggerClassName = original.loggerClassName;
+ threadName = original.threadName;
+ resourceKey = original.resourceKey;
+ formattedMessage = original.formattedMessage;
+ }
+
+ /**
+ * Wrap a JDK log record. If the target record is already an {@code ExtLogRecord}, it is simply returned. Otherwise
+ * a wrapper record is created and returned.
+ *
+ * @param rec the original record
+ * @return the wrapped record
+ */
+ public static ExtLogRecord wrap(LogRecord rec) {
+ if (rec == null) {
+ return null;
+ } else if (rec instanceof ExtLogRecord) {
+ return (ExtLogRecord) rec;
} else {
- try {
- final String sourceClassName = (String) SOURCE_CLASS_NAME.get(original);
- if (sourceClassName != null) {
- setSourceClassName(sourceClassName);
- }
- final String sourceMethodName = (String) SOURCE_METHOD_NAME.get(original);
- if (sourceMethodName != null) {
- setSourceMethodName(sourceMethodName);
- }
- } catch (IllegalAccessException e) {
- // ignore and recalculate caller
- }
+ return new WrappedExtLogRecord(rec);
}
}
@@ -137,9 +133,6 @@
private transient final String loggerClassName;
private transient boolean calculateCaller = true;
- private static final Field SOURCE_CLASS_NAME = getAccessibleField("sourceClassName");
- private static final Field SOURCE_METHOD_NAME = getAccessibleField("sourceMethodName");
-
private FormatStyle formatStyle = FormatStyle.MESSAGE_FORMAT;
private Map<String, String> mdcCopy;
private int sourceLineNumber = -1;
@@ -148,21 +141,6 @@
private String formattedMessage;
private String threadName;
- private static Field getAccessibleField(final String name) {
- return AccessController.doPrivileged(new PrivilegedAction<Field>() {
- public Field run() {
- final Field field;
- try {
- field = LogRecord.class.getDeclaredField(name);
- } catch (NoSuchFieldException e) {
- throw new NoSuchFieldError(e.getMessage());
- }
- field.setAccessible(true);
- return field;
- }
- });
- }
-
private void writeObject(ObjectOutputStream oos) throws IOException {
copyAll();
oos.defaultWriteObject();
Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/Logger.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/Logger.java 2009-12-14 15:51:06 UTC (rev 3850)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/Logger.java 2009-12-14 19:23:43 UTC (rev 3851)
@@ -866,7 +866,7 @@
* @param record the log record
*/
public void logRaw(final LogRecord record) {
- logRaw((record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME));
+ logRaw(ExtLogRecord.wrap(record));
}
/**
Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WrappedExtLogRecord.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WrappedExtLogRecord.java (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/WrappedExtLogRecord.java 2009-12-14 19:23:43 UTC (rev 3851)
@@ -0,0 +1,149 @@
+/*
+ * 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;
+
+import java.util.ResourceBundle;
+
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+class WrappedExtLogRecord extends ExtLogRecord {
+
+ private static final long serialVersionUID = 980830752574061944L;
+ private static final String LOGGER_CLASS_NAME = java.util.logging.Logger.class.getName();
+
+ private final LogRecord orig;
+
+ WrappedExtLogRecord(final LogRecord orig) {
+ super(orig.getLevel(), orig.getMessage(), LOGGER_CLASS_NAME);
+ this.orig = orig;
+ }
+
+ public String getLoggerName() {
+ return orig.getLoggerName();
+ }
+
+ public void setLoggerName(final String name) {
+ super.setLoggerName(name);
+ orig.setLoggerName(name);
+ }
+
+ public ResourceBundle getResourceBundle() {
+ return orig.getResourceBundle();
+ }
+
+ public void setResourceBundle(final ResourceBundle bundle) {
+ super.setResourceBundle(bundle);
+ orig.setResourceBundle(bundle);
+ }
+
+ public String getResourceBundleName() {
+ return orig.getResourceBundleName();
+ }
+
+ public void setResourceBundleName(final String name) {
+ super.setResourceBundleName(name);
+ orig.setResourceBundleName(name);
+ }
+
+ public Level getLevel() {
+ return orig.getLevel();
+ }
+
+ public void setLevel(final Level level) {
+ super.setLevel(level);
+ orig.setLevel(level);
+ }
+
+ public long getSequenceNumber() {
+ return orig.getSequenceNumber();
+ }
+
+ public void setSequenceNumber(final long seq) {
+ super.setSequenceNumber(seq);
+ orig.setSequenceNumber(seq);
+ }
+
+ public String getSourceClassName() {
+ return orig.getSourceClassName();
+ }
+
+ public void setSourceClassName(final String sourceClassName) {
+ super.setSourceClassName(sourceClassName);
+ orig.setSourceClassName(sourceClassName);
+ }
+
+ public String getSourceMethodName() {
+ return orig.getSourceMethodName();
+ }
+
+ public void setSourceMethodName(final String sourceMethodName) {
+ super.setSourceMethodName(sourceMethodName);
+ orig.setSourceMethodName(sourceMethodName);
+ }
+
+ public String getMessage() {
+ return orig.getMessage();
+ }
+
+ public void setMessage(final String message) {
+ super.setMessage(message);
+ orig.setMessage(message);
+ }
+
+ public Object[] getParameters() {
+ return orig.getParameters();
+ }
+
+ public void setParameters(final Object[] parameters) {
+ orig.setParameters(parameters);
+ }
+
+ public int getThreadID() {
+ return orig.getThreadID();
+ }
+
+ public void setThreadID(final int threadID) {
+ orig.setThreadID(threadID);
+ }
+
+ public long getMillis() {
+ return orig.getMillis();
+ }
+
+ public void setMillis(final long millis) {
+ orig.setMillis(millis);
+ }
+
+ public Throwable getThrown() {
+ return orig.getThrown();
+ }
+
+ public void setThrown(final Throwable thrown) {
+ orig.setThrown(thrown);
+ }
+
+ protected Object writeReplace() {
+ return new ExtLogRecord(this);
+ }
+}
More information about the jboss-svn-commits
mailing list