Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J Java Logging Design

I am beginning to use SLF4J for logging and the first thing that strikes to me is the following piece of code

public class MyClass
{
   private static final logger = org.slf4j.LoggerFactory.getLogger(MyClass.class)
}

What is the design principle or logic of using the class as an argument to obtain an instance of the logger ?

like image 476
Anand Sunderraman Avatar asked Dec 26 '22 23:12

Anand Sunderraman


1 Answers

The result of this is that each logging statement generated by that logger contains the name of the class. You want to do this to:

  1. identify the source of that logging message in the output
  2. filter effectively on those messages. You can enable disable messages by class and/or the containing package. e.g. you may wish to disable all your DAO package messages (for whatever reason)

The above is a very common pattern in logging. Note however that you don't just have to provide a class, but you can provide your own string instead, and thus arbitrarily categorise your messages yourself.

like image 121
Brian Agnew Avatar answered Dec 30 '22 10:12

Brian Agnew