To enable features such as
logger.info("This is a formatted number: %.2f", number)
I decided to write a subclass of org.apache.log4j.Logger . I know I could write a wrapper class to achieve the same result, but since I add a lot of additions to the log at runtime, I prefer to use inheritance.
The subclass is as follows:
public final class FormatLogger extends Logger { private final static FormatLoggerFactory factory = new FormatLoggerFactory(); protected FormatLogger(String name) { super(name); } public static Logger getLogger(String name) { return Logger.getLogger(name, factory); } public void fatal(String formatter, Object... args) { log(Level.FATAL, formatter, args); } public void log(Level level, String formatter, Object... args) { if (super.isEnabledFor(level)) { super.log(level, String.format(formatter, args)); } } }
Everything works well - all but one: the message text now adds the name of the journal subclass, rather than the name of the class that calls the registrar. As a template for the template, I use the following format:
[%d{yyyyMMdd HHmmss}] %-5p [%t] %C: %m%n
i.e. it looks like this:
[20110525 214515] INFO [main] org.xyz.FormatLogger: This is a formatted number: 23.23
instead:
[20110525 214515] INFO [main] org.xyz.Main: This is a formatted number: 23.23
Is there a way to do this βcorrectlyβ so that β% Cβ continues to print the original class name?
source share