Is SLF4J thread-safe?

user1768830 picture user1768830 · Aug 31, 2013 · Viewed 11.3k times · Source

I might have a Dog class that has a single instance shared across multiple threads. I plan on using SLF4J for all logging:

public class Dog {
    private Logger logger = LoggerFactory.getLogger(Dog.class);

    // ...etc.
}

Is my logger instance thread safe? Why/why not?

Answer

Stephen C picture Stephen C · Aug 31, 2013

Certainly, everyone assumes that a Logger is going to be thread-safe. But you would need to look at the code / javadocs of the implementation classes behind the facade to be absolutely sure.

For specific implementations:

(Obviously, these are statements that the respective code is designed to thread-safe. There can always be bugs. For example, were at the time of writing a couple of open thread-safety bugs in the Log4j 2 tracker, though it doesn't seem like those bugs would directly affect your example code.)