简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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