Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do loggers recommend using a logger per class?

As per NLog's documentation:

Most applications will use one logger per class, where the name of the logger is the same as the name of the class.

This is the same way that log4net operates. Why is this a good practice?

like image 913
Daniel T. Avatar asked Jun 29 '10 19:06

Daniel T.


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.

Should loggers be static?

Loggers should be declared to be static and final. It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program.

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 do you write a logger in Java?

The process of creating a new Logger in Java is quite simple. You have to use Logger. getLogger() method. The getLogger() method identifies the name of the Logger and takes string as a parameter.


2 Answers

With log4net, using one logger per class makes it easy to capture the source of the log message (ie. the class writing to the log). If you don't have one logger per class, but instead have one logger for the entire app, you need to resort to more reflection tricks to know where the log messages are coming from.

Compare the following:

Log per class

using System.Reflection; private static readonly ILog _logger =      LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);      public void SomeMethod() {     _logger.DebugFormat("File not found: {0}", _filename); } 

One logger per app (or similar)

Logger.DebugFormat("File not found: {0}", _filename); // Logger determines caller  -- or --  Logger.DebugFormat(this, "File not found: {0}", _filename); // Pass in the caller 

Using the second example, the Logger would need to build a stack trace to see who was calling it or your code would always have to pass in the caller. With the logger-per-class style, you still do this, but you can do it once per class instead of once per call and eliminate a serious performance problem.

like image 69
Jeremy Wiebe Avatar answered Oct 05 '22 11:10

Jeremy Wiebe


Advantage for using "logger per file" in NLog: you have possibility to manage/filter logs by namespace and class name. Example:

<logger name="A.NameSpace.MyClass"      minlevel="Debug" writeTo="ImportantLogs" />  <logger name="A.NameSpace.MyOtherClass" minlevel="Trace" writeTo="ImportantLogs" />  <logger name="StupidLibrary.*"          minlevel="Error" writeTo="StupidLibraryLogs" />  <!-- Hide other messages from StupidLibrary --> <logger name="StupidLibrary.*" final="true" />   <!-- Log all but hidden messages --> <logger name="*" writeTo="AllLogs" />  

NLogger has a very useful code snippet to do this. The nlogger snippet will create the following code:

private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); 

So only few keystrokes and you have logger per class. It will use namespace and class name as the name of the logger. To set different name to your class logger, you can use this:

private static NLog.Logger logger = NLog.LogManager.GetLogger("MyLib.MyName"); 

And, as @JeremyWiebe said, you don't have to use tricks to get the name of the class which is trying to log a message: Name of the logger (which is usually the name of the class) can be easy logged to file (or other target) by using ${logger} in layout.

like image 35
CoperNick Avatar answered Oct 05 '22 12:10

CoperNick