Skip to content

Commit

Permalink
Maven-like Log4j configuration for test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrygamat committed Dec 9, 2023
1 parent c1d91c9 commit 31869b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/test/java/jssc/junit/rules/DisplayMethodNameRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.MarkerManager;
import org.junit.Rule;
import org.junit.rules.*;
import org.junit.runner.*;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
* Adds the method name to the JUnit logs, useful for debugging
Expand All @@ -15,7 +16,7 @@ public class DisplayMethodNameRule {
@Override
protected void starting(Description description) {
Logger log = LogManager.getLogger(description.getTestClass());
log.debug(description.getMethodName());
log.debug(MarkerManager.getMarker("MethodName"), description.getMethodName());
}
};
}
16 changes: 14 additions & 2 deletions src/test/resources/log4j2.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# Logging settings for unit tests

# The root logger with appender name
rootLogger=DEBUG, STDOUT
rootLogger=DEBUG,STDOUT,TESTNAME

# Assign STDOUT a valid appender & define its layout
appender.console.name=STDOUT
appender.console.type=Console
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=[%highlight{%p}{INFO=blue}] [%c{1}] %m%n
appender.console.layout.pattern=[%highlight{%p}{INFO=blue}] [%c{1}] %m%n

# Make logs for Junit4 method names look like maven
appender..name=TESTNAME
appender.testName.type=Console
appender.testName.layout.type=PatternLayout
appender.testName.layout.pattern=[%highlight{%p}{INFO=blue}] Running %c.%highlight{%m}{FATAL=bold,white, ERROR=bold,white, WARN=bold,white, INFO=bold,white, DEBUG=bold,white, TRACE=bold,white}%n
appender.testName.filter.1.type=MarkerFilter
appender.testName.filter.1.marker=MethodName
appender.console.filter.1.type=MarkerFilter
appender.console.filter.1.marker=MethodName
appender.console.filter.1.onMatch=DENY
appender.console.filter.1.onMismatch=ACCEPT

0 comments on commit 31869b7

Please sign in to comment.