GeeCON Prague 2014*
The moose likes Servlets and the fly likes Singleton logger for the whole application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Singleton logger for the whole application" Watch "Singleton logger for the whole application" New topic
Author

Singleton logger for the whole application

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
In an enterprise Java web application , can I create a singleton Logger instance for the whole application?
Any concurrency issue?
(I'm thinking about java.util.loggin.* tools).
Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

If you use Apache's log4j then it automatically produces a singleton logger for the web application. This is usually what you want.

If you use Sun's java.util.logging classes then you get a singleton logger for the entire JVM, which could include several web applications. This is often not what you want.

Or yes, you could create your own logging system. Concurrency issues could be overcome. But that would constitute reinventing the wheel.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

If you use Sun's java.util.logging classes then you get a singleton logger for the entire JVM, which could include several web applications. This is often not what you want.

Suppose I have two web application deployed on Tomcat.
A servlet in the first application has this line:
Logger logger = Logger.getLogger("com.web.FrontController")
A servlet in the second application has:
Logger logger = Logger.getLogger("com.portal.Intercepter")
These two lines will return the same Logger instance?
Each application has its own class loader.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Originally posted by John Todd:

Suppose I have two web application deployed on Tomcat.
A servlet in the first application has this line:
Logger logger = Logger.getLogger("com.web.FrontController")
A servlet in the second application has:
Logger logger = Logger.getLogger("com.portal.Intercepter")
These two lines will return the same Logger instance?
Each application has its own class loader.


It will return the same logger for that application. Each application has a single logger loaded from its log4j.jar lib. Each application can instantiate the properties for that logger using log4j.xml and the DomConfigurator


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by John Meyers:
It will return the same logger for that application. Each application has a single logger loaded from its log4j.jar lib. Each application can instantiate the properties for that logger using log4j.xml and the DomConfigurator
But John Todd was asking about the java.util.logging classes, not about log4j. What you say is correct for log4j. I can't answer John Todd's question because I too use log4j in web applications. Maybe somebody else could?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by John Todd:

Suppose I have two web application deployed on Tomcat.
A servlet in the first application has this line:
Logger logger = Logger.getLogger("com.web.FrontController")
A servlet in the second application has:
Logger logger = Logger.getLogger("com.portal.Intercepter")
These two lines will return the same Logger instance?
Each application has its own class loader.


It will never return the same logger instance even in the same web application.


Rahul Bhattacharjee
LinkedIn - Blog
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Is it safe to create a singleton Logger (java.util.logging) using the factory pattern for my application?

It will never return the same logger instance even in the same web application.

But we don't care about this, right?
Forgive my ignorance.
 
GeeCON Prague 2014
 
subject: Singleton logger for the whole application