log4j2 Setting printObject to true in custom Appender

妖精的绣舞 提交于 2019-12-20 07:37:11

问题


HERE It says that -

"Appenders should specify printObject as "true" if the toString method renders the values of the attributes passed to the Appender."

Can someone please explain this? Does this mean that if printObject=true, we should override toString() method? And what if we do not override toString() ?


回答1:


During Log4j 2 initialization, all plugins in the configuration are processed and stored in Node objects. The Node.toString() method is implemented such that if the original Plugin's printObject attribute was true, Node.toString() will delegate to the Plugin's toString() method. Otherwise, it returns a String containing the plugin class and the plugin name.

So in general it is wise to provide a good toString() method and declare printObject to be true. On the other hand, it is not a disaster if you miss either: this is mostly for debugging. So, if printObject is true but you don't override toString(), all that happens is that when debugging your plugin you won't see useful values.

Log4j 2 logs details of the initialization process to the StatusLogger at DEBUG/TRACE level. You can see this information if your configuration starts with <Configuration status="trace">.

It is possible that custom plugins can use the printObject attribute to influence what is logged to the StatusLogger logging during configuration. What is certain is that after initialization, it is useful to have your Plugin's toString() value shown when debugging the resulting Node values.



来源:https://stackoverflow.com/questions/41746157/log4j2-setting-printobject-to-true-in-custom-appender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!