• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Log4J and Listeners

 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello;

I have a distributed web application I would like to implement Log4J in.

I was thinking I would create a listener to get a static Logger that all the servlets and java classes could access.

Is this the correct way to do this? If so which listener would be best to implement? I was thinking HttpSessionListener. Is this correct? How have others handled logging in an application?

Thanks,

Luke
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no issue with the listener aspect -- a context listener is a great way to set up application-wide information, for example. What I question is the wisdom of having one Log4J logger that all classes use. Since this eliminates the benefits of Log4J's hierarchical logger system, why bother to use Log4J at all?
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you reccommend each class having its own?

Right now I am just trying to get to work in a test class. It would pretty simple once I have that to distribute it across the project.

Thanks,

Luke
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common pattern is for each class to create its own logger using the fully qualified name of the class. This not only tells you which class logged the message, it gives you a great deal of control over the logging environment.

For example, you can turn DEBUG logging on for only a single class, or for the package hierarchy that the class is in at any level. This is much more useful than having a single logger that emits tons of debug statement from all the classes that you need to wade through to find the relevant messages.
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This makes a lot of sense. I will go this route.

Thanks,

Luke
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic