logback怎样通过http访问log
发布网友
发布时间:2022-05-01 21:55
我来回答
共1个回答
热心网友
时间:2022-06-24 01:18
为了方便查看和归档:
不同包的日志可能要放到不同的文件中,如service层和层的日志;
不同日志级别:调试、信息、警告和错误等也要分文件输出。
一、根据包输出,通过logger的name属性指定不同的包
1. 创建不同包下的两个类:
Java代码
package com.john.service;
@Service
public class ObligationService {
static final Logger log = LoggerFactory.getLogger(ObligationService.class);
@Resource
ObligationDao ;
public void obligate() {
log.debug("obligate start...");
.queryObligations();
log.error("An error occurred ring the progress");
log.debug("obligate end...");
}
}
Java代码
package com.john.;
@Repository
public class ObligationDao {
static final Logger log = LoggerFactory.getLogger(ObligationDao.class);
public void queryObligations() {
log.debug("query obligations start...");
log.error("An error occurred ring the query");
log.debug("query obligations end...");
}
}
2. 在logback(-test).xml中添加:
Xml代码
<appender name="serviceAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/logs/bribery.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/bribery-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/logs/venality.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/venality-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.john.service" level="DEBUG" additivity="false">
<appender-ref ref="serviceAppender" />
</logger>
<logger name="com.john." level="DEBUG" additivity="false">
<appender-ref ref="Appender" />
</logger>
在类路径下创建Spring的beans.xml文件:(beans节点省略)
Xml代码
<context:component-scan base-package="com.john" />
3. 测试方法:
Java代码
@Test
public void dispatchByPackageTest() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ObligationService service = ctx.getBean(ObligationService.class);
service.obligate();
}
执行测试后,
bribery.log:
15:59:59.839 [main] DEBUG c.j.s.ObligationService - obligate start...
15:59:59.844 [main] ERROR c.j.s.ObligationService - An error occurred ring the progress
15:59:59.844 [main] DEBUG c.j.s.ObligationService - obligate end...
venality.log:
15:59:59.844 [main] DEBUG c.j.d.ObligationDao - query obligations start...
15:59:59.844 [main] ERROR c.j.d.ObligationDao - An error occurred ring the query
15:59:59.844 [main] DEBUG c.j.d.ObligationDao - query obligations end...
二、根据日志级别输出
这里把所有级别为ERROR的日志输出到一个文件中
1. 修改logback(-test).xml文件
serviceAppender和Appender都加入以下级别过滤器,这样错误日志就不会在bribery.log和venality.log中打印了:
Xml代码
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
新建一个错误日志的appender:
Xml代码
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/logs/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/venality-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %thread %X{invokeNo} %logger{40} %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
name为com.john.和com.john.service的logger都加上errorAppender:
Xml代码
<appender-ref ref="errorAppender" />
2. 执行上面的测试方法,service和的错误日志都打印到error.log文件中了:
16:12:51,433 main com.john..ObligationDao An error occurred ring the query
16:12:51,433 main com.john.service.ObligationService An error occurred ring the progress