Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

java - how to add custom logging level in log4j

I want to add custom logging level security to my log4j.xml file and this log level should be above debug and less than level trace.How to configure the custom logging level in log4j.xml file and when i use the level debug in root element of log4j.xml file the log level security should not be printed in log file wwhereas if i use custom level security in root element of log4j.xml file all the levels up security should be printed but not trace.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Log4J 2 supports custom log levels. Custom log levels can be defined in code or in configuration.

To create custom log levels in configuration (log4j.xml), use the following example snippet,

<CustomLevels>
      <CustomLevel name="CUSTOM" intLevel="350" />
</CustomLevels>

The intLevel parameter (350 in the example above) determines where the custom level exists in relation to the standard levels built-in to Log4J 2.

In your case, since you need a log level above DEBUG and less than TRACE, use the following code snippet in log4j.xml

<CustomLevels>
      <CustomLevel name="CUSTOM" intLevel="550" />
</CustomLevels>

Since the intLevel of DEBUG is 500 and TRACE is 600 (according to Log4J 2), we have set the intLevel for the custom log level as 550.

For more information, refer the following link,

https://logging.apache.org/log4j/2.x/manual/customloglevels.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...