This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Log4J and Listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Log4J and Listeners" Watch "Log4J and Listeners" New topic
Author

Log4J and Listeners

Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
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


Luke
SCJP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61086
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
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

Joined: Jan 10, 2002
Posts: 61086
    
  66

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

Joined: Sep 30, 2004
Posts: 239
This makes a lot of sense. I will go this route.

Thanks,

Luke
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log4J and Listeners