欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

logback

程序员文章站 2022-07-03 15:50:36
...
今天用了下logback,发现是比较好。优势不多说,直接进入正题:
使用:

[img]http://dl2.iteye.com/upload/attachment/0085/1847/14d9314b-80ed-33ea-9ab4-a1cb61b4f997.png[/img]
部署在lib包下


上一篇说过了 这里的log4j-over-slf4j就是用在这个时候,如果版本不对,运行的时候就会报出版本不匹配,

logback.xml:是在src的目录下
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <consolePlugin silent="true" />consolePlugin是用于Eclipse的logback插件,默认会去连接localhost的4321端口,如果不需要直接拿掉-->
<substitutionProperty name="log_path" value="../elearinglog" />
<!-- Appenders http://logback.qos.ch/manual/appenders.html -->


<appender name="syslogfile" class="ch.qos.logback.classic.sift.SiftingAppender">

<discriminator>
<Key>FILE_PATH</Key>
<DefaultValue>sysLog</DefaultValue>
</discriminator>
<sift>
<appender name="FILEx"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${FILE_PATH}.log</File>
<Append>true</Append>
<rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${FILE_PATH}.%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>5000</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%date - %msg%n
</Pattern>
</layout>
</encoder>
</appender>
</sift>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="ERRORFILE" />
<!-- <appender-ref ref="DEBUG_CONSOLE" /> -->
</root>


<!-- myweb app -->
<logger name="MySysLogger">
<level value="INFO" />
<appender-ref ref="syslogfile" />
</logger>


</configuration>


java对象:
package app;

import org.slf4j.MDC;

public class User {
public static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("MySysLogger");

public static void main(String[] args) {
User u = new User();
u.p();
}

public void p() {
// TODO Auto-generated method stub
String msg="Here is DEBUG messgae";

try {
startLogging("H:\\system");
logger.info(msg);



} catch (Exception e) {

e.printStackTrace();
} finally {

stopLogging();
}

}


private static void startLogging(String sysLogPath) throws Exception {

MDC.put("FILE_PATH", sysLogPath);
//MDC.put(FROM_USER, fromNmae);
//MDC.put(TO_USER, toName);
}

private static void stopLogging() {

MDC.remove("FILE_PATH");
}

}


日志结果是2013-05-29 22:20:12,733 - Here is DEBUG messgae。


配置参数详解:http://blog.sina.com.cn/s/blog_9ed9ac7d01016ja1.html