William Kinaan
William Kinaan

Reputation: 28799

log4j does not log for file

I am using log4j to log to both console and file. It just works with console, but not with file, thought the file is being created

This is my configuration file:

name=PropertiesConfig
property.filename = logs
appenders = console, file

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

loggers=file
logger.file.name=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

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

What am I missing please?

Upvotes: 2

Views: 1148

Answers (2)

VivekRatanSinha
VivekRatanSinha

Reputation: 616

The connection between file appender and root logger is missing. Add the following line at the bottom of your properties file:

rootLogger.appenderRef.file.ref = LOGFILE

It will work as expected.

Upvotes: 4

Walid Ammou
Walid Ammou

Reputation: 440

I think you expect that the variable ${filename} be substituted with the value of property.filename.

Can you try to substitute ${filename} with a hard path and try again (E.g: 'C:\' on Windows or '/tmp/' on Unix-like).

Upvotes: 0

Related Questions