I have created a decorator class from slf4j logger
class to do a certain check Inside the information
, debugs
, etc. I am using log4j12 binding.
This log-in works fine except for FQCN: This is not the initial class FQCN, but the FQCN of Decorator Class.
package foo.bar; Import org.slf4j.Logger; Public class MyLogger Logger applicable {Private Final Logger Logger; Personal Finale Boolean Trace And Exits; Public malogger (logger logger, boolean trace and ext.) {This.logger = logger; This.traceAndExit = traceAndExit; } // MyLogger @ Override Public Zero Information (String Message) {try {logger.info (Message); } Catch (exception e) {if (traceAndExit) {traceAndExit (e); } // if} // catch} // info ...
If any other class foo.bar.OtherClass
I do this:
MyLogger myLogger = New MyLogger (LoggerFactory.getLogger (OtherClass.class), is true); MyLogger.info ("This is a message");
Then I receive the foo.bar.OtherClass
print instead of foo.bar.MyLogger
(% C
conversion character).
Any ideas about how to do it properly? It is mandatory for me to print the FQCN.
Unfortunately, you did not mention that logging implementation is you using . Slf4j is just a common API that is shared by various logging implementations.
Fortunately, at least the logback, log 4J and log4J2, which should be accounted for most logging implementations in the wild, similar to the % C
format specifier Explain.
and % C
define "full-qualified class name of calling issuing logging requests ." This is done in ch.qos.logback.classic.spi.CallerData and
org.apache.logging.log4j.util.ReflectionUtil
, estimated. As a matter of fact, the framework checks the stack trace, unless they are sure to leave their own codebase, then face the "next" square on the stack. In your case, this is the MyLogger
class.
The easiest way to avoid the situation is to leave the format specifier completely but while debugging is useful, stack introspection has a serious effect on the performance and such situations Can not handle it as if your cleaning
% c
The format specifier incorporates the name of the logger instead. If you initialize the logger correctly:
// static logger private static last logger STATIC_LOG = LoggerFactory.getLogger (OtherClass.class); // Instance logger private finals logger log = loggeractorgatelogger (getClass ());
% c
Generates expected output such as the advanced logging solution that allows for multiple logger per square for example audit logging.
If you want to use the % C
specification at all, the logback provides the option to set the framework package ignored by the collar scan:
// LoggerContext LC = (LoggerContext) during the application startups LoggerFactory.getILoggerFactory (); Lc.getFrameworkPackages () Add ("foo.bar.logging")
Note that this will completely disable % C
output for the entire foo. Bar.logging package. Log4j 2 does not appear to provide the same configuration options.
No comments:
Post a Comment