logback 生成日志

<?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>

posted @ 2017-07-17 13:48  try_do_it  阅读(1044)  评论(1编辑  收藏  举报