将所有内容记录到文件中

时间:2014-03-23 14:27:12

标签: java configuration logback

我有一个这样的logback配置文件:http://pastebin.com/yZGVWKEV

我的问题是我登出的内容会被记录到文件中,但是其他库(例如spring和logback)正在注销它没有。

它们只出现在控制台上,但是格式不是我定义的格式:

15:07:25,226 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [.../logback-local-config.xml] at [file:.../logback-local-config.xml]
15:07:25,352 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:07:25,356 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
15:07:25,397 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:07:25,462 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:07:25,467 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [INFO_FILE]
15:07:25,495 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use gz compression
15:07:25,498 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern ./logs/%d{yyyy,aux}/%d{MM,aux}/service.%d{yyyyMMdd}.%d{HHmmssSSS,aux}-debug.log for the active file
15:07:25,504 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyyMMdd' from file name pattern './logs/%d{yyyy,aux}/%d{MM,aux}/service.%d{yyyyMMdd}.%d{HHmmssSSS,aux}-debug.log.gz'.
15:07:25,504 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:07:25,508 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sun Mar 23 15:05:59 CET 2014
15:07:25,510 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:07:25,514 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO_FILE] - Active log file name: ./logs/service-debug.log
15:07:25,514 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO_FILE] - File property is set to [./logs/service-debug.log]
15:07:25,516 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:07:25,516 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [DEBUG_FILE]
15:07:25,518 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use gz compression
15:07:25,521 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern ./logs/%d{yyyy,aux}/%d{MM,aux}/service.%d{yyyyMMdd}.%d{HHmmssSSS,aux}-info.log for the active file
15:07:25,522 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyyMMdd' from file name pattern './logs/%d{yyyy,aux}/%d{MM,aux}/service.%d{yyyyMMdd}.%d{HHmmssSSS,aux}-info.log.gz'.
15:07:25,522 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:07:25,522 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sun Mar 23 15:05:59 CET 2014
15:07:25,523 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:07:25,524 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[DEBUG_FILE] - Active log file name: ./logs/service-info.log
15:07:25,524 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[DEBUG_FILE] - File property is set to [./logs/service-info.log]
15:07:25,525 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework.beans.factory.xml] to DEBUG
15:07:25,525 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - ch.* level set to DEBUG
15:07:25,526 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.* level set to DEBUG
15:07:25,526 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - org.* level set to DEBUG
15:07:25,526 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
15:07:25,526 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
15:07:25,527 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [INFO_FILE] to Logger[ROOT]
15:07:25,527 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [DEBUG_FILE] to Logger[ROOT]
15:07:25,527 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:07:25,528 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@4adde028 - Registering current configuration as safe fallback point
márc. 23, 2014 3:07:25 DU org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [.../application.xml]
márc. 23, 2014 3:07:25 DU org.springframework.context.support.GenericApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@38987d26: startup date [Sun Mar 23 15:07:25 CET 2014]; root of context hierarchy
márc. 23, 2014 3:07:25 DU org.springframework.beans.factory.config.PropertyPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [.../service.properties]
2014-03-23 15:07:25.985 [main] INFO  ....Main$ - Service startup successful...

我怎样才能让所有内容都以正确的格式记录到文件中?

1 个答案:

答案 0 :(得分:0)

要将spring logging配置为logback,您需要执行以下步骤。

  1. 排除Spring使用commons-logging的spring日志框架,排除commons-dependency jar。

     <exclusions>
        <!-- Exclude Commons Logging in favor of SLF4j -->
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
    
  2. 现在使用jcl来使用slfj进行spring。

     <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>jcl-over-slf4j</artifactId>
           <version>${slf4j.version}</version>
     </dependency>
    
  3. 现在将spring log level添加到logback.xml。

    <logger name="org.springframework">
        <level value="INFO" />
    </logger>