复制代码
为懒人提供无限可能,生命不息,code不止
人类感性的情绪,让我们知难行难
首页
前端
后台
数据库
运维
资源下载
实用工具
登录
注册
性能优化
【原创】logback日志切割(按时间,文件大小分割,按业务模块生成多个日志文件)
作者: whooyun
发表于:
2017-02-27 11:09
1、日志都输入到一个文件,控制台,并且按天和文件大小进行日志切割;
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="logfile_name" value="loanms"></property>
<property name="logfile_path" value="/data/whydubbo_log"></property>
<timestamp key="byhour" dataPattern="yyyy-MM-dd"></timestamp>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<!-- output log to console,打印到catalina.out begin -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d{yyyy-mm-dd HH:MM:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</layout>
</appender>
<!-- output log to console,打印到catalina.out end -->
<!-- output log to file -->
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- logfile's name. use An absolute path or an Relative path -->
<File>${logfile_path}/${logfile_name}.log</File>
<!--zip log, -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件归档文件名 按天来回滚{yyyy-MM-dd},如果需要按小时来回滚,则设置为{yyyy-MM-dd_HH} -->
<FileNamePattern>${logfile_path}/${logfile_name}.%d{yyyy-MM-dd}.%i.log
</FileNamePattern>
<!--日滚,如果超过5天,则会将前面1天的日志文件删除 -->
<MaxHistory>7</MaxHistory>
<!--按文件大小滚动,日志文件超过设置大小则归档一次-->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB 就归档-->
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<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>
<!--其它框架的日志-->
<logger name="java.sql.Connection">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis.common.jdbc.SimpleDataSource">
<level value="DEBUG" />
</logger>
<root>
<level value="DEBUG" />
<!-- output log to console,打印到catalina.out begin -->
<appender-ref ref="STDOUT" />
<!-- output log to console,打印到catalina.out begin -->
<appender-ref ref="FILE" />
</root>
<!--<include file="/tmp/logback-demo.xml"/> -->
</configuration>
2、日志按业务模块输入到各个文件,一个控制台,并且按天和文件大小进行日志切割;
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="logfile_name" value="whydubbo_loan"></property>
<property name="logfile_path" value="/data/whydubbo_log"></property>
<timestamp key="byhour" dataPattern="yyyy-MM-dd"></timestamp>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<!-- output log to console,打印到catalina.out begin -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d{yyyy-mm-dd HH:MM:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</layout>
</appender>
<!-- output log to console,打印到catalina.out end -->
<appender name="loanAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- logfile's name. use An absolute path or an Relative path -->
<File>${logfile_path}/${logfile_name}.log</File>
<!--zip log, -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件归档文件名 按天来回滚{yyyy-MM-dd},如果需要按小时来回滚,则设置为{yyyy-MM-dd_HH} -->
<FileNamePattern>${logfile_path}/${logfile_name}.%d{yyyy-MM-dd}.%i.log
</FileNamePattern>
<!--日滚,如果超过7天,则会将前面1天的日志文件删除 -->
<MaxHistory>7</MaxHistory>
<!--按文件大小滚动,日志文件超过设置大小则归档一次-->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB 就归档-->
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<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>
<!--其它框架的日志-->
<logger name="java.sql.Connection">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis.common.jdbc.SimpleDataSource">
<level value="DEBUG" />
</logger>
<!-- 可以按业务模块输出日志,建议使用此种方法,这样有利于快速定位日志-->
<logger name="com.whydubbo.loan" level="DEBUG" additivity="false">
<appender-ref ref="loanAppender" />
</logger>
<root>
<level value="DEBUG" />
<!-- output log to console,打印到catalina.out begin -->
<appender-ref ref="STDOUT" />
</root>
<!--<include file="/tmp/logback-demo.xml"/> -->
</configuration>