问题
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