From beaa87e3d2de9321fe47cb6c9ea13b467decdac2 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 26 Jul 2012 10:19:23 -0700 Subject: [PATCH] Log & StdErrLog updates. + Removed deprecatd Log methods related to anonymous logging. + Added StdErrLog.LEVEL_OFF (and related properties *.LEVEL=OFF support) --- .../org/eclipse/jetty/server/RequestTest.java | 15 +- .../java/org/eclipse/jetty/util/log/Log.java | 175 +----------------- .../org/eclipse/jetty/util/log/StdErrLog.java | 17 +- .../util/component/LifeCycleListenerTest.java | 6 +- .../org/eclipse/jetty/util/log/LogTest.java | 2 - .../eclipse/jetty/util/log/StdErrLogTest.java | 33 +++- 6 files changed, 60 insertions(+), 188 deletions(-) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java index 8426e402d22..67438cf31d9 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java @@ -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; } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java index d56a289df10..9f440c71bae 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java @@ -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. * diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java index ede2ba59fb0..5551348dc78 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java @@ -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; } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/component/LifeCycleListenerTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/component/LifeCycleListenerTest.java index 7a7a915ccf6..ee776c1aec8 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/component/LifeCycleListenerTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/component/LifeCycleListenerTest.java @@ -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(); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/log/LogTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/log/LogTest.java index 8d3a3617c29..f1ec678f651 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/log/LogTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/log/LogTest.java @@ -27,7 +27,6 @@ public class LogTest private static Logger originalLogger; private static Map 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); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java index 3789f71ee95..5134a72bc34 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java @@ -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. *

@@ -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); }