Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to implement logging in a servlet based web app

 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic