Log4j Configuration: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
=External=
* http://logging.apache.org/log4j/2.x/manual/configuration.html
=Internal=
=Internal=


Line 6: Line 10:
=Turning Logging Off Completely for a Category=
=Turning Logging Off Completely for a Category=


<pre>
<syntaxhighlight lang='xml'>
<category name="io.novaordis.test">
<category name="io.novaordis.test">
     <priority value="OFF"/>
     <priority value="OFF"/>
</category>
</category>
</pre>
</syntaxhighlight>


=Turning Logging Off for an Inner Class=
=Turning Logging Off for an Inner Class=
Line 16: Line 20:
If we have an inner class with its own Logging instance:
If we have an inner class with its own Logging instance:


<pre>
<syntaxhighlight lang='xml'>
package x.y.z;
package x.y.z;


Line 26: Line 30:
     }
     }
}
}
</pre>
</syntaxhighlight>


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


<pre>
<syntaxhighlight lang='xml'>
<log4j:configuration ...>
<log4j:configuration ...>
     ...
     ...
Line 38: Line 42:
     ...
     ...
</log4j:configuration>
</log4j:configuration>
</pre>
</syntaxhighlight>


=Programmatic Configuration with Override of Configuration Files from Classpath=
=Programmatic Configuration with Override of Configuration Files from Classpath=


<pre>
<syntaxhighlight lang='java'>
public class SomeClass {
public class SomeClass {


Line 60: Line 64:
     ...
     ...
}
}
</pre>
</syntaxhighlight>
 
=Configuration with Properties=
 
{{External|http://logging.apache.org/log4j/2.x/manual/configuration.html#Properties}}

Latest revision as of 21:32, 20 December 2018

External

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);

    ...
}

Configuration with Properties

http://logging.apache.org/log4j/2.x/manual/configuration.html#Properties