Log & StdErrLog updates.

+ Removed deprecatd Log methods related to anonymous logging.
+ Added StdErrLog.LEVEL_OFF (and related properties *.LEVEL=OFF support)
This commit is contained in:
Joakim Erdfelt 2012-07-26 10:19:23 -07:00
parent 3cdd533361
commit beaa87e3d2
6 changed files with 60 additions and 188 deletions

View File

@ -13,13 +13,7 @@
package org.eclipse.jetty.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
@ -44,17 +38,16 @@ import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
*/
public class RequestTest
{
private static final Logger LOG = Log.getLogger(RequestTest.class);
private Server _server;
private LocalHttpConnector _connector;
private RequestHandler _handler;
@ -841,7 +834,7 @@ public class RequestTest
File evil_keys = new File("/tmp/keys_mapping_to_zero_2m");
if (!evil_keys.exists())
{
Log.info("testHashDOS skipped");
LOG.info("testHashDOS skipped");
return;
}

View File

@ -179,22 +179,18 @@ public class Log
LOG.debug("Logging to {} via {}", LOG, log_class.getName());
}
}
public static void setLog(Logger log)
{
Log.LOG = log;
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static Logger getLog()
{
initialized();
return LOG;
}
public static void setLog(Logger log)
{
Log.LOG = log;
}
/**
* Get the root logger.
* @return the root logger
@ -247,165 +243,6 @@ public class Log
}
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void debug(Throwable th)
{
if (!isDebugEnabled())
return;
LOG.debug(EXCEPTION, th);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void debug(String msg)
{
if (!initialized())
return;
LOG.debug(msg);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void debug(String msg, Object arg)
{
if (!initialized())
return;
LOG.debug(msg, arg);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void debug(String msg, Object arg0, Object arg1)
{
if (!initialized())
return;
LOG.debug(msg, arg0, arg1);
}
/**
* Ignore an exception unless trace is enabled.
* This works around the problem that log4j does not support the trace level.
* @param thrown the Throwable to ignore
*/
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void ignore(Throwable thrown)
{
if (!initialized())
return;
LOG.ignore(thrown);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void info(String msg)
{
if (!initialized())
return;
LOG.info(msg);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void info(String msg, Object arg)
{
if (!initialized())
return;
LOG.info(msg, arg);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void info(String msg, Object arg0, Object arg1)
{
if (!initialized())
return;
LOG.info(msg, arg0, arg1);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static boolean isDebugEnabled()
{
if (!initialized())
return false;
return LOG.isDebugEnabled();
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void warn(String msg)
{
if (!initialized())
return;
LOG.warn(msg);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void warn(String msg, Object arg)
{
if (!initialized())
return;
LOG.warn(msg, arg);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void warn(String msg, Object arg0, Object arg1)
{
if (!initialized())
return;
LOG.warn(msg, arg0, arg1);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void warn(String msg, Throwable th)
{
if (!initialized())
return;
LOG.warn(msg, th);
}
/**
* @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
*/
@Deprecated
public static void warn(Throwable th)
{
if (!initialized())
return;
LOG.warn(EXCEPTION, th);
}
/**
* Obtain a named Logger based on the fully qualified class name.
*

View File

@ -73,6 +73,7 @@ public class StdErrLog extends AbstractLogger
public static final int LEVEL_DEBUG = 1;
public static final int LEVEL_INFO = 2;
public static final int LEVEL_WARN = 3;
public static final int LEVEL_OFF = 10;
private int _level = LEVEL_INFO;
// Level that this Logger was configured as (remembered in special case of .setDebugEnabled())
@ -86,6 +87,16 @@ public class StdErrLog extends AbstractLogger
// The abbreviated log name (used by default, unless _long is specified)
private final String _abbrevname;
private boolean _hideStacks = false;
public static StdErrLog getLogger(Class<?> clazz)
{
Logger log = Log.getLogger(clazz);
if (log instanceof StdErrLog)
{
return (StdErrLog)log;
}
throw new RuntimeException("Logger for " + clazz + " is not of type StdErrLog");
}
public StdErrLog()
{
@ -181,8 +192,12 @@ public class StdErrLog extends AbstractLogger
{
return LEVEL_WARN;
}
else if ("OFF".equalsIgnoreCase(levelStr))
{
return LEVEL_OFF;
}
System.err.println("Unknown StdErrLog level [" + levelSegment + "]=[" + levelStr + "], expecting only [ALL, DEBUG, INFO, WARN] as values.");
System.err.println("Unknown StdErrLog level [" + levelSegment + "]=[" + levelStr + "], expecting only [ALL, DEBUG, INFO, WARN, OFF] as values.");
return -1;
}

View File

@ -38,7 +38,7 @@ public class LifeCycleListenerTest
try
{
((StdErrLog)Log.getLogger(AbstractLifeCycle.class)).setHideStacks(true);
StdErrLog.getLogger(AbstractLifeCycle.class).setHideStacks(true);
lifecycle.start();
assertTrue(false);
}
@ -49,11 +49,9 @@ public class LifeCycleListenerTest
}
finally
{
((StdErrLog)Log.getLogger(AbstractLifeCycle.class)).setHideStacks(false);
StdErrLog.getLogger(AbstractLifeCycle.class).setHideStacks(false);
}
lifecycle.setCause(null);
((StdErrLog)Log.getLog()).setHideStacks(false);
lifecycle.start();

View File

@ -27,7 +27,6 @@ public class LogTest
private static Logger originalLogger;
private static Map<String,Logger> originalLoggers;
@SuppressWarnings("deprecation")
@BeforeClass
public static void rememberOriginalLogger()
{
@ -81,7 +80,6 @@ public class LogTest
assertNamedLogging(Green.class);
}
@SuppressWarnings("deprecation")
private void assertNamedLogging(Class<?> clazz)
{
Logger lc = Log.getLogger(clazz);

View File

@ -359,6 +359,31 @@ public class StdErrLogTest
output.assertContains("java.lang.Throwable: scene lost");
}
/**
* Tests {@link StdErrLog#LEVEL_OFF} filtering.
*/
@Test
public void testOffFiltering() throws UnsupportedEncodingException
{
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName(),new Properties());
log.setHideStacks(false);
log.setLevel(StdErrLog.LEVEL_OFF);
StdErrCapture output = new StdErrCapture(log);
// Various logging events
log.debug("Squelch");
log.debug("Squelch", new RuntimeException("Squelch"));
log.info("Squelch");
log.info("Squelch", new IllegalStateException("Squelch"));
log.warn("Squelch");
log.warn("Squelch", new Exception("Squelch"));
log.ignore(new Throwable("Squelch"));
// Validate Output
output.assertNotContains("Squelch");
}
/**
* Tests StdErrLog.debug() methods with level filtering.
* <p>
@ -454,6 +479,9 @@ public class StdErrLogTest
log.setLevel(StdErrLog.LEVEL_WARN);
Assert.assertThat("log.level(warn).isDebugEnabled", log.isDebugEnabled(), is(false));
log.setLevel(StdErrLog.LEVEL_OFF);
Assert.assertThat("log.level(off).isDebugEnabled", log.isDebugEnabled(), is(false));
}
@Test
@ -473,6 +501,9 @@ public class StdErrLogTest
log.setLevel(StdErrLog.LEVEL_WARN);
Assert.assertThat("log.level(warn).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_WARN));
log.setLevel(StdErrLog.LEVEL_OFF);
Assert.assertThat("log.level(off).getLevel()", log.getLevel(), is(StdErrLog.LEVEL_OFF));
}
@Test
@ -511,7 +542,7 @@ public class StdErrLogTest
Assert.assertThat("Logger.name", log.getName(), is("jetty"));
// Pass null as child reference, should return parent logger
Logger log2 = log.getLogger(null);
Logger log2 = log.getLogger((String)null);
Assert.assertThat("Logger.child.name", log2.getName(), is("jetty"));
Assert.assertSame("Should have returned same logger", log2, log);
}