Tabnine Logo
TestLogger
Code IndexAdd Tabnine to your IDE (free)

How to use
TestLogger
in
slf4jtest

Best Java code snippets using slf4jtest.TestLogger (Showing top 20 results out of 315)

origin: portingle/slf4jtesting

/**
 * check if a regex exists in a particular log level output.
 */
public boolean contains(LogLevel level, String substring) {
  for (TestLogger l : loggers.values()) {
    if (l.contains(level, substring))
      return true;
  }
  return false;
}
origin: com.portingle/slf4jtesting

@Override
public Collection<LogMessage> lines() {
  ArrayList<LogMessage> lm = new ArrayList<>();
  for (TestLogger l : loggers.values()) {
    lm.addAll(l.lines());
  }
  return Collections.unmodifiableCollection(lm);
}
origin: com.portingle/slf4jtesting

/**
 * check if a substring exists within any of the loggers output
 *
 * @deprecated "use matches(..."
 */
public boolean contains(String substring) {
  for (TestLogger l : loggers.values()) {
    if (l.matches(substring))
      return true;
  }
  return false;
}
origin: portingle/slf4jtesting

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);
  }
}
origin: portingle/slf4jtesting

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));
}
origin: portingle/slf4jtesting

@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));
}
origin: portingle/slf4jtesting

@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);
}
origin: com.portingle/slf4jtesting

/**
 * clear all registered loggers
 */
public void clear() {
  for (TestLogger l : loggers.values()) {
    l.clear();
  }
}
origin: portingle/slf4jtesting

TestLogger logger = loggerFactory.getLogger(this.getClass());
logger.info("Line1" + System.lineSeparator() + "Line2");
logger.assertMatches(new Predicate<LogMessage>() {
  public boolean matches(LogMessage row) {
    return pattern.matcher(row.text).matches() && row.level == LogLevel.InfoLevel;
  final Pattern nonMatch = Pattern.compile("NOTGOOD");
  logger.assertMatches(new Predicate<LogMessage>() {
    public boolean matches(LogMessage row) {
      return nonMatch.matcher(row.text).matches();
origin: portingle/slf4jtesting

logger.info("Info should be printed to Err stream");
logger.error("Error should be printed to Out stream");
origin: portingle/slf4jtesting

public void testErrorIsEnabledByDefault() {
  Settings settings = Settings.instance();
  TestLoggerFactory f = new TestLoggerFactory(settings);
  TestLogger log = f.getLogger("john");
  assert (log.isErrorEnabled());
}
origin: portingle/slf4jtesting

  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");
  }
}
origin: portingle/slf4jtesting

/**
 * 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");
}
origin: portingle/slf4jtesting

public void testWarnIsDisabledByDefault() {
  TestLoggerFactory f = Settings.instance().buildLogging();
  TestLogger log = f.getLogger("john");
  assert (!log.isWarnEnabled());
}
origin: portingle/slf4jtesting

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);
  }
}
origin: portingle/slf4jtesting

/**
 * 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!"));
}
origin: portingle/slf4jtesting

/**
 * clear all registered loggers
 */
public void clear() {
  for (TestLogger l : loggers.values()) {
    l.clear();
  }
}
origin: portingle/slf4jtesting

public void testErrorCanBeDisabled() {
  TestLoggerFactory f = Settings.instance().disable(LogLevel.ErrorLevel).buildLogging();
  TestLogger log = f.getLogger("john");
  assert (!log.isErrorEnabled());
}
origin: portingle/slf4jtesting

logger.error("Should be printed");
logger.error("Pattern to suppress " + System.lineSeparator() +" << should not be printed");
origin: com.portingle/slf4jtesting

/**
 * check if a regex exists in a particular log level output
 */
public boolean matches(LogLevel level, String regex) {
  for (TestLogger l : loggers.values()) {
    if (l.matches(level, regex))
      return true;
  }
  return false;
}
slf4jtestTestLogger

Most used methods

  • contains
  • lines
  • matches
  • clear
  • assertMatches
  • error
  • info
  • isErrorEnabled
  • isWarnEnabled

Popular in Java

  • Parsing JSON documents to java classes using gson
  • scheduleAtFixedRate (Timer)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • setScale (BigDecimal)
  • PrintStream (java.io)
    Fake signature of an existing Java class.
  • Timestamp (java.sql)
    A Java representation of the SQL TIMESTAMP type. It provides the capability of representing the SQL
  • NumberFormat (java.text)
    The abstract base class for all number formats. This class provides the interface for formatting and
  • LinkedHashMap (java.util)
    LinkedHashMap is an implementation of Map that guarantees iteration order. All optional operations a
  • Handler (java.util.logging)
    A Handler object accepts a logging request and exports the desired messages to a target, for example
  • XPath (javax.xml.xpath)
    XPath provides access to the XPath evaluation environment and expressions. Evaluation of XPath Expr
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now