Rajkishan Swami
Rajkishan Swami

Reputation: 3769

SpringBoot issue while migrating from log4j to lof4j2

BEFORE(Works Fine)

log4j.properties

log4j.rootLogger=DEBUG, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/spring-boot-mqtt.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [%t] [%c{1}] - %m%n

and maven dependency,

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>

AFTER(Doesn't Work)

log4j2.properties

name = PropertiesConfig
property.filename = logs
appenders = file

appender.file.type = File
appender.file.name = ROLLING
appender.file.append = true
appender.file.fileName=${filename}/spring-boot-mqtt.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] [%t][%c{2}] - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = file
rootLogger.appenderRef.file.ref = ROLLING

and my maven dependency,

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.5</version>
    </dependency>

When i use the same configuration for log4j2 in a standalone java application, it works fine. But in spring-boot application, a file is generated but it stays empty. I see same defualt log in console. Also layout.pattern in properties doesn't seem to affect anything.

NOTE : Using Slf4j

Upvotes: 0

Views: 752

Answers (1)

user987339
user987339

Reputation: 10707

Regarding writing to files you should check documentation:

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

Upvotes: 1

Related Questions