2014-11-04 43 views
3

有沒有人知道Dropwizard 0.7.1日誌的默認格式模式,如果沒有配置logFormat?Dropwizard默認的回滾模式

我需要某事像:

%d{HH:mm:ss.SSS} %-5level [%X{id}] [%X{format}] [%thread]: %class{0}::%method:%line - %msg%n 
+1

根據源代碼'io.dropwizard.logging.DropwizardLayout',這是 '%-5p [%d {ISO8601, 「+ timeZone.getID()+」}]%C:%M% N%rEx'。由於我不確定timeZone和setPattern之前的其他行是否相關,因此將其留作評論。 – Natan 2014-11-25 11:10:34

+1

@ user3280180 - 如果解決了,請標記答案。 – nullpointer 2016-12-12 16:51:45

回答

2

的logback使用Layout interfacedoLayout()方法的實現翻譯事件記錄到字符串可以輸出,如the documentation概述。 Dropwizard provides an extensionPatternLayout class(其反過來abstract PatternLayoutBase class的擴展):

PatternLayout需要記錄事件並返回一個String。不過, 這個String可以通過調整PatternLayout的轉換 模式來定製。

public class DropwizardLayout extends PatternLayout { 
    public DropwizardLayout(LoggerContext context, TimeZone timeZone) { 
     super(); 
     setOutputPatternAsHeader(false); 
     getDefaultConverterMap().put("ex", PrefixedThrowableProxyConverter.class.getName()); 
     getDefaultConverterMap().put("xEx", PrefixedExtendedThrowableProxyConverter.class.getName()); 
     getDefaultConverterMap().put("rEx", PrefixedRootCauseFirstThrowableProxyConverter.class.getName()); 
     setPattern("%-5p [%d{ISO8601," + timeZone.getID() + "}] %c: %m%n%rEx"); 
     setContext(context); 
    } 
} 

這樣做是:

  1. 確保outputPatternAsHeader被禁用,這是輸出要在任何日誌的頂部的字符串模式的標誌。確認所用模式的一種方法是在appender上啓用此標誌,如the logback documentation中所述。
  2. 覆蓋與打印異常相關的若干conversion words以使用an implementation of ThrowableProxyConverter,該堆棧跟蹤帶有感嘆號。
  3. 使用傳入的時區將模式設置爲"%-5p [%d{ISO8601," + timeZone.getID() + "}] %c: %m%n%rEx"。如果不顯式添加時區信息,則logback將使用JVM的時區或GMT。如果這是可以接受的,您可以使用%d,因爲ISO8601格式是默認格式。
  4. 設置上下文。