Java代码
- package com.john.service;
- public class ObligationService {
- static final Logger log = LoggerFactory.getLogger(ObligationService.class);
- ObligationDao dao;
- public void obligate() {
- log.debug("obligate start...");
- dao.queryObligations();
- log.error("An error occurred during the progress");
- log.debug("obligate end...");
- }
- }
Java代码
- package com.john.dao;
- @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 during the query");
- log.debug("query obligations end...");
- }
- }
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="daoAppender" 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.dao" level="DEBUG" additivity="false">
- <appender-ref ref="daoAppender" />
- </logger>
Xml代码
- <context:component-scan base-package="com.john" />
Java代码
- public void dispatchByPackageTest() {
- AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- ObligationService service = ctx.getBean(ObligationService.class);
- service.obligate();
- }
Xml代码
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <level>ERROR</level>
- <onMatch>DENY</onMatch>
- <onMismatch>ACCEPT</onMismatch>
- </filter>
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>
Xml代码
- <appender-ref ref="errorAppender" />