Bug 362251 - Allow Log.setDebugEnabled(true) to override on configured StdErrLog levels
This commit is contained in:
parent
903c5dcbbf
commit
962e4afda9
|
@ -38,7 +38,8 @@ import org.eclipse.jetty.util.DateCache;
|
|||
public class StdErrLog implements Logger
|
||||
{
|
||||
private static DateCache _dateCache;
|
||||
|
||||
private static Properties __props = System.getProperties();
|
||||
|
||||
private final static boolean __source = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.SOURCE",
|
||||
System.getProperty("org.eclipse.jetty.util.log.stderr.SOURCE","false")));
|
||||
private final static boolean __long = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.stderr.LONG","false"));
|
||||
|
@ -78,6 +79,8 @@ public class StdErrLog implements Logger
|
|||
public static final int LEVEL_WARN = 3;
|
||||
|
||||
private int _level = LEVEL_INFO;
|
||||
// Level that this Logger was configured as (remembered in special case of .setDebugEnabled())
|
||||
private int _configuredLevel;
|
||||
private PrintStream _stderr = System.err;
|
||||
private boolean _source = __source;
|
||||
// Print the long form names, otherwise use abbreviated
|
||||
|
@ -95,14 +98,16 @@ public class StdErrLog implements Logger
|
|||
|
||||
public StdErrLog(String name)
|
||||
{
|
||||
this(name,System.getProperties());
|
||||
this(name,__props);
|
||||
}
|
||||
|
||||
public StdErrLog(String name, Properties props)
|
||||
{
|
||||
__props = props;
|
||||
this._name = name == null?"":name;
|
||||
this._abbrevname = condensePackageString(this._name);
|
||||
this._level = getLoggingLevel(props,this._name);
|
||||
this._configuredLevel = this._level;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -319,18 +324,36 @@ public class StdErrLog implements Logger
|
|||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #setLevel(int)} instead.
|
||||
* Legacy interface where a programmatic configuration of the logger level
|
||||
* is done as a wholesale approach.
|
||||
*/
|
||||
@Deprecated
|
||||
public void setDebugEnabled(boolean enabled)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
_level = LEVEL_DEBUG;
|
||||
synchronized (__loggers)
|
||||
{
|
||||
this._level = LEVEL_DEBUG;
|
||||
|
||||
// Boot stomp all cached log levels to DEBUG
|
||||
for(StdErrLog log: __loggers.values())
|
||||
{
|
||||
log._level = LEVEL_DEBUG;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_level = LEVEL_INFO;
|
||||
synchronized (__loggers)
|
||||
{
|
||||
this._level = this._configuredLevel;
|
||||
|
||||
// restore all cached log configured levels
|
||||
for(StdErrLog log: __loggers.values())
|
||||
{
|
||||
log._level = log._configuredLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -637,6 +660,11 @@ public class StdErrLog implements Logger
|
|||
}
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
public static void setProperties(Properties props)
|
||||
{
|
||||
__props = props;
|
||||
}
|
||||
|
||||
public void ignore(Throwable ignored)
|
||||
{
|
||||
|
|
|
@ -510,4 +510,50 @@ public class StdErrLogTest
|
|||
Assert.assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfiguredAndSetDebugEnabled()
|
||||
{
|
||||
Properties props = new Properties();
|
||||
props.setProperty("org.eclipse.jetty.util.LEVEL","WARN");
|
||||
props.setProperty("org.eclipse.jetty.io.LEVEL", "WARN");
|
||||
|
||||
StdErrLog root = new StdErrLog("", props);
|
||||
assertLevel(root,StdErrLog.LEVEL_INFO); // default
|
||||
|
||||
StdErrLog log = (StdErrLog)root.getLogger(StdErrLogTest.class.getName());
|
||||
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
|
||||
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
|
||||
|
||||
// Boot stomp it all to debug
|
||||
root.setDebugEnabled(true);
|
||||
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(true));
|
||||
assertLevel(log,StdErrLog.LEVEL_DEBUG); // as stomped
|
||||
|
||||
// Restore configured
|
||||
root.setDebugEnabled(false);
|
||||
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
|
||||
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
|
||||
}
|
||||
|
||||
private void assertLevel(StdErrLog log, int expectedLevel)
|
||||
{
|
||||
Assert.assertThat("Log[" + log.getName() + "].level",levelToString(log.getLevel()),is(levelToString(expectedLevel)));
|
||||
}
|
||||
|
||||
private String levelToString(int level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case StdErrLog.LEVEL_ALL:
|
||||
return "ALL";
|
||||
case StdErrLog.LEVEL_DEBUG:
|
||||
return "DEBUG";
|
||||
case StdErrLog.LEVEL_INFO:
|
||||
return "INFO";
|
||||
case StdErrLog.LEVEL_WARN:
|
||||
return "WARN";
|
||||
default:
|
||||
return Integer.toString(level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue