2016-03-23 74 views
0

我需要爲我的應用程序設置最大文件大小,但目前我使用的是Dropwizard核心版本0.8.4,其文件appender不支持此功能。Dropwizard自定義記錄器不會將日誌轉儲到文件中

因此,我通過編寫一個自定義appender來更新到最新的dropwizard(它支持我的需要)版本,而不是現在的選項。

private void initLogging(Configuration configuration) throws JoranException { 
    final File logDir = new File("/tmp/enforcer"); 
    final File logFile = new File(logDir, "wallet.log"); 

    final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); 
    RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>(); 
    rollingFileAppender.setFile(logFile.getAbsolutePath()); 
    rollingFileAppender.setName("com.documents4j.logger.server.file"); 
    rollingFileAppender.setContext(loggerContext); 

    FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy(); 
    fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() +"%i.gz"); 
    fixedWindowRollingPolicy.setMaxIndex(7); 
    fixedWindowRollingPolicy.setContext(loggerContext); 
    fixedWindowRollingPolicy.setParent(rollingFileAppender); 
    fixedWindowRollingPolicy.start(); 

    SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>(); 
    sizeBasedTriggeringPolicy.setMaxFileSize("2KB"); 
    sizeBasedTriggeringPolicy.setContext(loggerContext); 
    sizeBasedTriggeringPolicy.start(); 



    rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy); 
    rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy); 

    rollingFileAppender.start(); 

    System.out.println("Logging: The log is written to " + logFile); 
    final ch.qos.logback.classic.Logger log = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); 
    log.setLevel(Level.DEBUG); 
    log.addAppender(rollingFileAppender); 
} 


@Override 
public void run(Configuration configuration, Environment environment) throws Exception 
{ 

    initLogging(configuration); 
} 

我YAML文件中的配置是

logging: 
    level: INFO 
    org.springframework.retry.support.RetryTemplate: DEBUG 
    appenders: 
    - type: file 
    currentLogFilename: /tmp/enforcer.log 
    threshold: ALL 
    archive: true 
    archivedLogFilenamePattern: /tmp/enforcer-%d.log 
    archivedFileCount: 5 
    timeZone: UTC 
    logFormat: '%-5level [%date{yyyy-MM-dd HH:mm:ss,SSS}] [%X{realdocRequestId}] %logger{15}: %m%n' 

現在,當我運行我的應用我注意到,儘管自定義日誌文件(/tmp/enforcer/wallet.log)在特定目錄中創建,但實際日誌不會被轉儲,即wallet.log文件大小爲0 kb,其中隨着yaml中配置的日誌文件被創建並且size爲特定kb並隨着日誌事件的產生而增加。

我無法弄清楚我在做什麼錯,幫助將不勝感激。

+1

所有dropwizard版本支持自定義記錄程序。你只需要創建它們並告訴DW他們。我已經演示瞭如何在這裏:http://stackoverflow.com/questions/27483442/dropwizard-doesnt-log-custom-loggers-to-file/33085996#33085996你不需要覆蓋工廠。您只需要創建一個新的Logger impl,正確註釋它並將其添加到META-INF位置,以便DW可以找到並初始化它。 – pandaadb

+0

@pandaadb感謝您的答案,這有幫助,但我想知道爲什麼我的代碼失敗??什麼我想在這裏失蹤? – RIPAN

回答

1

您的記錄器不會記錄任何內容,因爲您從不設置編碼器。設置一個斷點:

OutputStreamAppender#start(),你會看到沒有編碼器。

它會工作,一旦你添加:

 LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>(); 
     DropwizardLayout formatter = new DropwizardLayout(loggerContext, TimeZone.getDefault()); 
     formatter.setPattern("[%level] [%h] %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} [%thread] [%logger] %msg%n"); 
     layoutEncoder.setLayout(formatter); 
     formatter.start(); 
     rollingFileAppender.setEncoder(layoutEncoder); 

當然,你可以定義你喜歡的任何格式(和格式)。

但請記住,這是你嘗試實現的一個相當不好的例子。您現在在配置日誌記錄(DW和您自己的)的代碼中有2個點。你有yaml配置日誌以及你自己的。我建議投入一些工作並添加一個可以使用的AppenderFactory。

希望幫助,

阿圖爾

+0

非常感謝@pandaadb,它工作。 – RIPAN

相關問題