Hello I just want to make sure I am understanding your user manual
(specifically https://www.slf4j.org/legacy.html) correctly:
Sounds like if I put the following JARs on my runtime classpath I can pump
logs made from other logging systems and send them to the same SLF4J
appenders my app is using:
- jcl-over-slf4j.jar --> JCL
- log4j-over-slf4j.jar --> Log4J
- jul-to-slf4j.jar --> JUL
- requires I call two SLF4JBridgeHandler methods (
removeHandlersForRootLogger() and install()) at app startup in order
to work
Am I understanding this correctly? Thanks in advance!
I'm interested in utilizing the Mapped Diagnostic Context (MDC)
<https://logback.qos.ch/manual/mdc.html> but have two types of MDC needs:
- *global*: I need to add certain key-value pairs to the MDC when the
server starts up, and I need them present in every log message for the
entire life of the server; and
- *request-scoped* (thread scoped?): certain key-value pairs need to be
added to the MDC but will change for every/most HTTP request the server
receives
I know that MDC is thread-safe, so I'm guessing each HTTP request will have
its own "blank slate" version/instance of the MDC to work with, which takes
care of my request-scoped needs. But from inside request handlers (servlet
filters, Spring controllers, etc.) I still need the global MDC key-value
pairs present in any log messages that are made.
For example, say, at server startup I need to add:
MDC.put("Server-Instance-Id", serverInstanceId);
I will need "*Server-Instance-Id*" in every single log message sent,
regardless of what thread/request the log message is sent from.
But then say in my FizzController#getFizzes() method, which accepts HTTP
requests to, say, GET /v1/fizzes, I have:
MDC.put("Fruit", pear.getName());logger.info("User is requested all fizzes");
In this case above, I would want *both* "*Server-Instance-Id*" and "*Fruit*"
values included in the log message.
Similarly, if I clear the MDC (MDC.clear()) from inside a request thread
(such as a call to FizzController#getFizzes()), I don't want it to clear
the global context variables (such as "*Server-Instance-Id*"). Only clear
the request-scoped ones.
Does anyone know how to accomplish this with SLF4J config or its API?
Java 11 and logback-classic-1.2.11 here. I'm trying to write my own custom
appender and am following this Baeldung article
<https://www.baeldung.com/custom-logback-appender> to test it out.
My *src/main/java/myapp/logging/CatAppender* appender (on the runtime
classpath):
public class CatAppender extends AppenderBase<ILoggingEvent> {
@Override
protected void append(ILoggingEvent eventObject) {
System.out.println("meow");
}
}
My *src/main/resources/logback.xml*:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="cat" class="myapp.logging.CatAppender"/>
<root level="info">
<appender-ref ref="cat" />
</root>
</configuration>
In my *build.gradle* I specify to use logback and Lombok:
plugins {
id "io.freefair.lombok" version '6.4.0'
}
dependencies {
implementation (
'ch.qos.logback:logback-classic:1.2.11'
,'org.projectlombok:lombok:1.18.16'
)
}
And then in my Java code I use Lombok to inject an SLF4J logger like so:
@Slf4j
public class SomethingDoer {
public void doSomething() {
log.info("this should invoke the CatAppender...");
}
}
But when *SomethingDoer#doSomething()* runs, I don't see a meow printed to
my STDOUT console. Have I wired anything up incorrectly here?