Pivotal Knowledge Base

Follow

Setting Debug Level Logging for Various Aspects of a Web Application

Environment

 Product  Version
 Spring Framework  2.5/3.0

Purpose

This article provides information about implementing an effective logging strategy from the beginning of developing a web application.

Resolution

To allow log statements to be integrated into the application code, you can leverage several different logging frameworks:

Option 1: Log4J Logger

import org.apache.log4j.Logger;
public class FooClass {
private static final Logger logger = Logger.getLogger(FooClass.class.getName());
public void fooMethod(int bar) {
logger.debug("foo method executed with param: _トン + bar); } }

For more information, see http://logging.apache.org/log4j/1.2/manual.html.

To enable log4j in the project, install the log4j.jar dependency. For information about the dependency details, see http://mvnrepository.com/artifact/log4j/log4j.

Option 2: Slf4J Logger

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FooClass {
final Logger logger = LoggerFactory.getLogger(FooClass.class);
public void fooMethod(int bar) {
logger.debug("foo method executed with param: {}_トン, bar); } }
For more information, see http://www.slf4j.org/manual.html.
These dependencies must be added to enable log4j in combination with slf4j:

Option 3: Apache Commons Logging

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class FooClass {
private Log logger = LogFactory.getLog(FooClass.class);
public void fooMethod(int bar) {
logger.debug("foo method executed with param: _トン + bar); } }
For more information, see http://commons.apache.org/logging/guide.html.

These dependencies must be added to enable log4j in combination with Apache commons logging:
To output logging to the console of your server and a log file, create a log4j.properties file:
#disable root DEBUG logging (enable only if needed)
log4j.rootLogger=info, stdout, R
#output to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#output to file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R.File=application.log
log4j.appender.R.MaxFileSize=1000KB
log4j.appender.R.MaxBackupIndex=2
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#DEBUG log packages of interest
log4j.logger.org.springframework=DEBUG
log4j.logger.com.foo.myapplication=DEBUG
log4j.logger.org.hibernate=DEBUG

Copy this configuration file to the classpath of your application (WEB-INF/ or WEB-INF/classes). The application.log file can be found at the root of your project.

Caution: If you are starting a new project, Pivotal recommends you conduct research to help you decide which logging API to use in your code.
 
 

Comments

Powered by Zendesk