org.slf4j.Logger can be used for general purposes, but it is necessary to unify the log output level for business applications. Therefore, create an extended logger that outputs only the trace level defined in the business rule.
Create your own logger class that wraps org.slf4j.Logger. Just leave the output method to org.slf4j.Logger Debug level is output only during development.
SystemLogger.java
/**
*
*/
package jp.co.product.system.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*Log output common class
*
*/
public class SystemLogger {
private Logger logger;
/**
*constructor
*
* @param cls
* @see org.slf4j.LoggerFactory#getLogger(Class)
*/
public SystemLogger(Class<?> cls) {
logger = LoggerFactory.getLogger(cls);
}
/**
*Error output
*
* @param msg Log output contents
* @see org.slf4j.Logger#error(String)
*/
public void error(String msg) {
logger.error(msg);
}
/**
*Information output
*
* @param msg Log output contents
* @see org.slf4j.Logger#error(String)
*/
public void info(String msg) {
logger.info(msg);
}
/**
*Debug output
*
* @param msg Log output contents
* @see org.slf4j.Logger#error(String)
*/
public void debug(String msg) {
if (logger.isDebugEnabled())
logger.debug(msg);
}
}
log4j.properties
log4j.properties is as usual. Just set it so that it can be output from the class that implements the extended logger.
log4j.properties
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.logger.org.apache.activemq=ERROR
log4j.logger.org.springframework.batch=DEBUG
log4j.logger.org.springframework.transaction=INFO
log4j.logger.jp.co.product.system.batch=INFO
log4j.logger.test.jdbc=DEBUG
# for debugging datasource initialization
# log4j.category.test.jdbc=DEBUG
Just create an instance of the extended logger class and use it. Since it is a sample source, the instance generation is not a singleton ... (T & T)
ExampleItemWriter.java
package jp.co.product.system.batch.demo;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
import jp.co.product.system.common.SystemLogger;
/**
* Dummy {@link ItemWriter} which only logs data it receives.
*/
@Component("writer")
public class ExampleItemWriter implements ItemWriter<Object> {
//private static final Log log = LogFactory.getLog(SystemLoggerExampleItemWriter.class);
private static final SystemLogger log = new SystemLogger(ExampleItemWriter.class);
/**
* @see ItemWriter#write(java.util.List)
*/
public void write(List<? extends Object> data) throws Exception {
// log.info(data);
log.debug("debug" + data.toString());
log.info("Info" + data.toString());
log.error("error" + data.toString());
}
}
Validated by Spring Batch The error of slf4j is output at the time of execution. Module definition of slf4j is required by referring to the following site Exterminate the mysterious SLF4J WARN with GAE / J
pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
Recommended Posts