• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Logger question

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example - if I am using Log4J implementation in web application,
First approach - I can use logger.debug, logger.warn etc statements in all classes. these logger statements are directly calling LOg4J framework to log messages in log file. So, log statements are kind of scattered across whole code.
Second approach - I can create a centralized Logger class [ and define debug, warn, info etc methods in it] and use this centralized Logger instance across whole code to log debug, warn, info etc statements. This centralized Logger class internally uses Log4J methods to log messages in log file.

Which approach is better ?Is it advisable or better pattern to use centralized logging code rather than using scattered log code ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in the second approach you would still have log statements scattered across the code; the only difference is that you put your own logger class in between which really does nothing but pass on the calls to Log4J.

If you don't want to tie your code to one particular logging framework implementation such as Log4J, then use SLF4J - it's a logging API that allows you to use different logging framework implementations, such as Log4J, Logback or java.util.logging without changing your code.
 
Rancher
Posts: 4734
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abhay Agarwal wrote:For example - if I am using Log4J implementation in web application,
First approach - I can use logger.debug, logger.warn etc statements in all classes. these logger statements are directly calling LOg4J framework to log messages in log file. So, log statements are kind of scattered across whole code.
Second approach - I can create a centralized Logger class [ and define debug, warn, info etc methods in it] and use this centralized Logger instance across whole code to log debug, warn, info etc statements. This centralized Logger class internally uses Log4J methods to log messages in log file.

Which approach is better ?Is it advisable or better pattern to use centralized logging code rather than using scattered log code ?



Unless your new Logger class also accepts the name of the class that is doing the logging then this is actually worse as all your log calls will look like they are coming from your Logger class, and not the actual class doing the logging.

So this:

becomes this, if you want to know where the logging occurred:


Not only do you still have the logging statement (as Jesper points out), but it's now longer, and you now need some way of getting that name into the log, probably by holding a map of class name to loggers...

Put simply, Log4J is already centralised logging .
 
I'm doing laundry! Look how clean this tiny ad is:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic