org.testmonkeys.cucumber.spring.logback.CucumberScenarioAppender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cucumber-spring-logback Show documentation
Show all versions of cucumber-spring-logback Show documentation
Logback integration library for cucumber spring
package org.testmonkeys.cucumber.spring.logback;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import io.cucumber.java.Scenario;
/**
* CucumberScenarioAppender appends logs to the current cucumber scenario output
*/
public class CucumberScenarioAppender extends AppenderBase {
private PatternLayoutEncoder encoder;
private Scenario getScenario() {
return SpringContext.getBean(CucumberScenarioContext.class).getCurrentScenario();
}
@Override
protected void append(ILoggingEvent iLoggingEvent) {
String message = this.encode(iLoggingEvent);
Scenario scenario = getScenario();
if (scenario != null) scenario.log(message);
}
private String encode(ILoggingEvent loggingEvent) {
if (this.encoder == null) return loggingEvent.getFormattedMessage();
return new String(this.encoder.encode(loggingEvent));
}
public void setEncoder(PatternLayoutEncoder encoder) {
this.encoder = encoder;
this.encoder.start();
}
}