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()); }
/** * 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"); }
TestLoggerFactory loggerFactory = Settings.instance().enable(LogLevel.InfoLevel).buildLogging(); TestLogger logger = loggerFactory.getLogger(this.getClass());
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)); }
.buildLogging()); add(Settings.instance() .suppressPrinting(Pattern.compile(".*Pattern to suppress.*", Pattern.DOTALL)) .buildLogging()); .buildLogging()); }};
.redirectPrintStream(LogLevel.InfoLevel, System.err) .enable(LogLevel.InfoLevel) .buildLogging();
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)); }
@Test public void testDemoAssemblyInjection() { // enable info logging because only error is enabled by default TestLoggerFactory loggingImpl = Settings.instance(). enable(LogLevel.InfoLevel).buildLogging(); // create the Assembly component along with its internal internalDetail instance Example assembly = new Example(loggingImpl); assembly.doSomeInfoLogging(); // check the Info logging assert (loggingImpl.matches("Hello from Assembly")); assert (loggingImpl.matches("Hello from InternalSubcomponent")); } }
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")); }
/** * 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!")); }