This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Best way to implement logging in a servlet based web app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Best way to implement logging in a servlet based web app" Watch "Best way to implement logging in a servlet based web app" New topic
Author

Best way to implement logging in a servlet based web app

Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
I had posed a previously regarding the best way to implement database functionality within a web app. I was advised to use JNDI DataSource and load everything through server.xml and web.xml.


server.xml ...


web.xml ...


Java code



I had previously, however, implemented my own logging which is done through a servlet called Debug ...

web.xml


Notice that this servlet is marked to load on startup ...

I am doing this to insure that servlet will open up log file even before it is explicitly called.

Whenever I need to write something out to a log I simply do this ...



Bear Bibeault did, however, point out to me that if this sort of thing goes through Controller (aka the Servlet), I am doing something wrong ...

What is the best way to implement logging in a Java web app?

Thanks,
Aryeh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Log4J. There's no need to roll your own logging.*

And this seems to have little to do with separating the Model from the Controller. Logging is cross-cutting concern.





* And if you did, why would it be a servlet?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Hi Bear,

* And if you did, why would it be a servlet?


I am trying to learn ... if you were to roll your own, how would you do it?

Thanks,
Aryeh
Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Hi Bear,

Additionally, I have xml files that I am using for configuration that I am reading in contextInitialized() method of ServletContextListener implementing class.









Is this BAD design?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18118
    
    8

Aryeh Golob wrote:
* And if you did, why would it be a servlet?


I am trying to learn ...


Then here's the first thing you should learn about servlets: The purpose of a servlet is to receive an HTTP request from a client, produce a response, and send it to that client.

So now think about it. Where does logging fit into that? If you were to write a "logging servlet", what HTTP requests would it be handling? And what about all of those other servlets, which are handling HTTP requests relating to your real business requirements? Wouldn't they get to do logging?

In other words: yes, logging needs to be done in the web application. But just because something needs to be done, that doesn't imply you have to create a servlet to do it. Not everything in a web application is a servlet. Just use log4j in the normal way, by putting logging code wherever it needs to be put.
 
jQuery in Action, 2nd edition
 
subject: Best way to implement logging in a servlet based web app
 
Similar Threads
JNDI on startup
DataSource is Null in JNDI Connection with Tomcat
javax. servlet. ServletException: Name myoracle is not bound in this Context
adding a Datasource to tomcat jndi
Error confguring multiple contexts/data sources in Tomcat 5