[jboss-svn-commits] JBoss Common SVN: r3069 - jboss-logmanager/trunk/src/main/java/org/jboss/logmanager.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Mar 12 16:32:47 EDT 2009
Author: david.lloyd at jboss.com
Date: 2009-03-12 16:32:47 -0400 (Thu, 12 Mar 2009)
New Revision: 3069
Modified:
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggerInstance.java
Log:
Add source line number info, and better source class/method detection
Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java 2009-03-12 15:07:45 UTC (rev 3068)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java 2009-03-12 20:32:47 UTC (rev 3069)
@@ -29,7 +29,8 @@
import java.util.logging.LogRecord;
/**
- *
+ * An extended log record, which includes additional information including MDC/NDC and correct
+ * caller location (even in the presence of a logging facade).
*/
public class ExtLogRecord extends LogRecord {
@@ -40,18 +41,49 @@
*
* @param level a logging level value
* @param msg the raw non-localized logging message (may be null)
+ * @param loggerClassName the name of the logger class
*/
- public ExtLogRecord(java.util.logging.Level level, String msg) {
+ public ExtLogRecord(final java.util.logging.Level level, final String msg, final String loggerClassName) {
super(level, msg);
- setSourceClassName(null);
+ this.loggerClassName = loggerClassName;
ndc = NDC.get();
+ setUnknownCaller();
}
+ /**
+ * Construct a new instance by copying an original record.
+ *
+ * @param original the record to copy
+ */
+ public ExtLogRecord(final LogRecord original, final String loggerClassName) {
+ this(original.getLevel(), original.getMessage(), loggerClassName);
+ setLoggerName(original.getLoggerName());
+ setMillis(original.getMillis());
+ setParameters(original.getParameters());
+ setResourceBundle(original.getResourceBundle());
+ setResourceBundleName(original.getResourceBundleName());
+ setSequenceNumber(original.getSequenceNumber());
+ // todo - find a way to not infer caller info if not already inferred?
+ final String originalSourceClassName = original.getSourceClassName();
+ if (originalSourceClassName != null) setSourceClassName(originalSourceClassName);
+ final String originalSourceMethodName = original.getSourceMethodName();
+ if (originalSourceMethodName != null) setSourceMethodName(originalSourceMethodName);
+ // todo sourceLineNumber
+ setThreadID(original.getThreadID());
+ setThrown(original.getThrown());
+ }
+
+ private final String ndc;
+ private final String loggerClassName;
+
+ private transient boolean calculateCaller = true;
+
private Map<String, String> mdcCopy;
- private String ndc;
+ private int sourceLineNumber = -1;
private void writeObject(ObjectOutputStream oos) throws IOException {
copyMdc();
+ calculateCaller();
oos.defaultWriteObject();
}
@@ -73,14 +105,103 @@
*/
public String getMdc(String key) {
final Map<String, String> mdcCopy = this.mdcCopy;
- if (mdcCopy == null) {
- return MDC.get(key);
- } else {
- return mdcCopy.get(key);
- }
+ return mdcCopy != null ? mdcCopy.get(key) : MDC.get(key);
}
+ /**
+ * Get the NDC for this log record.
+ *
+ * @return the NDC
+ */
public String getNdc() {
return ndc;
}
+
+ /**
+ * Get the class name of the logger which created this record.
+ *
+ * @return the class name
+ */
+ public String getLoggerClassName() {
+ return loggerClassName;
+ }
+
+ /**
+ * Find the first stack frame below the call to the logger, and populate the log record with that information.
+ */
+ private void calculateCaller() {
+ if (! calculateCaller) {
+ return;
+ }
+ calculateCaller = false;
+ final StackTraceElement[] stack = new Throwable().getStackTrace();
+ boolean found = false;
+ for (StackTraceElement element : stack) {
+ final String className = element.getClassName();
+ if (found && ! loggerClassName.equals(className)) {
+ setSourceClassName(className);
+ setSourceMethodName(element.getMethodName());
+ setSourceLineNumber(element.getLineNumber());
+ return;
+ } else {
+ found = loggerClassName.equals(className);
+ }
+ }
+ setUnknownCaller();
+ }
+
+ private void setUnknownCaller() {
+ setSourceClassName("<unknown>");
+ setSourceMethodName("<unknown>");
+ setSourceLineNumber(-1);
+ }
+
+ /**
+ * Get the source line number for this log record.
+ * <p/>
+ * Note that this sourceLineNumber is not verified and may be spoofed. This information may either have been
+ * provided as part of the logging call, or it may have been inferred automatically by the logging framework. In the
+ * latter case, the information may only be approximate and may in fact describe an earlier call on the stack frame.
+ * May be -1 if no information could be obtained.
+ *
+ * @return the source line number
+ */
+ public int getSourceLineNumber() {
+ calculateCaller();
+ return sourceLineNumber;
+ }
+
+ /**
+ * Set the source line number for this log record.
+ *
+ * @param sourceLineNumber the source line number
+ */
+ public void setSourceLineNumber(final int sourceLineNumber) {
+ calculateCaller = false;
+ this.sourceLineNumber = sourceLineNumber;
+ }
+
+ /** {@inheritDoc} */
+ public String getSourceClassName() {
+ calculateCaller();
+ return super.getSourceClassName();
+ }
+
+ /** {@inheritDoc} */
+ public void setSourceClassName(final String sourceClassName) {
+ calculateCaller = false;
+ super.setSourceClassName(sourceClassName);
+ }
+
+ /** {@inheritDoc} */
+ public String getSourceMethodName() {
+ calculateCaller();
+ return super.getSourceMethodName();
+ }
+
+ /** {@inheritDoc} */
+ public void setSourceMethodName(final String sourceMethodName) {
+ calculateCaller = false;
+ super.setSourceMethodName(sourceMethodName);
+ }
}
Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggerInstance.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggerInstance.java 2009-03-12 15:07:45 UTC (rev 3068)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggerInstance.java 2009-03-12 20:32:47 UTC (rev 3069)
@@ -67,6 +67,7 @@
* The empty handler list.
*/
private static final Handler[] EMPTY_HANDLERS = new Handler[0];
+ private static final String LOGGER_CLASS_NAME = LoggerInstance.class.getName();
/**
* Construct a new instance of an actual logger.
@@ -261,11 +262,12 @@
* @param record the log record
*/
private void doLog(final LogRecord record) {
+ final ExtLogRecord extRecord = (record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME);
// todo - resource bundle
- record.setLoggerName(getName());
+ extRecord.setLoggerName(getName());
final Filter filter = this.filter;
try {
- if (filter != null && ! filter.isLoggable(record)) {
+ if (filter != null && ! filter.isLoggable(extRecord)) {
return;
}
} catch (VirtualMachineError e) {
@@ -278,7 +280,7 @@
final Handler[] handlers = current.handlers;
if (handlers != null) {
for (Handler handler : handlers) try {
- handler.publish(record);
+ handler.publish(extRecord);
} catch (VirtualMachineError e) {
throw e;
} catch (Throwable t) {
@@ -313,7 +315,7 @@
if (FINER_INT < effectiveLevel) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "ENTRY");
+ final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "ENTRY", LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
doLog(rec);
@@ -323,7 +325,7 @@
if (FINER_INT < effectiveLevel) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "ENTRY {0}");
+ final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "ENTRY {0}", LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setParameters(new Object[] { param1 });
@@ -338,7 +340,7 @@
for (int i = 0; i < params.length; i++) {
builder.append(" {").append(i).append('}');
}
- final ExtLogRecord rec = new ExtLogRecord(Level.FINER, builder.toString());
+ final ExtLogRecord rec = new ExtLogRecord(Level.FINER, builder.toString(), LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
if (params != null) rec.setParameters(params);
@@ -349,7 +351,7 @@
if (FINER_INT < effectiveLevel) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "RETURN");
+ final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "RETURN", LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
doLog(rec);
@@ -359,7 +361,7 @@
if (FINER_INT < effectiveLevel) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "RETURN {0}");
+ final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "RETURN {0}", LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setParameters(new Object[] { result });
@@ -370,7 +372,7 @@
if (FINER_INT < effectiveLevel) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "THROW");
+ final ExtLogRecord rec = new ExtLogRecord(Level.FINER, "THROW", LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setThrown(thrown);
@@ -381,49 +383,49 @@
if (SEVERE_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.SEVERE, msg));
+ doLog(new ExtLogRecord(Level.SEVERE, msg, LOGGER_CLASS_NAME));
}
public void warning(final String msg) {
if (WARNING_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.WARNING, msg));
+ doLog(new ExtLogRecord(Level.WARNING, msg, LOGGER_CLASS_NAME));
}
public void info(final String msg) {
if (INFO_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.INFO, msg));
+ doLog(new ExtLogRecord(Level.INFO, msg, LOGGER_CLASS_NAME));
}
public void config(final String msg) {
if (CONFIG_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.CONFIG, msg));
+ doLog(new ExtLogRecord(Level.CONFIG, msg, LOGGER_CLASS_NAME));
}
public void fine(final String msg) {
if (FINE_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.FINE, msg));
+ doLog(new ExtLogRecord(Level.FINE, msg, LOGGER_CLASS_NAME));
}
public void finer(final String msg) {
if (FINER_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.FINER, msg));
+ doLog(new ExtLogRecord(Level.FINER, msg, LOGGER_CLASS_NAME));
}
public void finest(final String msg) {
if (FINEST_INT < effectiveLevel) {
return;
}
- doLog(new ExtLogRecord(Level.FINEST, msg));
+ doLog(new ExtLogRecord(Level.FINEST, msg, LOGGER_CLASS_NAME));
}
public void log(final Level level, final String msg) {
@@ -431,7 +433,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- doLog(new ExtLogRecord(level, msg));
+ doLog(new ExtLogRecord(level, msg, LOGGER_CLASS_NAME));
}
public void log(final Level level, final String msg, final Object param1) {
@@ -439,7 +441,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setParameters(new Object[] { param1 });
doLog(rec);
}
@@ -449,7 +451,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
if (params != null) rec.setParameters(params);
doLog(rec);
}
@@ -459,7 +461,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setThrown(thrown);
doLog(rec);
}
@@ -469,7 +471,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
doLog(rec);
@@ -480,7 +482,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setParameters(new Object[] { param1 });
@@ -492,7 +494,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
if (params != null) rec.setParameters(params);
@@ -504,7 +506,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setThrown(thrown);
@@ -516,7 +518,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setResourceBundleName(bundleName);
@@ -528,7 +530,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setResourceBundleName(bundleName);
@@ -541,7 +543,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setResourceBundleName(bundleName);
@@ -554,7 +556,7 @@
if (level.intValue() < effectiveLevel || effectiveLevel == OFF_INT) {
return;
}
- final ExtLogRecord rec = new ExtLogRecord(level, msg);
+ final ExtLogRecord rec = new ExtLogRecord(level, msg, LOGGER_CLASS_NAME);
rec.setSourceClassName(sourceClass);
rec.setSourceMethodName(sourceMethod);
rec.setResourceBundleName(bundleName);
More information about the jboss-svn-commits
mailing list