question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject Logback logger into log writer, rather than a static field

See original GitHub issue

Detailed 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:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
binkleycommented, May 15, 2019

@whiskeysierra Thank you for the sell and the suggestion. We’re using the captured HTTP traces from Logbook for several purposes, including:

  • Replacing reliance on slow contract tests for non-contract concerns
  • Validating Zipkin trace IDs in messages passed to Spring Controller and through Feign interfaces (we wrote a hook for Feign to direct it’s request/responses through Logbook)
  • Validating Problem RFC responses for 4xx/5xx status codes
  • Validating presentation of stack traces (lack thereof, actually) to clients in 4xx/5xx responses

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.

0reactions
binkleycommented, Aug 25, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found