How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java

General acquisition method

private static final Logger logger = LoggerFactory.getLogger(TestSub1.class);

In this case, the description is different for each class file, and care must be taken when copying.

TestSample1.java


package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestSample1 {

	private static final Logger logger = LoggerFactory.getLogger(TestSub1.class);

	public void exec() {
		logger.debug("Exec method was executed.");
	}
}

Get using this

private final Logger logger = LoggerFactory.getLogger(this.getClass());

If you use this, you can't make it static, but you can copy it because the description is the same in every class file.

TestSample2.java


package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestSample2 {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	public void exec() {
		logger.debug("TestSample#Exec method was executed.");
	}
}

Use class name acquisition method

private static final Logger logger = LoggerFactory.getLogger(Util.getClassName());

By using the ʻUtil # getClassNamemethod, all class files have the same description and can be copied. It can also be used withstatic`.

TestSample3.java


package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestSample3 {

	private static final Logger logger = LoggerFactory.getLogger(Util.getClassName());

	public void exec() {
		logger.debug("TestSample3#exec method was executed.");
	}
}

Util.java


package test;

public class Util {
	public static String getClassName() {
		return Thread.currentThread().getStackTrace()[2].getClassName();
	}
}

For CDI

You can use @Produces and @Inject to inject instances into Logger. All class files have the same description and can be copied.

SampleManagedBean.java


package test.mb;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;

@Named
@SessionScoped
public class SampleManagedBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@Inject
	private transient Logger logger;

	public String execute() {
		logger.info("SampleManagedBean#execute method was executed.");
		return null;
	}
}

LoggerFactoryProducer.java


package test.util;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Dependent
public class LoggerFactoryProducer {

    @Produces
    public Logger getLogger(InjectionPoint ip) {
    	return LoggerFactory.getLogger(ip.getMember().getDeclaringClass().getName());
    }
}

that's all

Recommended Posts

How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to get the class name / method name running in Java
How to solve the unknown error when using slf4j in Java
[Java] How to get to the front of a specific string using the String class
How to get the date in java
How to get the length of an audio file in java
When you get lost in the class name
How to get Class from Element in Java
[Java] How to get the authority of the folder
How to find the total number of pages when paging in Java
How to get the absolute path of a directory running in Java
[Java] How to get the URL of the transition source
Get the name of the test case in the JUnit test class
[Java] How to get the maximum value of HashMap
graphql-ruby: How to get the name of query or mutation in controller Note
[Java] How to easily get the longest character string of ArrayList using stream
How to name variables in Java
How to derive the last day of the month in Java
Summary of how to use the proxy set in IE when connecting with Java
[Java] Get date information 10 days later using milliseconds in the Date class
How to get the contents of Map using for statement Memorandum
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to increment the value of Map in one line in Java
Get the result of POST in Java
[Java] How to use the File class
[Java] How to get the current directory
[Processing × Java] How to use the class
[Java] How to use the Calendar class
How to find out the Java version of a compiled class file
How to get the value after "_" in Windows batch like Java -version
How to constrain the action of the transition destination when not logged in
I want to get the IP address when connecting to Wi-Fi in Java
I want to get the field name of the [Java] field. (Old tale tone)
How to reference a column when overriding the column name method in ActiveRecord
How to create your own annotation in Java and get the value
How to get Excel sheet name list in Java (POI vs SAX)
How to get keycloak credentials in interceptor class
How to get today's day of the week
[Java] How to get the redirected final URL
[Swift] How to get the number of elements in an array (super basic)
How to get the query string to actually issue when using PreparedStatement with JDBC
Sample code to get the values of major SQL types in Java + MySQL 8.0
In Apache POI 3.15, when I get the result of the formula, FormulaParseException occurs (the formula refers to "cell of sheet name including" ・ ")
Get the URL of the HTTP redirect destination in Java
[Java] How to use compareTo method of Date class
[Java] How to omit the private constructor in Lombok
[Kotlin] Get the argument name of the constructor by reflection
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
Source used to get the redirect source URL in Java
[Rails] How to change the column name of the table
[Java] Get the file in the jar regardless of the environment
As of April 2018 How to get Java 8 on Mac
[Android] How to get the setting language of the terminal
How to convert A to a and a to A using AND and OR in Java
[Rails] How to get the contents of strong parameters
Summary of how to implement default arguments in Java
[Swift] How to get the document ID of Firebase
Differences in code when using the length system in Java
Summarize the additional elements of the Optional class in Java 9
[Java] How to get the key and value stored in Map by iterative processing