Tutorial by Examples

Current version (log4j2) Using Maven: Add the following dependency to your POM.xml file: <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.6.2</version> </de...
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!...
Log4j gives you posibility to log data into console and file at same time. Create a log4j.properties file and put inside this basic configuration: # Root logger option log4j.rootLogger=DEBUG, stdout, file # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppende...
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5p [%t] %C{2} %m%n"/> </Console> </Appenders&gt...
If you want to migrate from existing log4j 1.x in your project to log4j 2.x then remove all existing log4j 1.x dependencies and add the following dependency: Log4j 1.x API Bridge Maven Build <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> ...
For this example to work you'll need a JDBC driver compatible to the system the database is running on. An opensource one that allows you to connect to DB2 databases on an IBM System i can be found here: JT400 Even though this example is DB2 specific, it works for almost every other system if you e...
You can use a filter to log only messages "lower" than e.g. ERROR level. But the filter is not supported by PropertyConfigurator. So you must change to XML config to use it. See log4j-Wiki about filters. Example "specific level" <appender name="info-out" class=&quo...

Page 1 of 1