Generally in traditional deployment, we copy the code from development server to the production server and carry out some other configurations. 5.1.Have you ever observed that an application runs successfully in development environment, but the same application with the same code creates multiple issues while running in the production environment? If not, you may observe this in near future when you follow traditional deployment procedure. With that objective in mind, let’s see how you can use the MDC mechanism. You might want to track easily each client’s transaction. This map will contain relevant information that might be useful when logging messages.įor example, imagine you are working on a regular server-client architecture. The Mapped Diagnostic Context, or MDC for short, is simply a map managed by the logging framework on a per-thread basis. Each thread will be dedicated to one client, and will, therefore, have a specific context. SLF4J Supports Mapped Diagnostic Context MechanismsĪny server application you develop will typically attend several clients using a pool of threads. One good indication of SLF4J’s popularity is its wide usage in popular libraries and frameworks in the Java ecosystem – such as Spring Boot, or Hibernate which has first-class support for it with minimal configuration changes. You should definitely keep it handy in case you have to deal with any of those issues. Simply put, you’ll find most corner cases and specific scenarios over on StackOverflow.Īlso, within the SLF4J documentation itself, you’ll find a section explaining the most common errors and warnings that you might come across when working with this logging abstraction. If you ever had to struggle with a poorly documented library or had to deal with an edge case that nobody covered yet, you’ll certainly understand just how important this aspect is. ("client requested process the following list: ", myObject) 4. Logger logger = LoggerFactory.getLogger(SimpleClass.class) Let’s go ahead and see how your code will look like after you add logging statements: import Lucky for us, the SLF4J developers made the API really simple and straightforward. The Logging API you choose can’t be complex at all, or it will seriously affect the performance of your application. You’ll have to log information in almost any class you’ll be working on. Nevertheless, there are quite a lot of other aspects that make this library an appealing logging solution.
Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J. This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. When you are first creating an application, you might not know what logging framework will be most suitable for your future needs, or you could simply want your system to remain agnostic regarding the logging implementation to be used.įurthermore, it’s also quite useful to have the flexibility to not be tied to a specific logging framework.
One of the most important aspects of developing an efficient system is to have clean, helpful logs that will help you understand what errors are being triggered, and what information is being processed. What Is SLF4J and What are the Benefits of Using It?