Note that there are some explanatory texts on larger screens.

plurals
  1. POSubclassing org.apache.log4j.Logger without altering %C?
    primarykey
    data
    text
    <p>To enable things like </p> <pre><code>logger.info("This is a formatted number: %.2f", number) </code></pre> <p>I decided to write a subclass of <code>org.apache.log4j.Logger</code>. I know, I could have written a wrapper class to achieve the same result but since I'm adding a lot of appenders to the logger at runtime I prefer to use inheritance.</p> <p>The subclass looks like this:</p> <pre><code>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)); } } } </code></pre> <p>Everything works nicely - everything but one thing: the message text now adds the name of the logger subclass instead of the name of the class calling the logger. As pattern layout, I use the following format: </p> <pre><code>[%d{yyyyMMdd HHmmss}] %-5p [%t] %C: %m%n </code></pre> <p>i.e. things look like this:</p> <pre><code>[20110525 214515] INFO [main] org.xyz.FormatLogger: This is a formatted number: 23.23 </code></pre> <p>instead of:</p> <pre><code>[20110525 214515] INFO [main] org.xyz.Main: This is a formatted number: 23.23 </code></pre> <p>Is there some way to do such thing "properly" so that "%C" keeps printing the original class name?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload