Inject Logback logger into log writer, rather than a static field
See original GitHub issueDetailed Description
With version “1.13.0” I could inject a mock SLF4J logger bean named “httpLogger” into tests, and capture the calls of logbook, to verify my request/response behavior for logging. This was very helpful when logging is a 1st-class concern, and made testing straight-forward. (I discovered these fact experimentally.)
With version “2.0.0-RC.1”, the logger is a static field in `DefaultHttpLogWriter", and so not accessible for tests barring tricks. The code is:
public final class DefaultHttpLogWriter implements HttpLogWriter {
private final Logger log = LoggerFactory.getLogger(Logbook.class);
// ... rest omitted ...
}
The code for “1.13.0” is:
public final class DefaultHttpLogWriter implements HttpLogWriter {
public enum Level {
TRACE, DEBUG, INFO, WARN, ERROR
}
private final Logger logger;
private final Predicate<Logger> activator;
private final BiConsumer<Logger, String> consumer;
public DefaultHttpLogWriter() {
this(LoggerFactory.getLogger(Logbook.class));
}
public DefaultHttpLogWriter(final Logger logger) {
this(logger, Level.TRACE);
}
// ... rest omitted ...
}
It would be helpful to recover this behavior in Logbook 2. Static fields make testing a challenge!
I believe my work around is to provide my own HttpLogWriter
bean which uses an injected logger similar to how “1.13.0” worked.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top Results From Across the Web
Solving Your Logging Problems with Logback - Stackify
To address this well-known limitation, Logback provides the RollingFileAppender, which rolls over the log file when certain conditions are met.
Read more >A Guide To Logback - Baeldung
The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the...
Read more >Chapter 3: Logback configuration
Inserting log requests into the application code requires a fair amount ... LoggerFactory; public class MyApp1 { final static Logger logger = LoggerFactory....
Read more >Any reason to use private instead of private final static on the ...
Personally, I use private final Logger logger = LoggerFactory.getLogger(this.getClass());. The main advantage of this is I can cut and paste ...
Read more >Configuring Logback with Spring Boot - CodinGame
The LOGGER allows messages to be written to the log using the methods which represent each logging level, trace , debug , info...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@whiskeysierra Thank you for the sell and the suggestion. We’re using the captured HTTP traces from Logbook for several purposes, including:
In support, we use the JSON format for Logbook, so we can parse the log lines into Java POJOs:
https://github.com/binkley/spikes/blob/master/spring-boot-logging/src/test/java/x/loggy/HttpTrace.java
(This is a black-box approach, without wanting to hook into the innards of Logbook.)
SLF4J Test looks very interesting! That may be cleaner than using a Mockito mock for the logger. Thanks.
I’m behind the ball here, but to close this out – our general pattern is to inject the logger as a field. Combined with Lombok
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
, the code overhead for developers is manageable.