首先引入jar包
<properties> <log4j2.version>2.16.0</log4j2.version> <slf4j.version>1.7.25</slf4j.version> </properties> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <!--slf4j use Log4j2--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2.version}</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>${log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2.version}</version> </dependency>
添加和配置log4j2实现类
// configConstant中配置日志系统实现类 me.setLogFactory(new Log4j2LogFactory()); SqlReporter.setLog(true); // sql日志
import com.jfinal.log.ILogFactory; import com.jfinal.log.Log; /** * Log4j2LogFactory. */ public class Log4j2LogFactory implements ILogFactory { public Log getLog(Class<?> clazz) { return new Log4j2Log(clazz); } public Log getLog(String name) { return new Log4j2Log(name); } }
import com.jfinal.log.Log; import org.apache.logging.log4j.Level; public class Log4j2Log extends Log { private org.apache.logging.log4j.Logger log; Log4j2Log(Class<?> clazz) { log = org.apache.logging.log4j.LogManager.getLogger(clazz); } Log4j2Log(String name) { log = org.apache.logging.log4j.LogManager.getLogger(name); } public static Log4j2Log getLog(Class<?> clazz) { return new Log4j2Log(clazz); } public static Log4j2Log getLog(String name) { return new Log4j2Log(name); } public void trace(String message) { log.log(Level.TRACE, message); } public void trace(String message, Throwable t) { log.log(Level.TRACE, message, t); } public void debug(String message) { log.log(Level.DEBUG, message); } public void debug(String message, Throwable t) { log.log(Level.DEBUG, message, t); } public void info(String message) { log.log(Level.INFO, message); } public void info(String message, Throwable t) { log.log(Level.INFO, message, t); } public void warn(String message) { log.log(Level.WARN, message); } public void warn(String message, Throwable t) { log.log(Level.WARN, message, t); } public void error(String message) { log.log(Level.ERROR, message); } public void error(String message, Throwable t) { log.log(Level.ERROR, message, t); } public void fatal(String message) { log.log(Level.FATAL, message); } public void fatal(String message, Throwable t) { log.log(Level.FATAL, message, t); } public boolean isTraceEnabled() { return log.isTraceEnabled(); } public boolean isDebugEnabled() { return log.isDebugEnabled(); } public boolean isInfoEnabled() { return log.isInfoEnabled(); } public boolean isWarnEnabled() { return log.isEnabled(Level.WARN); } public boolean isErrorEnabled() { return log.isEnabled(Level.ERROR); } public boolean isFatalEnabled() { return log.isEnabled(Level.FATAL); } }
添加log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <!--只接受程序中DEBUG级别的日志进行处理--> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="[%d{HH:mm:ss.SSS}] %-5level %class{36} %L %M - %msg%xEx%n" /> </Console> <!--处理DEBUG级别的日志,并把该日志放到logs/debug.log文件中--> <!--打印出DEBUG级别日志,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档--> <RollingFile name="RollingFileDebug" fileName="./logs/debug.log" filePattern="logs/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="DEBUG" /> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <SizeBasedTriggeringPolicy size="500 MB" /> <TimeBasedTriggeringPolicy /> </Policies> </RollingFile> <!--处理INFO级别的日志,并把该日志放到logs/info.log文件中--> <RollingFile name="RollingFileInfo" fileName="./logs/info.log" filePattern="logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <!--只接受INFO级别的日志,其余的全部拒绝处理--> <ThresholdFilter level="INFO" /> <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <SizeBasedTriggeringPolicy size="500 MB" /> <TimeBasedTriggeringPolicy /> </Policies> </RollingFile> <!--处理WARN级别的日志,并把该日志放到logs/warn.log文件中--> <RollingFile name="RollingFileWarn" fileName="./logs/warn.log" filePattern="logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="WARN" /> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL" /> </Filters> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <SizeBasedTriggeringPolicy size="500 MB" /> <TimeBasedTriggeringPolicy /> </Policies> </RollingFile> <!--处理error级别的日志,并把该日志放到logs/error.log文件中--> <RollingFile name="RollingFileError" fileName="./logs/error.log" filePattern="logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"> <ThresholdFilter level="ERROR" /> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n" /> <Policies> <SizeBasedTriggeringPolicy size="500 MB" /> <TimeBasedTriggeringPolicy /> </Policies> </RollingFile> <!--druid的日志记录追加器--> <RollingFile name="druidSqlRollingFile" fileName="./logs/druid-sql.log" filePattern="logs/$${date:yyyy-MM}/api-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %L %M - %msg%xEx%n" /> <Policies> <SizeBasedTriggeringPolicy size="500 MB" /> <TimeBasedTriggeringPolicy /> </Policies> </RollingFile> </appenders> <loggers> <root level="DEBUG"> <appender-ref ref="Console" /> <appender-ref ref="RollingFileInfo" /> <appender-ref ref="RollingFileWarn" /> <appender-ref ref="RollingFileError" /> <appender-ref ref="RollingFileDebug" /> </root> <!--记录druid-sql的记录--> <logger name="druid.sql.Statement" level="debug" additivity="false"> <appender-ref ref="druidSqlRollingFile" /> </logger> <logger name="druid.sql.Statement" level="debug" additivity="false"> <appender-ref ref="druidSqlRollingFile" /> </logger> <!--log4j2 自带过滤日志--> <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" /> <Logger name="org.apache.catalina.util.LifecycleBase" level="error" /> <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" /> <logger name="org.apache.sshd.common.util.SecurityUtils" level="warn" /> <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" /> <Logger name="org.crsh.plugin" level="warn" /> <logger name="org.crsh.ssh" level="warn" /> <Logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="error" /> <Logger name="org.hibernate.validator.internal.util.Version" level="warn" /> <logger name="org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration" level="warn" /> <logger name="org.springframework.boot.actuate.endpoint.jmx" level="warn" /> <logger name="org.thymeleaf" level="warn" /> </loggers> </configuration>
添下来就能愉快地使用了
private static final Log log = Log.getLog(XXX.class); log.error("", e); log.info("");