How to use Log4j in Java code

suggest change

First need to create a final static logger object:

final static Logger logger = Logger.getLogger(classname.class);

Then, call logging methods:

//logs an error message
logger.info("Information about some param: " + parameter); // Note that this line could throw a NullPointerException!

//in order to improve performance, it is advised to use the `isXXXEnabled()` Methods
if( logger.isInfoEnabled() ){
    logger.info("Information about some param: " + parameter);
}

// In log4j2 parameter substitution is preferable due to readability and performance
// The parameter substitution only takes place if info level is active which obsoletes the use of isXXXEnabled().
logger.info("Information about some param: {}" , parameter);

//logs an exception
logger.error("Information about some error: ", exception);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


log4j, log4j2:
* How to use Log4j in Java code

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
67 log4j, log4j2
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap