Reduce logging of the same exceptioins

2019-05-10 09:41发布

Are there any smart ways to reduce logging of "equals" exceptions?

For example:

java.lang.IllegalArgumentException: Wrong parameter, should be a float from 0 to 100
at com.test.Foo.setAmount(Foo.java:93)
at com.test.Bar.setAmounts(Bar.java:39)
at com.test.Bar2.init(Bar2.java:152)
at java.awt.event.InvocationEvent.dispatch(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue.access$200(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue$3.run(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue$3.run(Unknown Source) [na:1.7.0_65]
at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_65]
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue.dispatchEvent(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.run(Unknown Source) [na:1.7.0_65]

such exception can be thrown 90 times per second under specific circumstances. We use AWT handler to log exceptions, and it can "freeze" all Swing's application.

The first workaround which came to mind was to log exceptions in a different thread, but such approch may be a real hell in later debug.

The second thought was to put exceptions in a WeakHashMap and log exception only for first occurance. Im not sure how to perform equals for exception and whether it will be fast enough at all.

2条回答
Explosion°爆炸
2楼-- · 2019-05-10 10:04

What if you write an ExceptionWrapper class and implement equals() method which compare message and stack trace. Then logger keeps LinkedHashSet of the wrappers where newly happened exceptions are added. The logger logs the Set e.g. each second (or any different time interval). Thus newly added "equal" exceptions replace older.

查看更多
Bombasti
3楼-- · 2019-05-10 10:09

If you want to use Logback, there is a Filter called DuplicateMessageFilter that drops messages after a certain repetition.

查看更多
登录 后发表回答