JFinal使用log4j2

首先引入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("");


评论区

北流家园网

2021-12-14 14:02

不错,收藏了。想问下log4j跟log4j2是一样的吗?所说的漏洞跟log4j有关吗?当前项目使用的是log4j

tx

2021-12-14 14:29

@北流家园网 log4j没有发新版本,应该不受影响,最新版本 1.2.17,Central,8,589,May, 2012;
log4j2等于sl4j+logback,而且比sl4j的提示的api更多,可以打印对象啥的,因此有两个包api和core,分别借鉴sl4j和logback,用log4j有性能问题,可以才要logback和log4j2

happyboy

2021-12-14 14:41

2.16.0刚刚上仓库,你是真够麻利的

happyboy

2021-12-14 14:43

me.setLogFactory(new Log4j2LogFactory());不建议直接使用log4j2,建议使用slf4j,其实你pom里都引入了。

tx

2021-12-14 14:46

@happyboy OK,谢谢建议

steven_lhcb_9527

2021-12-16 08:47

End of Life On August 5, 2015 the Logging Services Project Management
Committee announced that Log4j 1.x had reached end of life. For
complete text of the announcement please see the Apache Blog. Users of
Log4j 1 are recommended to upgrade to Apache Log4j 2.
是的,log4j停止于1.x版本,迎来了log4j 2,也就是我们今天要说的log4j 2.

steven_lhcb_9527

2021-12-16 09:01

me.setLogFactory(new Log4j2LogFactory());这句执行后LogKit就直接可以用log4j2了

steven_lhcb_9527

2021-12-22 13:46

log4j2-2.16.0据说还爆出漏洞,现在最新是2.17.0了

热门分享

扫码入社