日志:logback的使用

logback

logback的使用要和slf4j一起使用,需要的jar为

slf4j-api.jar,logback-core.jar,logback-classic.jar

maven依赖为:

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <logback.version>1.1.7logback.version>
    <slf4j.version>1.7.21slf4j.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>${slf4j.version}version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
      <version>${logback.version}version>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>${logback.version}version>
      dependency>
  dependencies>

常用配置:



<configuration debug="false" scanPeriod="60 seconds">
    
    <property name="LOG_HOME" value="./logs"/>
    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg %npattern>
        encoder>
    appender>
    
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <FileNamePattern>${LOG_HOME}/runtime.log.%d{yyyy-MM-dd}.logFileNamePattern>
            
            <MaxHistory>30MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MBMaxFileSize>
        triggeringPolicy>
    appender>

    
    <logger name="haha" level="INFO" additivity="false">
        <appender-ref ref="FILE"/>
    logger>
    
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        
    root>
configuration>

具体的使用和其他日志无异。

你可能感兴趣的:(日志)