log4j2.properties example

log4j 2 logo

A simple log4j2.properties example, just for self-reference

P.S Tested with Log4j 2.11.2

src/resources/log4j2.properties

status = warn

appender.console.type = Console
appender.console.name = LogToConsole
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
#appender.file.name = LogToFile
#appender.file.fileName=logs/app.log
#appender.file.layout.type=PatternLayout
#appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

# Rotate log file
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = logs/app.log
appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10

# Log to console and rolling file
logger.app.name = com.mkyong
logger.app.level = debug
logger.app.additivity = false
logger.app.appenderRef.rolling.ref = LogToRollingFile
logger.app.appenderRef.console.ref = LogToConsole

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole

Download Source Code

References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
bart
4 years ago

properties file doesn’t work, just creates a blank log

Roshan Mishra
3 years ago
Reply to  bart

Hey buddy! is it working properly in your project?

Nguyen van
3 years ago
Reply to  Roshan Mishra

It’s work, It’s actually working.
I’m using Log4j latest version

A_A
4 years ago
Reply to  bart

I’m Conor and I’ll check it.

Last edited 4 years ago by A_A
FrankF
2 years ago

If you’re using Maven I believe log4j2.properties will go into src/main/resources folder…

charuseelan
3 years ago

in console its printing but the file is not generated

Diego
3 years ago

hello, Could yo hhelp me please, i set mi version into properties section of my pom

    <log4j2.version>2.19.0</log4j2.version>     

And config the log4j2.properties, when i lounch the api it generate the app.log but the file is empty. 0Kb, ¿what could be wrong? thanks in advance

san
3 years ago
Reply to  Diego

did you resolve it ? I’m facing the same issue

karthi
3 years ago

How do we define “app” in logger.app.name