Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...