具体介绍,请参考上一篇文章
http://phl.iteye.com/blog/1520693
本文主要介绍
1.环境变量用法
2.按大小切割日志
3.按时间切割日志的用法
其中代码部分,经过本人亲自运行验证,有疑问请随时跟帖,笔者有时间会答复大家
http://phl.iteye.com/blog/1520693
本文主要介绍
1.环境变量用法
2.按大小切割日志
3.按时间切割日志的用法
其中代码部分,经过本人亲自运行验证,有疑问请随时跟帖,笔者有时间会答复大家
package com.chinaso.phl;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
public class Test {
public static void main(String[] args) throws Exception {
System.setProperty("LOG_HOME", System.getProperty("user.dir") + File.separator + "target");
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
String path = java.net.URLDecoder.decode(Test.class.getResource("/").getPath(), "utf8") + "logback.xml";
configurator.doConfigure(path);
Logger logger = LoggerFactory.getLogger(Test.class);
for (int i = 0, count = 1024* 30 ; i < count; i++) {
logger.info("hello logback!");
logger.info("classpath is {}", path);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<!-- appender stdout-->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- 最简单的输出 只有时间和等级
<pattern>%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n</pattern> -->
<!-- 带时间、等级、类名 -->
<!-- %p和%level都是表示等级,其中%-5表示长度,无字符则用空白补齐 -->
<!-- %L表示行号 -->
<!-- %m是消息,%n是换行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss:SSS}[%-5p][%logger][%-4L] - %m%n</pattern>
</encoder>
</appender>
<!-- appender stdout-->
<appender name="stdout2" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%d{yyyy-MM-dd HH:mm:ss:SSS}[%p][%logger][seq:%X{seq}] - %m%n</Pattern>
</encoder>
</appender>
<!-- appender 大小滚动滚动日志,
16:21:54,643 |-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@9c0ec97 - Large window sizes are not allowed.
16:21:54,643 |-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@9c0ec97 - MaxIndex reduced to 21
-->
<appender name="main" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 此文件配置,不响应滚动日志的触发与生成 -->
<file>${LOG_HOME}/logs/main.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<!-- 此处使用环境变量,也可以使用绝对路径 -->
<FileNamePattern>${LOG_HOME}/logs/main%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>21</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>1MB</MaxFileSize>
</triggeringPolicy>
<encoder>
<Pattern>[%d{yyyyMMdd HH:mm:ss.SSS}] [%-5level] [%-30logger{0}][%-3L] [SeqId:%X{SeqId}] - %m%n</Pattern>
</encoder>
</appender>
<!-- appender 时间滚动日志 -->
<appender name="mainTime" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/logs/main-date.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 此处的时间分隔符只能用中划线 -->
<FileNamePattern>${LOG_HOME}/logs/main-%d{yyyyMMdd-HHmmss}.log</FileNamePattern>
<maxHistory>365</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>[%d{yyyyMMdd HH:mm:ss.SSS}] [%-5level] [%-30logger{0}][%-3L] [SeqId:%X{SeqId}] - %m%n</Pattern>
</encoder>
</appender>
<!-- logger 自定义。 通过 LoggerFactory.getLogger(name)取得 -->
<logger name="myLog" additivity="true" level="info">
<appender-ref ref="stdout2" />
</logger>
<!-- 限制包里面输出日志 -->
<logger name="com.panguso.phl" level="off">
<appender-ref ref="stdout" />
</logger>
<!-- root 默认日志配置 -->
<root level="info">
<appender-ref ref="stdout" />
</root>
</configuration>