This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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: 4301
47
  • 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 .
 
Maybe he went home and went to bed. And took this tiny ad with him:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!