Getting started with log4j2

Installation or Setup

This would be a basic setup in wich we will send all the log messages to the console and to a log file.

Let’s start with the libraries. Will use maven for that:


Next we’ll write the (I’ll use a properties file in this case). We’ll put it in the context path, in maven it should be in the resources folder (src/main/resources in most cases)

property.filename = logs
appenders = console, file

appender.console.type = Console = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File = LOGFILE
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

To use this example you’ll need to change so it targets the package of your choice. If you don’t do that the file won’t show any logs.