简体   繁体   English

在配置文件中定义自定义日志级别,并通过Java代码Log4J2访问它们

[英]Define Custom Log Level in Configuration file and Access them is Java Code, Log4J2

I would like to define custom log levels in configurations file like below 我想在配置文件中定义自定义日志级别,如下所示

<CustomLevels>
    <CustomLevel name="DIAG" intLevel="350" />
    <CustomLevel name="NOTICE" intLevel="450" />
    <CustomLevel name="VERBOSE" intLevel="550" />
</CustomLevels>

And i want to access them in Java Code like 我想像这样用Java代码访问它们

Level diag = // get level defined in configuration
// and use them like this
logger.log(diag, "message");
// and then this 'message' is printed in the appender with thresholdfilter as diag

And then put a threshold filter on appender comparing with the level defined in configurations file... 然后将阈值过滤器与配置文件中定义的级别进行比较,在追加器上...

Is this possible? 这可能吗? And if yes, then how? 如果是,那又如何?

Define custom levels like this: 定义自定义级别,如下所示:

public static final Level DIAG = Level.forName("DIAG", 350)

So all you need is to read those levels in from the configurations file (on startup) and assigned them to some static variables. 因此,您所需要的只是从配置文件中读取这些级别(在启动时),并将它们分配给一些静态变量。

This should do the job: 这应该做的工作:

logger.log(Level.getLevel("DIAG"), "your message");

As stated in the log4j2 doc log4j2文档中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM