logback的配置文件位置

参考文档:http://logback.qos.ch/manual/configuration.html
对于maven项目,将测试用的配置文件logback-test.xml放在test/resources/下即可.
Logback can be configured either programmatically or with a configuration script expressed in XML or Groovy format. By the way, existing log4j users can convert their log4j.properties files to logback.xml using our PropertiesTranslator web-application.

logback按如下顺序寻找配置文件,
Let us begin by discussing the initialization steps that logback follows to try to configure itself:
1) Logback tries to find a file called logback.groovy in the classpath.
2)If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
3)If no such file is found, it checks for the file logback.xml in the classpath..
4)If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

The fourth and last step is meant to provide a default (but very basic) logging functionality in the absence of a configuration file.
If you are using Maven and if you place the logback-test.xml under the src/test/resources folder, Maven will ensure that it won't be included in the artifact produced. Thus, you can use a different configuration file, namely logback-test.xml during testing, and another file, namely, logback.xml, in production. The same principle applies by analogy for Ant.

配置文件模板:


<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>myApp.log</file>

    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <logger name="org.hibernate">
    <appender-ref ref="STDOUT" />
  </logger>
  
  <logger name="com.test">
    <appender-ref ref="FILE" />
  </logger>  
  
  <root level="debug">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

你可能感兴趣的:(log4j,xml,logback,配置,slf4j)