引言
之前项目用的是Log4j 1.x,log4j2是log4j 1.x和logback的改进版,据说采用了一些新技术(无锁异步、等等),使得日志的吞吐量、性能比log4j 1.x提高10倍,并解决了一些死锁的bug,而且配置更加简单灵活。
由于Spring4中log4j2的配置繁琐,索性连Spring也一起升级到了5,以下是基于Spring5.2.2.RELEASE进行配置。
1. pom.xml 依赖
添加以下依赖
2.12.1
org.apache.logging.log4j
log4j-core
${log4j2.version}
org.apache.logging.log4j
log4j-api
${log4j2.version}
org.apache.logging.log4j
log4j-web
${log4j2.version}
复制代码
2. web.xml配置
如果是servlet3.0可以省略listener的配置,如果是2.5则必须添加。
日志文件名是log4j2.xml,这里我放在了src/main/resources/下。
org.apache.logging.log4j.web.Log4jServletContextListener
log4jConfiguration
classpath:/log4j2.xml
log4jContextName
star
log4jConfiguration
classpath:/log4j2.xml
复制代码
3. log4j2.xml
具体配置网上教程很多,这里就不再解释。
/var/log/siled/web
web-app
web-alm
web-ope
%d %5p [%T]%c{1}#%M %m%n
%d ALERM [%T]%c{1}#%M %m%n
fileName="${logdir}/${logfile-app}.log"
filePattern="${logdir}/${logfile-app}.%d{yyyyMMdd}.log">
fileName="${logdir}/${logfile-alm}.log"
filePattern="${logdir}/${logfile-alm}.%d{yyyyMMdd}.log">
fileName="${logdir}/${logfile-ope}.log"
filePattern="${logdir}/${logfile-ope}.%d{yyyyMMdd}.log">
复制代码
4. 使用Logger输出应用日志
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private Logger logger = LogManager.getLogger(this.getClass());
public class RequestInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
logger.info("username:{}" , request.getSession().getAttribute("username"));
logger.info("getRemoteUser:{}" , request.getRemoteUser());
logger.info("getRequestURI{}" , request.getRequestURI());
return true;
}
}
复制代码
5.配置Mybatis
以上只是输出了应用日志,但是SQL执行日志并没有输出。所以必须对Mybatis进行设置。
5.1 配置SqlSessionFactory
MybatisSqlSessionFactoryBean的configLocation属性设置为mybatis-config.xml的内容。注意,我这里用的其实是Mybatis Plus,如果是普通的Mybatis就设置对应的SqlSessionFactory。
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
// 添加这一句
复制代码
5.2配置mybatis-config.xml
src/main/resources下创建mybatis-config.xml,写入以下配置。
复制代码
这样SQL也能正常输出了。来看一下效果。
2019-12-13 16:56:38,577 DEBUG [55]DataSourceTransactionManager#getTransaction Creating new transaction with name [com.cuts.siled.service.impl.demo.DemoMybatisPlusServiceImpl.selectByMap]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2019-12-13 16:56:39,056 DEBUG [55]DataSourceTransactionManager#doBegin Acquired Connection [765858464, URL=jdbc:postgresql://localhost:5432/postgres, UserName=siled, PostgreSQL JDBC Driver] for JDBC transaction
2019-12-13 16:56:39,065 DEBUG [55]DataSourceTransactionManager#doBegin Switching JDBC Connection [765858464, URL=jdbc:postgresql://localhost:5432/postgres, UserName=siled, PostgreSQL JDBC Driver] to manual commit
2019-12-13 16:56:39,087 DEBUG [55]SqlSessionUtils#debug Creating a new SqlSession
2019-12-13 16:56:39,102 DEBUG [55]SqlSessionUtils#debug Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18d6733]
2019-12-13 16:56:39,190 DEBUG [55]SpringManagedTransaction#debug JDBC Connection [765858464, URL=jdbc:postgresql://localhost:5432/postgres, UserName=siled, PostgreSQL JDBC Driver] will be managed by Spring
2019-12-13 16:56:39,197 DEBUG [55]selectByMap#debug ==> Preparing: SELECT username,password,auth,updts FROM m_user WHERE auth = ? AND deleted='0'
2019-12-13 16:56:39,203 DEBUG [55]selectByMap#debug ==> Parameters: admin(String)
2019-12-13 16:56:39,262 DEBUG [55]selectByMap#debug <== Total: 1
复制代码