Package org.sonar.api.utils.log
Class LogTester
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- org.sonar.api.utils.log.LogTester
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public class LogTester extends org.junit.rules.ExternalResource
For tests only
This JUnit rule allows to configure and access logs in tests. By default trace level is enabled.
Warning - not compatible with parallel execution of tests in the same JVM fork.
Example:public class MyClass { private final Logger logger = Loggers.get("logger_name"); public void doSomething() { logger.info("foo"); } } public class MyTest { @org.junit.Rule public LogTester logTester = new LogTester(); @org.junit.Test public void test_log() { new MyClass().doSomething(); assertThat(logTester.logs()).containsOnly("foo"); } }
- Since:
- 5.1
-
-
Constructor Summary
Constructors Constructor Description LogTester()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
after()
protected void
before()
LogTester
clear()
java.util.List<LogAndArguments>
getLogs()
Logs with arguments in chronological order (item at index 0 is the oldest one)java.util.List<LogAndArguments>
getLogs(LoggerLevel level)
Logs with arguments in chronological order (item at index 0 is the oldest one) for a given leveljava.util.List<java.lang.String>
logs()
Logs in chronological order (item at index 0 is the oldest one)java.util.List<java.lang.String>
logs(LoggerLevel level)
Logs in chronological order (item at index 0 is the oldest one) for a given levelLogTester
setLevel(LoggerLevel level)
Enable/disable debug logs.
-
-
-
Method Detail
-
before
protected void before() throws java.lang.Throwable
- Overrides:
before
in classorg.junit.rules.ExternalResource
- Throws:
java.lang.Throwable
-
after
protected void after()
- Overrides:
after
in classorg.junit.rules.ExternalResource
-
setLevel
public LogTester setLevel(LoggerLevel level)
Enable/disable debug logs. Info, warn and error logs are always enabled. By default INFO logs are enabled when LogTester is started.
-
logs
public java.util.List<java.lang.String> logs()
Logs in chronological order (item at index 0 is the oldest one)
-
logs
public java.util.List<java.lang.String> logs(LoggerLevel level)
Logs in chronological order (item at index 0 is the oldest one) for a given level
-
getLogs
public java.util.List<LogAndArguments> getLogs()
Logs with arguments in chronological order (item at index 0 is the oldest one)
-
getLogs
public java.util.List<LogAndArguments> getLogs(LoggerLevel level)
Logs with arguments in chronological order (item at index 0 is the oldest one) for a given level
-
clear
public LogTester clear()
-
-