Daredevi1 Daredevi1 - 1 year ago 127
Java Question

Java - Logback to display testcase name

I am using Logback in my Selenium Webdriver project and I've reached a bit of a roadblock. I've search online but not been able to find the answer.

I am trying to find a way which allows me to get the testcase name (testGoogleWebsite) once inside the

method. So instead of just printing "Starting test" it prints out "Starting test - testGoogleWebsite"

I know I can do
on the first line of each test, but wanted to know if there's a better way.

My Test:

public void testGoogleWebsite() {

public void setup() throws FileNotFoundException {
LOG.debug("Starting test - ");
driver = new ChromeDriver();

public void tearDown(){
LOG.debug("End of test");

My logback file:

<?xml version="1.0" encoding="UTF-8"?>

<property name="DEV_HOME" value="target/Logs" />

<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->


<logger name="com.test" level="debug" additivity="false">
<appender-ref ref="FILE-AUDIT" />

<root level="debug">
<appender-ref ref="FILE-AUDIT" />


Answer Source

I think you are talking about logging your testacse name (as testGoogleWebsite is a testcase name I see). To do this you can put your LOG.debug("Starting test - "); in beforeMethod.

Code snippet

public void beforeMethod(Method method) {
  LOG.debug("Starting test - " + method.getName(););

This will log your each and every testcase name as you wanted.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download