JFinal使用log4j2记录日志和sql

田成化
2023-12-01

pom.xml中加入依赖:

<dependency>
    <groupId>cn.dreampie</groupId>
    <artifactId>jfinal-slf4j</artifactId>
    <version>0.1</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<!--桥接器:告诉slf4j使用Log4j2 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.11.0</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
<!--具体实现,log4j2 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.0</version>
</dependency>

模仿自带的Log4jLog类实现Log类

package util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.jfinal.log.Log;

public class Log4j2Log extends Log{
	private Logger log;
	
	public Log4j2Log(Class<?> clazz) {
		log = LogManager.getLogger(clazz);
	}
	
	public Log4j2Log(String name) {
		log = LogManager.getLogger(name);
	}

	@Override
	public void debug(String message) {
		log.debug(message);
	}

	@Override
	public void debug(String message, Throwable t) {
		log.debug(message, t);
	}

	@Override
	public void info(String message) {
		log.info(message);
	}

	@Override
	public void info(String message, Throwable t) {
		log.info(message, t);
	}

	@Override
	public void warn(String message) {
		log.warn(message);
	}

	@Override
	public void warn(String message, Throwable t) {
		log.warn(message, t);
	}

	@Override
	public void error(String message) {
		log.error(message);
	}

	@Override
	public void error(String message, Throwable t) {
		log.error(message, t);
	}

	@Override
	public void fatal(String message) {
		log.fatal(message);
	}

	@Override
	public void fatal(String message, Throwable t) {
		log.fatal(message, t);
	}

	@Override
	public boolean isDebugEnabled() {
		return false;
	}

	@Override
	public boolean isInfoEnabled() {
		return false;
	}

	@Override
	public boolean isWarnEnabled() {
		return false;
	}

	@Override
	public boolean isErrorEnabled() {
		return false;
	}

	@Override
	public boolean isFatalEnabled() {
		return false;
	}

}

实现ILogFactory接口

package util;

import com.jfinal.log.ILogFactory;
import com.jfinal.log.Log;

public class Log4j2Factory implements ILogFactory{

	public Log4j2Factory() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Log getLog(Class<?> clazz) {
		// TODO Auto-generated method stub
		return new Log4j2Log(clazz);
	}

	@Override
	public Log getLog(String name) {
		// TODO Auto-generated method stub
		return new Log4j2Log(name);
	}

}

在configConstant中配置

public void configConstant(Constants me) {
    ...
    me.setLogFactory(new Log4j2Factory());
}

配置文件log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="log_path">${sys:catalina.home}/logs/zcrTest/</Property>
    </Properties>
 
    <Appenders>
        <!-- 控制台 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
        </Console>
        <!-- 文件日志 -->
        <RollingFile name="DailyRollingFile" fileName="${log_path}/mylog.log"
            filePattern="${log_path}/mylog.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
            <TimeBasedTriggeringPolicy  interval="1"/>
      <SizeBasedTriggeringPolicy size="10 MB" /> 
        </RollingFile>
        <!-- sql日志 -->
        <RollingFile name="Sql" fileName="${log_path}/sql.log"
            filePattern="${log_path}/sql.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
            <TimeBasedTriggeringPolicy  interval="1"/>
      <SizeBasedTriggeringPolicy size="10 MB" /> 
        </RollingFile>
    </Appenders>
    <Loggers>
        <logger name="druid.sql.Statement" level="debug" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="Sql"/>
        </logger>
        <logger name="druid.sql.ResultSet" level="debug" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="Sql"/>
        </logger>
        <Logger name="mylog" level="trace" additivity="false">
            <appender-ref ref="Console"/>  
       <appender-ref ref="DailyRollingFile" />  
     </Logger>

        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="DailyRollingFile"/>
        </Root>
    </Loggers>
</Configuration>

 

 类似资料: