2017-11-10 74 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日誌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

ActiveMQUtilLogger.LOGGER.failedToParseAddressList(e, addressList); 

這是我能找到的最接近的是我正在尋找。很酷。但是,我沒有使用JBoss(也不想鎖定該API)。

我可以使用LOG4J,它有一個StructuredDataMessage and Structured Data Lookup它可以在一個佈局中使用,我會默認最終使用它;我的代碼可以委託一些StructuredDataMessage工廠來解決這個問題。然而,這比使用這個JBoss API更笨重一點。

有沒有人對此問題有任何建議 - 無論是另一種API,代碼模式還是漂亮的技巧?

+0

你在某種容器中運行嗎?你可能會用一種模式來做,但它可能取決於你使用的日誌管理器。 –

回答

1

你沒有說明你爲什麼不使用log4j2的任何具體原因,所以我建議去那條路線。正如您指出的那樣,它提供了StructuredDataMessage,您可以使用它來滿足您的需求。雖然在這種情況下,我會建議使用MapMessage,因爲您的示例不包括內置於StructuredDataMessage類中的內容,如idtype

下面是一些快速的示例代碼:

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配置文件與它一起去:

<?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> 

下面是一些示例輸出:

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工廠來解決這個

我同意,工廠模式將是一個好的町冰在這裏。正如我在示例代碼中所評論的那樣,您可以將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