2017-11-10 12 views
0

표준 집합을 내 응용 프로그램의 중요한 중요 시점에 제공 할 수있는 API를 찾고 싶습니다. 구체적으로는 (기초 외에) 다음과 같은 정보를 여러 줄의 메시지가 될 것입니다 :자세한 (및 여러 줄) 로그 용 Java 로깅 API

  1. 로깅 카테고리
  2. 짧은 설명 제목
  3. 설명
  4. 대응 조치 (손상 제어) 하나의 멀티 라인 로그 전자에 대한보고와 (등 예외 정보)
  5. 정보를 취할 것입니다 내 응용 프로그램

xample, 그래서 같은 :

2017-11-10 14:26:59,156 [main] WARN o.s.t.c.s.ExampleClass: 
    Caption: Unconformable data 
    Description: The data provided from the X datasource in order to perform Y operation could not be translated 
    Response Action: Application will discard unusable data into this component's DLQ 
    Details: The data string "x" was not of expected Integer type 
     <Some stacktrace>.... 

이가 발생하고 응용 프로그램이 예외의 이벤트에 대한 응답으로 무엇을하고 있는지, 여기서 발생한 정확히 무엇에 대해 매우 유익한 정보가 될 것 자세한 문입니다.

가장 가까운 JBoss logging API와 내가 찾은 코드 중 일부는 ActiveMQ Artemis source입니다.

@LogMessage(level = Logger.Level.WARN) 
    @Message(id = 202008, value = "Failed to check Address list {0}.", 
     format = Message.Format.MESSAGE_FORMAT) 
    void failedToParseAddressList(@Cause Exception e, String addressList); 

그리고 하나는 서면으로 자신의 코드에 line with this message를 기록 할 것 : 메시지 형식 선언과 같이 하나의 파일에서 정의 할 수 있습니다

I가 찾을 수있는 가장 가까운입니다
ActiveMQUtilLogger.LOGGER.failedToParseAddressList(e, addressList); 

내가 찾고 있었어. 아주 멋지다. 그러나, 나는 JBoss를 사용하지 않고있다. (그리고 또한 그 API를 잠그고 싶지도 않다.)

레이아웃에서 사용할 수있는 StructuredDataMessage and Structured Data Lookup이있는 LOG4J를 사용할 수 있으며, 기본적으로이를 사용하게됩니다. 내 코드는이 문제를 해결하기 위해 StructuredDataMessage 팩토리에 위임 할 수 있습니다. 그러나이 JBoss API와 같은 것을 사용하는 것보다 조금 더 복잡합니다.

다른 API, 코드 패턴 또는 멋진 트릭이든이 문제에 대한 제안 사항이있는 사람이 있습니까?

+0

어떤 종류의 용기에서 뛰고 있습니까? 패턴을 사용하여이를 수행 할 수는 있지만 사용중인 로그 관리자에 따라 다를 수 있습니다. –

답변

1

log4j2를 사용하지 않는 구체적인 이유가 명시되지 않았으므로 해당 경로를 제안 할 것입니다. 당신이 지적한대로 당신의 필요에 맞게 사용할 수있는 StructuredDataMessage을 제공합니다. 이 경우에는 StructuredDataMessage 클래스에 내장 된 idtype과 같은 것을 포함하지 않았으므로 MapMessage을 사용하는 것이 좋습니다.

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.apache.logging.log4j.message.MapMessage; 

public class MapMessageExample { 

    private static final Logger log = LogManager.getLogger(); 

    public static void main(String[] args){ 
     log.info(buildMsg("My title", "This is the description", 
       "No response needed", "Some details here"));   
    } 

    // This could be moved into a factory class 
    public static MapMessage buildMsg(String title, String description, 
      String responseAction, String details) 
    { 
     MapMessage mapMsg = new MapMessage(); 
     mapMsg.put("title", title); 
     mapMsg.put("desc", description); 
     mapMsg.put("response", responseAction); 
     mapMsg.put("details", details); 
     return mapMsg; 
    } 
} 

과 log4j2 :

여기에 몇 가지 빠른 샘플 코드입니다.XML 설정 파일은 그것과 함께 이동 :

00:40:45.810 [main] INFO example.MapMessageExample 
    Caption: My title 
    Description: This is the description 
    Response Action: No response needed 
    Details: Some details here 

어떤 마지막 생각 :

일부 StructuredDataMessage 공장에 위임 할 수있는 내 코드는이 문제를 해결하기 위해 여기에

<?xml version="1.0" encoding="UTF-8"?> 
<Configuration status="WARN"> 
    <Appenders> 
     <Console name="Console" target="SYSTEM_OUT"> 
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%M] %-5level %logger{36}%n\tCaption: ${map:title}%n\tDescription: ${map:desc}%n\tResponse Action: ${map:response}%n\tDetails: ${map:details}%n" /> 
     </Console> 
    </Appenders> 

    <Loggers> 
     <Root level="debug"> 
      <AppenderRef ref="Console" /> 
     </Root> 
    </Loggers> 
</Configuration> 

일부 샘플 출력

동의합니다. 공장 패턴은 좋은 쵸 여기 얼음. 샘플 코드에 주석을 달았으므로 buildMsg 메서드를 팩토리 클래스로 이동할 수 있습니다.

그러나이 JBoss API와 같은 것을 사용하는 것보다 조금 비쌉니다.

나는 그것이 얼마나 부피가 큰지 실제로 알지 못합니다. 대부분의 경우 MapMessage에있는 항목 중 하나 또는 두 개만 변경되면 언급 한 API와 유사한 매우 구체적인 메소드를 쉽게 작성할 수 있습니다. 당신은 아마 문자열을 상수이 방법에서 사용되는 할당 할 것이다

public static MapMessage reallySpecificLogMessage(String details){ 
    return buildMsg("My specific message title", "My specific description", 
      "My specific response action", details); 
} 

하지만 말했듯이 그냥 빨리 예입니다 위의 예에 추가합니다.

+0

그래, 나는 이걸 이걸 사용하는 줄 알았지 만 그 밖의 다른 아이디어가 있는지 알고 싶었어. 감사 – Dovmo