java LOG4J2:以编程方式配置时禁用“No log4j2 configuration file found..”打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27994264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LOG4J2: Disable "No log4j2 configuration file found.." print when configuring programmatically
提问by Excite
I am not using any XML configuration file, rather I am setting the logger configuration programmatically. The logger works correctly but just when I call the first line of the code below, a warning ERROR
message shows up to tell me that the configuration file was not found and default configuration will be used.
我没有使用任何 XML 配置文件,而是以编程方式设置记录器配置。记录器工作正常,但是当我调用下面代码的第一行时,会ERROR
显示一条警告消息,告诉我未找到配置文件,将使用默认配置。
But I don't want this message to be displayed on the console every time I rung the program because I will programmaticallyadd the configuration myself.
但是我不希望每次运行程序时都在控制台上显示此消息,因为我会自己以编程方式添加配置。
The message:
消息:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
It comes when I call the code below:
当我调用下面的代码时会出现:
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Or if I get the root logger first:
或者,如果我先获得根记录器:
Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
Is there some kind of property that I can set to disable this message from being displayed? Or another approach?.
是否有某种属性可以设置为禁止显示此消息?还是另一种方法?
NOTE: I disabled another notification regarding Log4j2 JMX by setting the property -Dlog4j2.disable.jmx=true
. But I couldn't find for this one.
注意:我通过设置属性禁用了另一个关于 Log4j2 JMX 的通知-Dlog4j2.disable.jmx=true
。但是我找不到这个。
采纳答案by Paul Vargas
Add the following system property in order to not show that message:
添加以下系统属性以不显示该消息:
-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=OFF
Alternatively, and more simply:
或者,更简单的是:
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.status.StatusLogger;
...
StatusLogger.getLogger().setLevel(Level.OFF);
回答by Kin Cheung
You can simply do this in your main class.
您可以在主类中简单地执行此操作。
static {
StatusLogger.getLogger().setLevel(Level.OFF);
}
回答by Jagath Ariyarathne
You have to implement initialize method for proper initialization of log4j configurations from code.
您必须实现 initialize 方法才能从代码正确初始化 log4j 配置。
Check the "How do I configure log4j2 in code without a configuration file" section in below page which provides the source code also.
检查下面页面中的“如何在没有配置文件的情况下在代码中配置 log4j2”部分,该部分还提供了源代码。