SpringBoot with log4j2 - ERROR StatusLogger No log4j2 configuration file found.

日志的重要性,不言而喻,Spring Boot支持大部分的log配置,其中包括:
(1)java util logging
(2)log4j
(3)log4j2
(4)logbak
默认的情况下spring boot会选择logback作为日志记录的载体。

我们都知道log配置文件都需要安装固定文件名命名才能被log系统自动加载,比如log4j能够自动加载log4j.properties,log4j2能自动加载log4j2.xml,log4j2-test.xml等命名规则的配置文件。

问题描述

关于log4j2,在SpringBoot中,官方推荐使用log4j2-spring.xml命名,系统启动时打印了一句error,虽然不影响运行,但是看着怪怪的,即使在springboot配置文件application.properties中配置了log4j2配置文件 logging.config=classpath:log4j2-spring.xml,但是任然会报如下错误:

1
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

原因

原因是log4j2不能自动加载log4j2-spring.xml

解决

  1. 直接忽略官方推荐,以log4j.xml命名
  2. 通过log4j2的插件机制(@Plugin)加载log4j2-spring.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.logging.log4j2;
import java.net.URI;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.config.Order;
import org.apache.logging.log4j.core.config.plugins.Plugin;
/**
* Spring Boot {@link ConfigurationFactory} that prevents logger warnings from being
* printed when the application first starts.
*
* @author Phillip Webb
* @since 1.5.0
*/
@Plugin(name = "SpringBootConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(4) // Order behind XmlConfigurationFactory
public class SpringBootConfigurationFactory extends ConfigurationFactory {
private static final String[] ALL_TYPES = { "*" };
@Override
protected String[] getSupportedTypes() {
return ALL_TYPES;
}
@Override
public Configuration getConfiguration(LoggerContext loggerContext, String name,
URI configLocation) {
if (configLocation == null) {
return new DefaultConfiguration();
}
return null;
}
@Override
public Configuration getConfiguration(LoggerContext loggerContext, String name,
URI configLocation, ClassLoader loader) {
return null;
}
@Override
public Configuration getConfiguration(LoggerContext loggerContext,
ConfigurationSource source) {
return null;
}
}

上述插件取自:https://github.com/spring-projects/spring-boot/commit/1e9e1b04d00ec319e0473f8126b39e6b72ef1de8#diff-c5a5f1786a9591c088e86c4e388939caR34

可以看到@since 1.5.0 ,所以如果将SpringBoot升级到1.5.0,就可以解决这个问题,或者将这个plugin放到自己的系统中。

我知道是不会有人点的,但万一有人想不开呢!