478372 - JavaUtilLog setSourceClass and setSourceMethod

This commit is contained in:
Greg Wilkins 2015-09-25 12:45:46 +10:00
parent 3e242bef16
commit dcb4c0d0a8
2 changed files with 64 additions and 17 deletions

View File

@ -1,7 +1,7 @@
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.LEVEL=INFO #org.eclipse.jetty.util.log.SOURCE=true
org.eclipse.jetty.LEVEL=DEBUG
org.eclipse.jetty.STACKS=true org.eclipse.jetty.STACKS=true
org.eclipse.jetty.SOURCE=false
#org.eclipse.jetty.STACKS=false #org.eclipse.jetty.STACKS=false
#org.eclipse.jetty.server.LEVEL=DEBUG #org.eclipse.jetty.server.LEVEL=DEBUG
#org.eclipse.jetty.io.LEVEL=DEBUG #org.eclipse.jetty.io.LEVEL=DEBUG

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.util.log; package org.eclipse.jetty.util.log;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord;
/** /**
* <p> * <p>
@ -26,12 +27,29 @@ import java.util.logging.Level;
* </p> * </p>
* *
* <p> * <p>
* You can also set the logger level using <a href="http://java.sun.com/j2se/1.5.0/docs/guide/logging/overview.html"> * You can also set the logger level using <a href="http://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html">
* standard java.util.logging configuration</a>. * standard java.util.logging configuration</a>.
* </p> * </p>
*
* Configuration Properties:
* <dl>
* <dt>org.eclipse.jetty.util.log.javautil.SOURCE=(true|false)</dt>
* <dd>Set the LogRecord source class and method for JavaUtilLog.<br>
* Default: true
* </dd>
* <dt>org.eclipse.jetty.util.log.SOURCE=(true|false)</dt>
* <dd>Set the LogRecord source class and method for all Loggers.<br>
* Default: depends on Logger class
* </dd>
* </dl>
*/ */
public class JavaUtilLog extends AbstractLogger public class JavaUtilLog extends AbstractLogger
{ {
private final static String THIS_CLASS= JavaUtilLog.class.getName();
private final static boolean __source =
Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.SOURCE",
Log.__props.getProperty("org.eclipse.jetty.util.log.javautil.SOURCE","true")));
private Level configuredLevel; private Level configuredLevel;
private java.util.logging.Logger _logger; private java.util.logging.Logger _logger;
@ -47,6 +65,8 @@ public class JavaUtilLog extends AbstractLogger
{ {
_logger.setLevel(Level.FINE); _logger.setLevel(Level.FINE);
} }
configuredLevel = _logger.getLevel(); configuredLevel = _logger.getLevel();
} }
@ -55,36 +75,63 @@ public class JavaUtilLog extends AbstractLogger
return _logger.getName(); return _logger.getName();
} }
protected void log(Level level,String msg,Throwable thrown)
{
LogRecord record = new LogRecord(level,msg);
if (thrown!=null)
record.setThrown(thrown);
record.setLoggerName(_logger.getName());
if (__source)
{
StackTraceElement[] stack = new Throwable().getStackTrace();
for (int i=0;i<stack.length;i++)
{
StackTraceElement e=stack[i];
if (!e.getClassName().equals(THIS_CLASS))
{
record.setSourceClassName(e.getClassName());
record.setSourceMethodName(e.getMethodName());
break;
}
}
}
_logger.log(record);
}
public void warn(String msg, Object... args) public void warn(String msg, Object... args)
{ {
if (_logger.isLoggable(Level.WARNING)) if (_logger.isLoggable(Level.WARNING))
_logger.log(Level.WARNING,format(msg,args)); log(Level.WARNING,format(msg,args),null);
} }
public void warn(Throwable thrown) public void warn(Throwable thrown)
{ {
warn("", thrown); if (_logger.isLoggable(Level.WARNING))
log(Level.WARNING,"",thrown);
} }
public void warn(String msg, Throwable thrown) public void warn(String msg, Throwable thrown)
{ {
_logger.log(Level.WARNING, msg, thrown); if (_logger.isLoggable(Level.WARNING))
log(Level.WARNING,msg,thrown);
} }
public void info(String msg, Object... args) public void info(String msg, Object... args)
{ {
if (_logger.isLoggable(Level.INFO)) if (_logger.isLoggable(Level.INFO))
_logger.log(Level.INFO, format(msg, args)); log(Level.INFO, format(msg, args),null);
} }
public void info(Throwable thrown) public void info(Throwable thrown)
{ {
info("", thrown); if (_logger.isLoggable(Level.INFO))
log(Level.INFO, "",thrown);
} }
public void info(String msg, Throwable thrown) public void info(String msg, Throwable thrown)
{ {
_logger.log(Level.INFO, msg, thrown); if (_logger.isLoggable(Level.INFO))
log(Level.INFO,msg,thrown);
} }
public boolean isDebugEnabled() public boolean isDebugEnabled()
@ -108,23 +155,25 @@ public class JavaUtilLog extends AbstractLogger
public void debug(String msg, Object... args) public void debug(String msg, Object... args)
{ {
if (_logger.isLoggable(Level.FINE)) if (_logger.isLoggable(Level.FINE))
_logger.log(Level.FINE,format(msg, args)); log(Level.FINE,format(msg, args),null);
} }
public void debug(String msg, long arg) public void debug(String msg, long arg)
{ {
if (_logger.isLoggable(Level.FINE)) if (_logger.isLoggable(Level.FINE))
_logger.log(Level.FINE,format(msg, arg)); log(Level.FINE,format(msg, arg),null);
} }
public void debug(Throwable thrown) public void debug(Throwable thrown)
{ {
debug("", thrown); if (_logger.isLoggable(Level.FINE))
log(Level.FINE,"",thrown);
} }
public void debug(String msg, Throwable thrown) public void debug(String msg, Throwable thrown)
{ {
_logger.log(Level.FINE, msg, thrown); if (_logger.isLoggable(Level.FINE))
log(Level.FINE,msg,thrown);
} }
/** /**
@ -137,10 +186,8 @@ public class JavaUtilLog extends AbstractLogger
public void ignore(Throwable ignored) public void ignore(Throwable ignored)
{ {
if (Log.isIgnored()) if (_logger.isLoggable(Level.WARNING))
{ log(Level.WARNING,Log.IGNORED,ignored);
warn(Log.IGNORED, ignored);
}
} }
private String format(String msg, Object... args) private String format(String msg, Object... args)