Thursday, March 15, 2012

Server-side logging for Apache Tomcat

Okay, so after spending two days on a problem that wasn't an actual problem, I was frustrated and determined that the only way I could get any feedback from the program was to implement a logging. I don't know if I've never had to use logging before because I'm smart and thus don't need it, or because I'm stupid and like doing things the hard way, but there goes.

A very simple guide to using logging for Apache Tomcat:

1.) The output will be put into the logs in the tomcat directory catalina.20XX-XX-XX.log.

2.) Include the following imports:
import java.util.logging.Level;
import java.util.logging.Logger;

3.) Initialize the logger in the class you want logged:
private static String nameOfLogger = <INSERT_CLASS_NAME_HERE>.class.getName();
private static Logger myLogger = Logger.getLogger(nameOfLogger); '

4.) Insert the following code if you want something always to be logged (alternatively, you can change "severe" to other levels, if you want the messages to only be printed if a certain level is reached):
myLogger.severe(<STRING YOU WANT PRINTED>);

5.) Don't forget to set the level of the logger:
myLogger.setLevel(Level.WARNING);

6.) Also create a logging.properties file in the /war/WEB-INF/ folder
An example file is like this:
# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).
#
# To use this configuration, copy it into your application's WEB-INF
# folder and add the following to your appengine-web.xml:
#
# <system-properties>
#   <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
#

# Set the default logging level for all loggers to WARNING
.level = WARNING

For more information check out this wonderful guide

No comments:

Post a Comment