Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a logger per class or is a set of loggers that are accessed by the entire application perferred?

Tags:

java

oop

logging

I have a project in Java, and I create seven loggers that are accessed by a facade from every point of the program. But in the internet, I see a lot of examples witha a logger in each class.

What is the most recommended way to do logging?

like image 694
Renato Dinhani Avatar asked Jul 08 '11 18:07

Renato Dinhani


People also ask

What does logger class do?

The logger class provides methods for logging. Since LogManager is the one doing actual logging, its instances are accessed using the LogManager's getLogger method. The global logger instance is accessed through Logger class' static field GLOBAL_LOGGER_NAME.

What is a logger in programming?

Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running. If you don't have any logging record and your program crashes, there are very few chances that you detect the cause of the problem.

How many types of loggers are there?

The following are the three components: Loggers – Responsible for capturing log records and passing them to the corresponding Appender. Appenders or Handlers – They are responsible for recording log events to a destination. Appenders format events with the help of Layouts, before sending outputs.

What is the use of logger getLogger in Java?

getLogger(java. lang. String): This method is used to find or create a logger with the name passed as parameter. It will create a new logger if logger does not exist with the passed name.


1 Answers

A logger in each class is better and more easy to extend. The reason is that defining one logger in one class easily separates the real logging api from the logger's configuration (format, persistence). I have worked with more than one big and complex java software (> 1 million lines of code), they all use one logger per class.

Also, definition of "one logger per class" doesn't mean that each class uses a different logger instance.

class Foo {
    private static final Logger log = Logger.getLogger( Foo.class );
}


class Bar {
    private static final Logger log = Logger.getLogger( Bar.class );
}

Whether the two loggers referenced in the above code use the same logger instance is not clear. Normally, there is a configuration place (in a property file or in the code) that specifies whether the loggers in Foo and Bar share a logger instance.

So "a logger in each class" just defines one logger for every class, but such loggers may refer to the same logger instance used in different classes

like image 71
Ben Xu Avatar answered Sep 24 '22 21:09

Ben Xu