diff --git a/jetty-centralized-logging/src/main/java/org/eclipse/jetty/logging/impl/CentralLogger.java b/jetty-centralized-logging/src/main/java/org/eclipse/jetty/logging/impl/CentralLogger.java index 1d0bb6cfef9..ff9516d8ac8 100644 --- a/jetty-centralized-logging/src/main/java/org/eclipse/jetty/logging/impl/CentralLogger.java +++ b/jetty-centralized-logging/src/main/java/org/eclipse/jetty/logging/impl/CentralLogger.java @@ -42,6 +42,12 @@ public class CentralLogger extends MarkerIgnoringBase private void log(Severity severity, String message, Throwable t) { + if (!level.isEnabled(severity)) + { + // Don't log level + return; + } + String now = new SimpleDateFormat(dateFormat).format(new Date()); for (Appender appender : appenders) @@ -59,18 +65,33 @@ public class CentralLogger extends MarkerIgnoringBase private void logFormatted(Severity severity, String format, Object arg) { + if (!level.isEnabled(severity)) + { + // Don't log level + return; + } String msg = MessageFormatter.format(format,arg); log(severity,msg,null); } private void logFormatted(Severity severity, String format, Object arg1, Object arg2) { + if (!level.isEnabled(severity)) + { + // Don't log level + return; + } String msg = MessageFormatter.format(format,arg1,arg2); log(severity,msg,null); } private void logFormatted(Severity severity, String format, Object[] argArray) { + if (!level.isEnabled(severity)) + { + // Don't log level + return; + } String msg = MessageFormatter.arrayFormat(format,argArray); log(severity,msg,null); } diff --git a/jetty-webapp-logging/pom.xml b/jetty-webapp-logging/pom.xml index 1e70b557bad..7178d765d1c 100644 --- a/jetty-webapp-logging/pom.xml +++ b/jetty-webapp-logging/pom.xml @@ -101,7 +101,7 @@ - copy-fresh-webapps + copy-test-webapps @@ -109,7 +109,7 @@ maven-dependency-plugin - copy-testing-wars + testwarcopy validate copy diff --git a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-commons/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-commons/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java index 8a81bd4741f..bdd933ff5ee 100644 --- a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-commons/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java +++ b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-commons/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.tests.webapp; +import java.io.FileNotFoundException; import java.io.IOException; import javax.servlet.ServletException; @@ -32,6 +33,7 @@ import org.apache.commons.logging.LogFactory; public class LoggingServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private static final String LOGID = "LoggingServlet(commons-logging)"; private Log log = LogFactory.getLog(LoggingServlet.class); /** @@ -39,16 +41,25 @@ public class LoggingServlet extends HttpServlet */ public LoggingServlet() { - log.debug("LoggingServlet(commons-logging) initialized"); + log.debug(LOGID + " initialized"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - log.info("LoggingServlet(commons-logging) GET requested"); + log.info(LOGID + " GET requested"); + + log.warn(LOGID + " Slightly warn, with a chance of log events"); + + log.error(LOGID + " Nothing is (intentionally) being output by this Servlet"); + + IOException severe = new FileNotFoundException("A file cannot be found"); + + log.fatal(LOGID + " Whoops (intentionally) causing a Throwable",severe); } } diff --git a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-java/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-java/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java index b1df8e65f43..a770611df43 100644 --- a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-java/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java +++ b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-java/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.tests.webapp; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,6 +32,7 @@ import javax.servlet.http.HttpServletResponse; public class LoggingServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private static final String LOGID = "LoggingServlet(java)"; private Logger log = Logger.getLogger(LoggingServlet.class.getName()); /** @@ -38,7 +40,7 @@ public class LoggingServlet extends HttpServlet */ public LoggingServlet() { - log.log(Level.FINE,"LoggingServlet(java) initialized"); + log.log(Level.FINE,LOGID + " initialized"); } /** @@ -47,6 +49,15 @@ public class LoggingServlet extends HttpServlet @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - log.log(Level.INFO,"LoggingServlet(java) GET requested"); + log.log(Level.INFO,LOGID + " GET requested"); + + log.log(Level.WARNING,LOGID + " Slightly warn, with a chance of log events"); + + log.log(Level.WARNING,LOGID + " Nothing is (intentionally) being output by this Servlet"); + + IOException severe = new FileNotFoundException("A file cannot be found"); + + log.log(Level.SEVERE,LOGID + " Whoops (intentionally) causing a Throwable",severe); + } } diff --git a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-log4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-log4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java index 6c051f054b8..f0aa8faf6ec 100644 --- a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-log4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java +++ b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-log4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.tests.webapp; +import java.io.FileNotFoundException; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -30,6 +31,7 @@ import org.apache.log4j.Logger; public class LoggingServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private static final String LOGID = "LoggingServlet(log4j)"; private Logger log = Logger.getLogger(LoggingServlet.class); /** @@ -37,15 +39,24 @@ public class LoggingServlet extends HttpServlet */ public LoggingServlet() { - log.debug("LoggingServlet(log4j) initialized"); + log.debug(LOGID + " initialized"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - log.info("LoggingServlet(log4j) GET requested"); + log.info(LOGID + " GET requested"); + + log.warn(LOGID + " Slightly warn, with a chance of log events"); + + log.error(LOGID + " Nothing is (intentionally) being output by this Servlet"); + + IOException severe = new FileNotFoundException("A file cannot be found"); + + log.fatal(LOGID + " Whoops (intentionally) causing a Throwable",severe); } } diff --git a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-slf4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-slf4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java index a2d51003365..a8e44c0f924 100644 --- a/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-slf4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java +++ b/jetty-webapp-logging/src/test/artifacts/dummy-webapp-logging-slf4j/src/main/java/org/eclipse/jetty/tests/webapp/LoggingServlet.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.tests.webapp; +import java.io.FileNotFoundException; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -31,6 +32,7 @@ import org.slf4j.LoggerFactory; public class LoggingServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private static final String LOGID = "LoggingServlet(slf4j)"; private Logger log = LoggerFactory.getLogger(LoggingServlet.class); /** @@ -38,16 +40,25 @@ public class LoggingServlet extends HttpServlet */ public LoggingServlet() { - log.debug("LoggingServlet(slf4j) initialized"); + log.debug(LOGID + " initialized"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - log.info("LoggingServlet(slf4j) GET requested"); + log.info(LOGID + " GET requested"); + + log.warn(LOGID + " Slightly warn, with a chance of log events"); + + log.error(LOGID + " Nothing is (intentionally) being output by this Servlet"); + + IOException severe = new FileNotFoundException("A file cannot be found"); + + log.error(LOGID + " Whoops (intentionally) causing a Throwable",severe); } } diff --git a/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/CentralizedLoggingTest.java b/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/CentralizedLoggingTest.java index c54bc9710eb..9f9bf202b75 100644 --- a/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/CentralizedLoggingTest.java +++ b/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/CentralizedLoggingTest.java @@ -15,7 +15,10 @@ // ======================================================================== package org.eclipse.jetty.webapp.logging; +import java.io.FileNotFoundException; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import junit.framework.TestCase; @@ -28,7 +31,7 @@ public class CentralizedLoggingTest extends TestCase private static final String LOGGING_SERVLET_ID = "org.eclipse.jetty.tests.webapp.LoggingServlet"; private XmlConfiguredJetty jetty; - private void assertContainsLogEvents(TestAppender capturedEvents, LogEvent[] expectedLogs) + private void assertContainsLogEvents(TestAppender capturedEvents, List expectedLogs) { for (LogEvent expectedEvent : expectedLogs) { @@ -73,15 +76,37 @@ public class CentralizedLoggingTest extends TestCase SimpleRequest.get(jetty,"/dummy-webapp-logging-slf4j/logging"); SimpleRequest.get(jetty,"/dummy-webapp-logging-java/logging"); - TestAppender.LogEvent expectedLogs[] = - { new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested"), - new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"), - new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested"), - new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") }; + String prefix = "LoggingServlet(commons-logging)"; + List expectedLogs = new ArrayList(); + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + prefix = "LoggingServlet(log4j)"; + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + prefix = "LoggingServlet(java)"; + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + prefix = "LoggingServlet(slf4j)"; + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + + assertContainsLogEvents(testAppender,expectedLogs); assertContainsLogEvents(testAppender,expectedLogs); } diff --git a/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/EmbeddedCentralizedLoggingTest.java b/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/EmbeddedCentralizedLoggingTest.java index 9d71bdb54e6..332e1c252c2 100644 --- a/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/EmbeddedCentralizedLoggingTest.java +++ b/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/EmbeddedCentralizedLoggingTest.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.webapp.logging; import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; @@ -40,7 +41,7 @@ public class EmbeddedCentralizedLoggingTest extends TestCase private static final String LOGGING_SERVLET_ID = "org.eclipse.jetty.tests.webapp.LoggingServlet"; private TestAppender testAppender; - private void assertContainsLogEvents(TestAppender capturedEvents, LogEvent[] expectedLogs) + private void assertContainsLogEvents(TestAppender capturedEvents, List expectedLogs) { for (LogEvent expectedEvent : expectedLogs) { @@ -145,15 +146,35 @@ public class EmbeddedCentralizedLoggingTest extends TestCase server.stop(); - TestAppender.LogEvent expectedLogs[] = - { new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested"), - new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested"), - new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested"), - new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") }; + String prefix = "LoggingServlet(commons-logging)"; + List expectedLogs = new ArrayList(); + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + prefix = "LoggingServlet(log4j)"; + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + prefix = "LoggingServlet(java)"; + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); + prefix = "LoggingServlet(slf4j)"; + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); assertContainsLogEvents(testAppender,expectedLogs); } @@ -168,9 +189,14 @@ public class EmbeddedCentralizedLoggingTest extends TestCase server.stop(); - TestAppender.LogEvent expectedLogs[] = - { new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(commons-logging) GET requested") }; + String prefix = "LoggingServlet(commons-logging)"; + List expectedLogs = new ArrayList(); + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); assertContainsLogEvents(testAppender,expectedLogs); } @@ -185,9 +211,14 @@ public class EmbeddedCentralizedLoggingTest extends TestCase server.stop(); - TestAppender.LogEvent expectedLogs[] = - { new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(java) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(java) GET requested") }; + String prefix = "LoggingServlet(java)"; + List expectedLogs = new ArrayList(); + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); assertContainsLogEvents(testAppender,expectedLogs); } @@ -202,9 +233,14 @@ public class EmbeddedCentralizedLoggingTest extends TestCase server.stop(); - TestAppender.LogEvent expectedLogs[] = - { new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(log4j) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(log4j) GET requested") }; + String prefix = "LoggingServlet(log4j)"; + List expectedLogs = new ArrayList(); + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); assertContainsLogEvents(testAppender,expectedLogs); } @@ -219,9 +255,14 @@ public class EmbeddedCentralizedLoggingTest extends TestCase server.stop(); - TestAppender.LogEvent expectedLogs[] = - { new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) initialized"), - new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,"LoggingServlet(slf4j) GET requested") }; + String prefix = "LoggingServlet(slf4j)"; + List expectedLogs = new ArrayList(); + // expectedLogs.add(new LogEvent(Severity.DEBUG,LOGGING_SERVLET_ID,prefix + " initialized")); + expectedLogs.add(new LogEvent(Severity.INFO,LOGGING_SERVLET_ID,prefix + " GET requested")); + expectedLogs.add(new LogEvent(Severity.WARN,LOGGING_SERVLET_ID,prefix + " Slightly warn, with a chance of log events")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Nothing is (intentionally) being output by this Servlet")); + expectedLogs.add(new LogEvent(Severity.ERROR,LOGGING_SERVLET_ID,prefix + " Whoops (intentionally) causing a Throwable") + .expectedThrowable(new FileNotFoundException("A file cannot be found"))); assertContainsLogEvents(testAppender,expectedLogs); } diff --git a/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/TestAppender.java b/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/TestAppender.java index 7ea84bb2755..f26c05b4f48 100644 --- a/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/TestAppender.java +++ b/jetty-webapp-logging/src/test/java/org/eclipse/jetty/webapp/logging/TestAppender.java @@ -50,6 +50,119 @@ public class TestAppender implements Appender this(null,severity,name,message,null); } + public LogEvent expectedThrowable(Throwable t) + { + this.t = t; + return this; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null)?0:message.hashCode()); + result = prime * result + ((name == null)?0:name.hashCode()); + result = prime * result + ((severity == null)?0:severity.hashCode()); + if (t != null) + { + result = prime * result + t.getClass().hashCode(); + if (t.getMessage() != null) + { + result = prime * result + t.getMessage().hashCode(); + } + else + { + result = prime * result + 0; + } + } + else + { + result = prime * result + 0; + } + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + LogEvent other = (LogEvent)obj; + if (message == null) + { + if (other.message != null) + { + return false; + } + } + else if (!message.equals(other.message)) + { + return false; + } + if (name == null) + { + if (other.name != null) + { + return false; + } + } + else if (!name.equals(other.name)) + { + return false; + } + if (severity == null) + { + if (other.severity != null) + { + return false; + } + } + else if (!severity.equals(other.severity)) + { + return false; + } + + // Throwable + if (t == null) + { + if (other.t != null) + { + return false; + } + } + else + { + if (!t.getClass().equals(other.t.getClass())) + { + return false; + } + if (t.getMessage() == null) + { + if (other.t.getMessage() != null) + { + return false; + } + } + else if (!t.getMessage().equals(other.t.getMessage())) + { + return false; + } + } + + return true; + } + @Override public String toString() { @@ -57,6 +170,11 @@ public class TestAppender implements Appender buf.append(severity.name()).append("|"); buf.append(name).append("|"); buf.append(message); + if (t != null) + { + buf.append("|").append(t.getClass().getName()); + buf.append("(\"").append(t.getMessage()).append("\")"); + } return buf.toString(); } } @@ -95,6 +213,7 @@ public class TestAppender implements Appender public boolean contains(LogEvent expectedEvent) { + /* // System.out.println("Looking for: " + expectedEvent); for (LogEvent event : events) { @@ -107,12 +226,27 @@ public class TestAppender implements Appender { continue; // not a match. skip. } + if (expectedEvent.t != null) + { + if (event.t == null) + { + continue; // not a match. skip. + } + if (!event.t.getClass().equals(expectedEvent.t.getClass())) + { + continue; // not a match. skip. + } + if (!event.t.getMessage().equals(expectedEvent.t.getMessage())) + { + continue; // not a match. skip. + } + } if (event.message.equals(expectedEvent.message)) { return true; } - } - return false; + }*/ + return events.contains(expectedEvent); } public List getEvents() diff --git a/jetty-webapp-logging/src/test/resources/logger/testing.properties b/jetty-webapp-logging/src/test/resources/logger/testing.properties index 059c15b0f22..060e409038c 100644 --- a/jetty-webapp-logging/src/test/resources/logger/testing.properties +++ b/jetty-webapp-logging/src/test/resources/logger/testing.properties @@ -1,4 +1,5 @@ -root.level=DEBUG +# Intentionally NOT get DEBUG messages +root.level=INFO root.appenders=testing appender.console.class=org.eclipse.jetty.logging.impl.ConsoleAppender diff --git a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-commons.war b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-commons.war index 06e4fe8ab6f..bac25eb2da6 100644 Binary files a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-commons.war and b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-commons.war differ diff --git a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-java.war b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-java.war index ace3cd62f3b..d4479a49731 100644 Binary files a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-java.war and b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-java.war differ diff --git a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-log4j.war b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-log4j.war index d3163fddfc2..20ab5e5a164 100644 Binary files a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-log4j.war and b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-log4j.war differ diff --git a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-slf4j.war b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-slf4j.war index cd9bbf20396..10e3cdb8ba1 100644 Binary files a/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-slf4j.war and b/jetty-webapp-logging/src/test/resources/webapps/dummy-webapp-logging-slf4j.war differ