Third-party logs
The code is as follows. This is the simplest configuration that only prints logs to the console. This code will be used as an example to explain how to block third-party logs.
<?xml version="1.0" encoding="UTF-8" ?> <Configuration status="WARN" monitorInterval="600"> <Appenders> <Console name="console_out_appender" target="SYSTEM_OUT"> <!-- Format of output log --> <PatternLayout pattern="%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%n"/> </Console> </Appenders> <Loggers> <!-- Configure the root node of the log,PrinttraceLogs of grades and above --> <root level="trace"> <appender-ref ref="console_out_appender"/> </root> </Loggers> </Configuration>
When using this configuration file, you will find that there are a large number of third-party logs filled in it, making it very difficult to find your own logs. The effect is as follows
This is the heartbeat mechanism log of zookeeper in third-party logs, which outputs one line in almost a second, which is particularly annoying
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:30 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 48ms
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:31 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 9ms
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:32 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 4ms
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:34 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 7ms
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:35 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 6ms
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:36 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 24ms
DEBUG [RMI TCP Connection(192.168.9.5:2181)] 2021-07-21 10:46:38 (:717) Got ping response for sessionid: 0x17a18cc4d2b4155 after 11ms
Specially block a third-party log
To block this log, one is to directly increase the log level of the root node of the log to info, so that these logs will not be visible, but this will also cover up the logs of the level that the main info has already been placed.
But it can be added<logger>
The tag specifically blocks third-party logs, the code is shown below.
<?xml version="1.0" encoding="UTF-8" ?> <Configuration status="WARN" monitorInterval="600"> <Appenders> <Console name="console_out_appender" target="SYSTEM_OUT"> <!-- Format of output log --> <PatternLayout pattern="%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%n"/> </Console> </Appenders> <Loggers> <!-- Configure the root node of the log --> <root level="trace"> <appender-ref ref="console_out_appender"/> </root> <!-- Third-party logging system --> <logger name="" level="info"/> </Loggers> </Configuration>
In this way, the info level can be used to specifically limit the zookeeper's heartbeat mechanism log, and it will not affect your own log level.
However, there are many things that randomly output logs, including the spring framework, and then you can block the log by adding a logger
Bulk block third-party logs
In order to block third-party logs, a lot of loggers have to be added, and the blocking is not completely blocked. The main reason is that I don’t know the name attribute of the logger to be blocked, so I can only search online. The result is as follows, but I still blocked incompletely.
<!-- Third-party logging system --> <logger name="" level="info"/> <logger name="" level="info"/> <logger name="" level="info"/> <logger name="" level="info"/> <logger name="" level="info"/> <logger name="" level="info"/> <logger name="" level="info"/> <logger name="" level="info"/>
How to set up third-party logs in batches? I tried using wildcard characters'*'
, but it doesn't work. After searching online for a long time, I found that there is only one sentence for the name attribute under the logger node: name: used to specify the full path of the package where the class or class to which the Logger is applicable is located, inherited from the Root node.
Maybe I can't understand reading.
Finally checked/questions/44551571/log4j2-wildcard-logger-namesand/log4j//manual/#Logger_Names, then I understand that as long as the parent log is blocked, the child first-level log will be automatically blocked.
So if you can't block it all so many things written above, just change it to the following sentence.
<!-- Third-party logging system --> <logger name="org" level="info"/>
Oh my God, I searched many people's answers online, all of which were silly configurations one by one, which made me so desperate.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.