简体   繁体   English

在log4j2中打印intLevel

[英]Print intLevel in log4j2

You can see in https://logging.apache.org/log4j/2.x/manual/customloglevels.html the numerical values corresponding to built-in log4j2 logging levels, for example INFO->400. 您可以在https://logging.apache.org/log4j/2.x/manual/customloglevels.html中看到与内置log4j2日志记录级别相对应的数值,例如INFO-> 400。 How can you refer to it in patternlayout resp. 如何在patternlayout中分别引用它。 in JDBC Logger configuration? 在JDBC Logger配置中?

I have an old log4j 1.x config for JDBC where it was referred as %iprio. 我有一个用于JDBC的旧log4j 1.x配置,其中它称为%iprio。

A workaround is to use 解决方法是使用

level{OFF=0,FATAL=100,ERROR=200,WARN=300,INFO=400,DEBUG=500,TRACE=600,ALL=1000} 电平{OFF = 0,致命= 100,ERROR = 200,WARN = 300,INFO = 400,DEBUG = 500,TRACE = 600,ALL = 1000}

but I am not very happy about that. 但是我对此不是很高兴。

It sounds like you want to log the integer value of the log level instead of the name and you don't want to use the labeling feature of the PatternLayout 's level parameter. 听起来您想记录日志级别的整数值而不是名称,并且您不想使用PatternLayoutlevel参数的标签功能。

One possible solution would be to create a custom Lookup , here is some sample code: 一种可能的解决方案是创建自定义Lookup ,这是一些示例代码:

package example;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;

@Plugin(name = "level", category = "Lookup")
public class LevelLookup implements StrLookup{
    /**
     * Lookup the value for the key.
     * @param key  the key to be looked up, may be null
     * @return The value for the key.
     */
    public String lookup(String key) {
        return null;
    }


    /**
     * Lookup the value for the key using the data in the LogEvent.
     * @param event The current LogEvent.
     * @param key  the key to be looked up, may be null
     * @return The value associated with the key.
     */
    public String lookup(LogEvent event, String key) {
        return String.valueOf(event.getLevel().intLevel());
    }
}

Next, here is a sample configuration using the new lookup - note the ${level:} : 接下来,这是使用新查找的示例配置-注意${level:}

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] ${level:} %logger{36} - %msg%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />           
        </Root>
    </Loggers>
</Configuration>

Here is some sample code that performs some logging: 这是一些执行日志记录的示例代码:

package example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SomeClass {

    private static final Logger log = LogManager.getLogger();   

    public static void main(String[] args){
        log.debug("This is some debug!");
        log.info("Here's some info!");
        log.error("Some erorr happened!");
    }
}

and finally here is the output: 最后是输出:

22:49:29.438 [main] 500 example.SomeClass - This is some debug!
22:49:29.440 [main] 400 example.SomeClass - Here's some info!
22:49:29.440 [main] 200 example.SomeClass - Some erorr happened!

Hope this helps! 希望这可以帮助!

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

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