屏蔽 Log4j 2 升级引发打印配置外的日志信息

最近将一个旧系统的日志框架从 Log4j 升级至 Log4j 2,发现部分在 Log4j 2 中未配置的日志信息(DEBUG / INFO)打印在控制台上,本文介绍屏蔽这些额外日志打印的方法。


Spring 框架打印日志

...
2019-02-25 10:10:02,605 DEBUG [org.springframework.core.env.StandardEnvironment] - Adding [systemProperties] PropertySource with lowest search precedence
2019-02-25 10:10:02,607 DEBUG [org.springframework.core.env.StandardEnvironment] - Adding [systemEnvironment] PropertySource with lowest search precedence
2019-02-25 10:10:02,608 DEBUG [org.springframework.core.env.StandardEnvironment] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2019-02-25 10:10:02,609 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6b927fb: startup date [Mon Feb 25 10:10:02 GMT+08:00 2019]; root of context hierarchy
2019-02-25 10:10:02,663 DEBUG [org.springframework.core.env.StandardEnvironment] - Adding [systemProperties] PropertySource with lowest search precedence
2019-02-25 10:10:02,663 DEBUG [org.springframework.core.env.StandardEnvironment] - Adding [systemEnvironment] PropertySource with lowest search precedence
2019-02-25 10:10:02,663 DEBUG [org.springframework.core.env.StandardEnvironment] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2019-02-25 10:10:02,717 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [xxx.xml]
2019-02-25 10:10:02,782 DEBUG [org.springframework.beans.factory.xml.DefaultDocumentLoader] - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
...

解决思路:

  1. 首先选取一条日志信息,定位源码所在包,如 org.springframework.beans.factory.xml.XmlBeanDefinitionReaderspring-beans 中。
  2. 查看 XmlBeanDefinitionReader 源码,定位日志 Loading XML bean definitions from class path resource 所在位置。
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
        Assert.notNull(encodedResource, "EncodedResource must not be null");
        if (this.logger.isInfoEnabled()) {
            this.logger.info("Loading XML bean definitions from " + encodedResource.getResource());
        }
......
  1. 定位 logger 对象所属 API
package org.springframework.beans.factory.support;

import java.io.IOException;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Assert;

public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable, BeanDefinitionReader {
    protected final Log logger = LogFactory.getLog(this.getClass());
......

发现这段日志是通过 Apache Commons Logging(原名:Jakarta Commons LoggingJCL)打印出来的。JCL 只提供 log 接口,具体实现在运行时动态寻找。但是当程序规模越来越大后,JCL 的动态绑定不是总能成功,所有 SLF4J 诞生了,与 JCL 的不同之处在于 SLF4J 在程序部署时静态绑定指定的日志工具。

因为此次升级也同时使用了 SLF4J,因此需要一个桥接器组件将 JCL 的日志输出重定向到 SLF4J。如果使用 Maven 则添加以下依赖,或者直接下载 jcl-over-slf4j.jar 放入工程 CLASSPATH


    org.slf4j
    jcl-over-slf4j
    1.7.25


Hibernate 框架打印日志

...
2019-02-25 16:27:54,602 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration boolean -> org.hibernate.type.BooleanType@4832f03b
2019-02-25 16:27:54,602 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration boolean -> org.hibernate.type.BooleanType@4832f03b
2019-02-25 16:27:54,603 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration java.lang.Boolean -> org.hibernate.type.BooleanType@4832f03b
2019-02-25 16:27:54,604 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration numeric_boolean -> org.hibernate.type.NumericBooleanType@5115f590
2019-02-25 16:27:54,606 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration true_false -> org.hibernate.type.TrueFalseType@2ff7a73
2019-02-25 16:27:54,607 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration yes_no -> org.hibernate.type.YesNoType@3a790e40
2019-02-25 16:27:54,617 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration byte -> org.hibernate.type.ByteType@60c73e58
2019-02-25 16:27:54,617 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration byte -> org.hibernate.type.ByteType@60c73e58
2019-02-25 16:27:54,617 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration java.lang.Byte -> org.hibernate.type.ByteType@60c73e58
2019-02-25 16:27:54,620 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration character -> org.hibernate.type.CharacterType@4483d35
2019-02-25 16:27:54,621 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration char -> org.hibernate.type.CharacterType@4483d35
2019-02-25 16:27:54,621 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration java.lang.Character -> org.hibernate.type.CharacterType@4483d35
2019-02-25 16:27:54,623 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration short -> org.hibernate.type.ShortType@3762c4fc
2019-02-25 16:27:54,623 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration short -> org.hibernate.type.ShortType@3762c4fc
2019-02-25 16:27:54,624 DEBUG [org.hibernate.type.BasicTypeRegistry] - Adding type registration java.lang.Short -> org.hibernate.type.ShortType@3762c4fc
...

解决思路和 Spring 框架日志定位思路类似:

  1. 首先选取一条日志信息,定位源码所在包,如 org.hibernate.type.BasicTypeRegistryhibernate-core 中。
  2. 查看 BasicTypeRegistry 源码,定位日志 Adding type registration 所在位置。
public void register(BasicType type) {
    if (this.locked) {
        throw new HibernateException("Can not alter TypeRegistry at this time");
    } else if (type == null) {
        throw new HibernateException("Type to register cannot be null");
    } else {
        if (type.getRegistrationKeys() == null || type.getRegistrationKeys().length == 0) {
            LOG.typeDefinedNoRegistrationKeys(type);
        }

        String[] arr$ = type.getRegistrationKeys();
        int len$ = arr$.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String key = arr$[i$];
            if (key != null) {
                LOG.debugf("Adding type registration %s -> %s", key, type);
                Type old = (Type)this.registry.put(key, type);
                if (old != null && old != type) {
                    LOG.typeRegistrationOverridesPrevious(key, old);
                }
            }
        }

    }
}
  1. 定位 LOG 对象所属 API。
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.HibernateException;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;
import org.jboss.logging.Logger;

public class BasicTypeRegistry implements Serializable {
    private static final CoreMessageLogger LOG = (CoreMessageLogger)Logger.getMessageLogger(CoreMessageLogger.class, BasicTypeRegistry.class.getName());

发现这段日志是通过 JBoss Logging 打印出来的。
Hibernate 日志加载顺序:
JBoss 日志实现 -> Log4j 2 -> Log4j -> SLF4J -> java.util.logging
为了让 Hibernate 使用 Log4j 2 输出日志,有两种解决方案:

  • jboss-logging 依赖升级到 3.2 以上版本,如:

    org.jboss.logging
    jboss-logging
    3.3.2.Final

  • 配置系统属性:jboss.logging.provider=slf4j

你可能感兴趣的:(屏蔽 Log4j 2 升级引发打印配置外的日志信息)