Log4j Configuration

From NovaOrdis Knowledge Base
Revision as of 15:18, 28 October 2017 by Ovidiu (talk | contribs) (→‎Internal)
Jump to navigation Jump to search

Internal

Turning Logging Off Completely for a Category

<category name="io.novaordis.test">
    <priority value="OFF"/>
</category>

Turning Logging Off for an Inner Class

If we have an inner class with its own Logging instance:

package x.y.z;

public class A {
    ...
    public static class B  {
        private static final Logger log = Logger.getLogger(B.class);
        ...
    }
}

then we refer to that category from a log4j.xml file as follows:

<log4j:configuration ...>
    ...
    <category name="x.y.z.A$B">
       <priority value="DEBUG"/>
    </category>
    ...
</log4j:configuration>

Programmatic Configuration with Override of Configuration Files from Classpath

public class SomeClass {

    private static Logger log; // DO NOT initialize here
    .... 

    File log4jConfigFile = ...;

    if (log4jConfigFile != null)  {
        // we want to override the log4j configuration embedded with the jar
        URL url = log4jConfigFile.toURI().toURL();
        System.setProperty(LogManager.DEFAULT_CONFIGURATION_KEY, url.toExternalForm());
    }

    log = Logger.getLogger(SomeClass.class);

    ...
}