• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton logger for the whole application

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic