I’m new to Servlet containers and have created a web application using Tomcat 6.0.26. I have ‘TODO: log’ scattered throughout my code. I see there exists:
myServlet.getServletContext().log()
which appears to write to a file prefixed with ‘localhost’ in the Tomcat ‘/logs’ directory. I don’t need any advanced logging capability, but I’d like a date, time, message and stack trace at minimum. In addition, I’ve created some classes used by my various servlets that need logging capabilities as well. Do I need to inject a SevletContext into these classes so they can log?
It appears that log4j from Apache is a popular logging package, but I’m not sure if it worth the trouble of setting it up.
What would be the recommended way of logging for my needs?
In addition to all the stuff BalusC has mentioned above, in your java code (servlets/beans/whatever) just import and initialize the Logger
Then at any logging point you can log at various levels like
These will appear in the logs depending on whether you set DEBUG or INFO in the log4j.propeties
Take a look at the examples on http://www.java2s.com/Code/Java/Language-Basics/Examplelog4jConfigurationFile.htm and all the Related examples in the same article