logback.xml配置如何按天输出日志文件

前端之家收集整理的这篇文章主要介绍了logback.xml配置如何按天输出日志文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

logback.xml配置如何按天输出日志文件

转载自http://my.oschina.net/bootstrap/blog/414079
摘要 logback.xml配置如何按天输出日志文件

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<appender name="stdout"

class="ch.qos.logback.core.ConsoleAppender">

<layout class="ch.qos.logback.classic.PatternLayout">

<Pattern>

%d{HH:mm:ss} [%thread] %-5level %logger{80} - %msg%n

</Pattern>

</layout>

</appender>

<appender name="R"

class="ch.qos.logback.core.rolling.RollingFileAppender">

<File>../ptvslog/server.log</File>

<layout class="ch.qos.logback.classic.PatternLayout">

<Pattern>

%d{HH:mm:ss} [%thread] %-5level %logger{80} - %msg%n

</Pattern>

</layout>

<rollingPolicy

class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">

<maxIndex>2</maxIndex>

<FileNamePattern>../ptvslog/server.log.%i</FileNamePattern>

</rollingPolicy>

<triggeringPolicy

class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">

<MaxFileSize>50MB</MaxFileSize>

</triggeringPolicy>

</appender>

(1)第一种配置如下时,将会使日志文件往后累加,而不是每日只生成当日的日志文件

<appender name="logfile"

class="ch.qos.logback.core.rolling.RollingFileAppender">

<File>../ptvslog/access.log</File>

<rollingPolicy

class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

<FileNamePattern>

../ptvslog/access.%d{yyyy-MM-dd}.log

</FileNamePattern>

<!-- keep 1 days worth of history -->

<MaxHistory>1</MaxHistory>

</rollingPolicy>

<layout class="ch.qos.logback.classic.PatternLayout">

<Pattern>

%d{yyyy-MM-dd HH:mm:ss} -%msg%n

</Pattern>

</layout>

</appender>

(2)第二种配置如下时,按天生成日志文件,每天的日志文件里只保存当天的日志信息

<appender name="logfile2"

class="ch.qos.logback.core.rolling.RollingFileAppender">

<Prudent>true</Prudent>

<rollingPolicy

class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

<FileNamePattern>

../ptvslog/access.%d{yyyy-MM-dd}.log

</FileNamePattern>

<!-- keep 60 days worth of history -->

<MaxHistory>60</MaxHistory>

</rollingPolicy>

<layout class="ch.qos.logback.classic.PatternLayout">

<Pattern>

%d{yyyy-MM-dd HH:mm:ss} -%msg%n

</Pattern>

</layout>

</appender>

(2)第三种配置如下时,按天生成文件夹,每个文件夹里放当天的日志文件

<appender name="logDirfile"

class="ch.qos.logback.core.rolling.RollingFileAppender">

<Prudent>true</Prudent>

<rollingPolicy

class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

<FileNamePattern>

../ptvslog/%d{yyyy-MM-dd}/access.%d{yyyy-MM-dd}.log

</FileNamePattern>

</rollingPolicy>

<layout class="ch.qos.logback.classic.PatternLayout">

<Pattern>

%d{yyyy-MM-dd HH:mm:ss} -%msg%n

</Pattern>

</layout>

</appender>

以下配置表示DataController类的INFO日志以logDirfile输出,additivity="false"表示只在

logDirfile输出,不在其他输出输出,如果设置为true,表示要在其他地方也输出

<logger name="com.apabi.ptvs.web.DataController"

additivity="false">

<level value="INFO" />

<appender-ref ref="logDirfile" />

</logger>

<root level="INFO">

<appender-ref ref="stdout" />

<appender-ref ref="R" />

</root>

</configuration>

原文链接:https://www.f2er.com/xml/295849.html

猜你在找的XML相关文章