/** * get or create the logger */ public TestLogger getLogger(Class<?> name) { return getLogger(name.getName()); }
/** * get or create the logger */ public TestLogger getLogger(Class<?> name) { return getLogger(name.getName()); }
public void testDelegateToAMockingLibrary() { Logger mockLogger = Mockito.mock(Logger.class); TestLoggerFactory f = Settings.instance() .printingEnabled(false) .delegate("john", mockLogger) .buildLogging(); TestLogger log = f.getLogger("john"); log.error("anError"); Mockito.verify(mockLogger).error("anError"); } }
@Test public void testLogMessageMatching() { // enable info logging because only error is enabled by default TestLoggerFactory loggerFactory = Settings.instance().enable(LogLevel.InfoLevel) .buildLogging(); TestLogger logger = loggerFactory.getLogger(this.getClass()); logger.info("Line1" + System.lineSeparator() + "Line2"); Pattern pattern = Pattern.compile("Line1.*", Pattern.DOTALL); boolean found = false; for (LogMessage l : logger.lines()) { if (pattern.matcher(l.text).matches()) found = true; } assert (found); }
public void testWarnIsDisabledByDefault() { TestLoggerFactory f = Settings.instance().buildLogging(); TestLogger log = f.getLogger("john"); assert (!log.isWarnEnabled()); }
public void testErrorIsEnabledByDefault() { Settings settings = Settings.instance(); TestLoggerFactory f = new TestLoggerFactory(settings); TestLogger log = f.getLogger("john"); assert (log.isErrorEnabled()); }
/** * In the previous examples we've seen logging captured using either the TestLogger or by replacing the system wide * logging using System.errOut/setErr. Sometimes however neither of these approaches is what we want and injecting * a mock logger is more useful. * <p> * This example uses mock to perform an "ordered" verification. */ @Test public void demoDelegatingToMockito() throws Exception { // we'll hook the mock up to the logging framework Logger mockLogger = Mockito.mock(Logger.class); // setup the logging impl so that logging to the logger "MyLogger" is directed at the mock TestLoggerFactory loggerFactory = Settings.instance() .delegate("MyLogger", mockLogger).buildLogging(); // do some work TestLogger logger = loggerFactory.getLogger("MyLogger"); logger.info("Hello Johnny"); logger.info("Hello Gracie"); // check that the mock was called in the right order InOrder inOrder = Mockito.inOrder(mockLogger); inOrder.verify(mockLogger).info("Hello Johnny"); inOrder.verify(mockLogger).info("Hello Gracie"); }
TestLogger logger = loggerFactory.getLogger(this.getClass());
/** * trivial demo of calling the logging functions on a logger and verifying what was logged. */ @Test public void demoConsoleLoggingUsingConstructor() throws Exception { TestLoggerFactory loggerFactory = Settings.instance() .enableAll() // necessary as by default only ErrorLevel is enabled .buildLogging(); TestLogger logger = loggerFactory.getLogger("MyLogger"); // expect to see some console logging logger.info("Hello World!"); // verification - the TestLogger instance will have collected all the enabled logging was sent to the loggers TestLogger testLogger = loggerFactory.getLogger("MyLogger"); Assert.assertTrue(testLogger.matches("Hello World!")); }
/** * trivial demo of calling the logging functions on a logger and verifying what was logged. */ @Test public void demoConsoleLoggingUsingBuilder() throws Exception { TestLoggerFactory loggerFactory = Settings.instance() .enableAll() // necessary as by default only ErrorLevel is enabled .buildLogging(); TestLogger logger = loggerFactory.getLogger("MyLogger"); // expect to see some console logging logger.info("Hello World!"); // verification - the TestLogger instance will have collected all the enabled logging was sent to the loggers TestLogger testLogger = loggerFactory.getLogger("MyLogger"); Assert.assertTrue(testLogger.matches("Hello World!")); }
public void testErrorCanBeDisabled() { TestLoggerFactory f = Settings.instance().disable(LogLevel.ErrorLevel).buildLogging(); TestLogger log = f.getLogger("john"); assert (!log.isErrorEnabled()); }
public void testWarnCanBeEnabled() { TestLoggerFactory f = Settings.instance().enable(LogLevel.WarnLevel).buildLogging(); TestLogger log = f.getLogger("john"); assert (log.isErrorEnabled()); }
public void testConsolePrintingCanBeDisabledButLinesAreStillRecorded() { StringPrintStream console = StringPrintStream.newStream(); PrintStream old = System.err; System.setErr(console); // << have to interfere with the system for this test try { TestLoggerFactory f = Settings.instance().printingEnabled(false).buildLogging(); TestLogger log = f.getLogger("john"); log.error("anError"); assert (!console.contains("anError")); assert (log.contains("anError")); } finally { System.setErr(old); } }
public void testPrintSuppressionsAffectsPrintStreamAndNotLogging() { StringPrintStream ps = StringPrintStream.newStream(); TestLoggerFactory f = Settings.instance() .redirectPrintStream(LogLevel.ErrorLevel, ps) .suppressPrinting(".*suppressPrinting-me.*") .buildLogging(); TestLogger log = f.getLogger("john"); String ShouldBeLogged = "printme"; String ShouldBePrintSuppressed = "suppressPrinting-me <<" + System.lineSeparator() + " dont print"; log.error(ShouldBeLogged); log.error(ShouldBePrintSuppressed); assert (ps.toString().contains(ShouldBeLogged)); assert (log.contains(ShouldBeLogged)); assert (!ps.toString().contains(ShouldBePrintSuppressed)); assert (log.contains(ShouldBePrintSuppressed)); }
@Test public void testBasicDemo() throws Exception { TestLoggerFactory loggerFactory = new TestLoggerFactory(); Example sut = new Example(loggerFactory); sut.doLogging(); TestLogger logger = loggerFactory.getLogger(Example.class); assertTrue(logger.matches(".*Hello.*")); } }
.buildLogging(); TestLogger logger = loggerFactory.getLogger(this.getClass()); logger.info("Info should be printed to Err stream"); logger.error("Error should be printed to Out stream");
public void testPrintStreamCanBeOverridden() { StringPrintStream ps = StringPrintStream.newStream(); TestLoggerFactory f = Settings.instance() .redirectPrintStream(LogLevel.ErrorLevel, ps).buildLogging(); TestLogger log = f.getLogger("john"); log.error("anError"); assert (ps.contains("anError")); assert (log.contains("anError")); }
@Test public void testMultiLineMatching() { // enable info logging because only error is enabled by default TestLoggerFactory loggerFactory = Settings.instance().enable(LogLevel.InfoLevel).buildLogging(); TestLogger logger = loggerFactory.getLogger(this.getClass()); // do some multiline logging logger.info("Line1" + System.lineSeparator() + "Line2"); // this one does match multiline logging assert (loggerFactory.matches("Line1.*")); // using DOTALL we can match multiline Pattern regex = Pattern.compile("Line1.*", Pattern.DOTALL); Pattern regexNoMatch = Pattern.compile("NOMATCH"); assert (loggerFactory.matches(regex)); assert (!loggerFactory.matches(regexNoMatch)); assert (loggerFactory.matches(LogLevel.InfoLevel, regex)); assert (!loggerFactory.matches(LogLevel.ErrorLevel, regex)); assert (logger.matches(regex)); assert (!logger.matches(regexNoMatch)); assert (logger.matches(LogLevel.InfoLevel, regex)); assert (!logger.matches(LogLevel.ErrorLevel, regex)); }
public void testLoggingDefaults() { StringPrintStream console = StringPrintStream.newStream(); PrintStream old = System.err; System.setErr(console); try { TestLoggerFactory f = new TestLoggerFactory(); TestLogger log = f.getLogger("john"); log.error("anError"); log.info("someInfo"); assert (console.contains("anError")); assert (!console.contains("someInfo")); assert (log.contains("anError")); assert (!log.contains("someInfo")); assert (console.matches(".*anError.*")); assert (!console.matches(".*someInfo.*")); assert (log.matches(".*anError.*")); assert (!log.matches(".*someInfo.*")); } finally { System.setErr(old); } }
public void testAssociatedPrintingCanBeDisabledButLinesAreStillRecorded() { StringPrintStream ps = StringPrintStream.newStream(); TestLoggerFactory loggerFactory = Settings.instance(). printingEnabled(false).redirectPrintStream(LogLevel.ErrorLevel, ps). buildLogging(); TestLogger log = loggerFactory.getLogger("john"); log.error("anError"); assert (!ps.contains("anError")); assert (log.contains("anError")); }