logback 日志配置

写入文件配置

1
2
3
4
5
6
7
8
9
10
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
</encoder>
</appender>

写入mysql数据库配置

1
2
3
4
5
6
7
8
9
10
11
12
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<!--日志异步到数据库 -->
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<!--连接池 -->
<dataSource class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<driverClassName>com.mysql.jdbc.Driver</driverClassName>
<url>jdbc:mysql://localhost:3306/DB</url>
<username >root</username>
<password>root</password>
</dataSource>
</connectionSource>
</appender>

写入控制台配置

1
2
3
4
5
6
7
8
9
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 日志输出编码 -->
<Encoding>UTF-8</Encoding>
<layout class="ch.qos.logback.classic.PatternLayout">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</layout>
</appender>

异步写入

<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
      <queueSize>512</queueSize>
      <appender-ref ref="FILE"/>
</appender>