A memorandum for creating an extended logger using org.slf4j.Logger

Overview

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.

Extended logger class

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

Call class

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());
	}
}

Verification environment

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

A memorandum for creating an extended logger using org.slf4j.Logger
Procedure for publishing an application using AWS (4) Creating a database
A memorandum for creating an extended logger using org.slf4j.Logger
Procedure for publishing an application using AWS (4) Creating a database
Building an environment for creating apps with Rails and Vue
I tried using an extended for statement in Java
Creating a calendar using Ruby
Creating a docker host on AWS using Docker Machine (personal memorandum)
Creating a 2021 weekly calendar (refill for personal organizer) using Ruby
A memorandum for writing beautiful code
[Unauthorized Operation] A memorandum because an error occurred when creating an EC2 instance.
[Java] How to turn a two-dimensional array with an extended for statement
A memorandum for android application development beginners
[Creating] A memorandum about coding in Java
Duplicate an object using a generic type
Creating a user authentication function using devise
Create an animation in which characters emerge for a moment using molecular dynamics
Creating an autocomplete feature using acts-as-taggable-on and Tagit.js
[Java] Creating an Excel file using Apache POI
Building a CICD pipeline using Docker (personal memorandum)
A tool for hitting arbitrary SQL using JDBC
[Rails] Creating a breadcrumb trail using Gem gretel
Dreaming of easily creating a Web API for the DB of an existing Java system