pom.xml里面需要引用相应的jar包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
<type>jar</type>
</dependency>
logback会自动去寻找logback-test.xml和logback.xml配置文件,如果都不存在,使用默认配置。
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property resource="config.properties"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level][%date]--%msg--[%M][%F{16},%L]%n</pattern>
</encoder>
</appender>
<appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}lf.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}lf.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>[%-5level][%date]--%msg--[%M][%F{16},%L]%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- project default level -->
<logger name="lf" level="${log.level}" />
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="rollingFile" />
</root>
</configuration>
config.properties相应参数设置
#log
log.level=DEBUG
log.path=/home/logs/
测试
在UserServiceImpl中测试
@Service
public class UserServiceImpl implements UserService{
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Autowired
UserDao userDao;
@Override
public Page<User> selectUserByCon(Map<String, Object> paramMap) {
Page<User> list = null;
try{
list=userDao.selectUserByCon(paramMap);
}catch (DataAccessException e){
}
LOG.info("selectUserByCon error.");
return list;
}
}
执行结果