[java] 로그에 Spring 트랜잭션 표시

트랜잭션 지원으로 봄을 구성했습니다. 모든 것을 올바르게 설정했는지 확인하기 위해 트랜잭션을 기록하는 방법이 있습니까? 로그에 표시하는 것은 무슨 일이 일어나고 있는지 확인하는 좋은 방법입니다.



답변

귀하의 log4j.properties(대체 로거 또는 log4j의 xml 형식의 경우 문서를 확인하십시오)

트랜잭션 관리자에 따라 스프링 프레임 워크의 로깅 수준을 설정하여 트랜잭션에 대한 자세한 정보를 제공 할 수 있습니다. 예를 들어, 사용하는 경우 JpaTransactionManager, 당신은 설정

log4j.logger.org.springframework.orm.jpa=INFO

(이것은 트랜잭션 관리자의 패키지입니다.)

log4j.logger.org.springframework.transaction=INFO

INFO충분하지 않은 경우 사용DEBUG


답변

나를 위해 추가 할 좋은 로깅 구성은 다음과 같습니다.

log4j.logger.org.springframework.transaction.interceptor = 추적

다음과 같은 로그가 표시됩니다.

2012-08-22 18 : 50 : 00,031 TRACE-[com.MyClass.myMethod]에 대한 트랜잭션 가져 오기

[com.MyClass.myMethod 메소드의 내 로그 문]

2012-08-22 18 : 50 : 00,142 TRACE-[com.MyClass.myMethod] 거래 완료


답변

Spring Boot 애플리케이션의 경우 application.properties

logging.level.ROOT=INFO
logging.level.org.springframework.orm.jpa=DEBUG
logging.level.org.springframework.transaction=DEBUG

또는 Yaml ( application.yaml) 을 선호하는 경우

logging:
   level:
      org.springframework.orm.jpa: DEBUG
      org.springframework.transaction: DEBUG


답변

의 가장 흥미로운 로그 정보 JtaTransactionManager.java(이 질문이 여전히에 관한 경우 JtaTransactionManager)가 DEBUG우선적 으로 기록됩니다 . log4j.properties클래스 경로에 어딘가 가 있다고 가정하면 다음 을 사용하는 것이 좋습니다.

log4j.logger.org.springframework.transaction=DEBUG


답변

런타임에 Spring 클래스에 접근 할 수 있기 때문에 트랜잭션 상태를 확인할 수 있습니다. 이 기사가 도움이 될 수 있습니다.

https://dzone.com/articles/monitoring-declarative-transac


답변

JDBC 로깅도 활성화 할 수 있습니다.

log4j.logger.org.springframework.jdbc=DEBUG


답변

다음은 ch.qos.logback.core.LayoutBase 에서 파생 된 Logback Layout 구현에 사용하는 코드 입니다.

메서드에 대한 참조를 저장하기 위해 스레드 로컬 변수를 만듭니다 org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive(). 새 로그 라인이 인쇄 될 때마다getSpringTransactionInfo() 호출되고 로그에 들어갈 한 문자 문자열을 반환합니다.

참조 :

암호:

private static ThreadLocal<Method> txCheckMethod;

private static String getSpringTransactionInfo() {
    if (txCheckMethod == null) {
        txCheckMethod = new ThreadLocal<Method>() {
            @Override public Method initialValue() {
                try {
                    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                    Class<?> tsmClass = contextClassLoader.loadClass("org.springframework.transaction.support.TransactionSynchronizationManager");
                    return tsmClass.getMethod("isActualTransactionActive", (Class<?>[])null);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
         };
    }
    assert txCheckMethod != null;
    Method m = txCheckMethod.get();
    String res;
    if (m == null) {
        res = " "; // there is no Spring here
    }
    else {
        Boolean isActive = null;
        try {
            isActive = (Boolean) m.invoke((Object)null);
            if (isActive) {
                res = "T"; // transaction active                    
            }
            else {
                res = "~"; // transaction inactive
            }
        }
        catch (Exception exe) {
            // suppress 
            res = "?";
        }
    }
    return res;
}