0
votes

I am a newbie to Spring Boot. I have a running Spring Boot project. I want to use log4j2 (I am bound to use log4j2 itself because of project restrictions) to redirect all logs of different levels to a log file named 'test.log'

Problem is - despite of all proper code included, I am not able to log INFO level logs in my test.log (I want to obviously log error and debug level logs also, but at least first INFO level logs should work fine)

--- I have excluded default logging and included log4j2 dependency in pom.xml :

<!-- Exclude Spring Boot's Default Logging -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Add Log4j2 Dependency -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency> 

--- I have included logging.config in application.properties:

logging.file=logs/test.log
logging.level.*=INFO
logging.config=src/main/resources/log4j2.properties

--- My log4j2.properties looks as follows:

#status = error // do i need this actually??
dest = logs/test.log
name = PropertiesConfig

property.filename = logs/test.log

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = logs/test.log
appender.rolling.filePattern = logs/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %-5p %-17c{2} (%30F:%L) %3x - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = com.thp.clinic.allergiesConditions
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
#rootCategory=INFO,rolling,stdout
#logging.level.root=info

#rootLogger.level = debug  //do i necessarily need root Logger????
#rootLogger.appenderRefs = RollingFile
#rootLogger.appenderRef.stdout.ref = STDOUT

logger.rolling.name=org.hibernate.SQL
logger.rolling.level = debug

--- Also controller of my test API has following test log lines:

    //Logger logger = LogManager.getLogger //is included       
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");

--- In my understanding, I have included all required things in code. But I am facing this issues - when I hit API, only hibernate debug loggers are being added to test.log; The five test loggers(or even other info level logs used) i have included in controller are not getting logged in test.log;

The console looks like following(two of five loggers are being displayed on console, but here also INFO level logger is missing):

20:05:42.989 [http-nio-8000-exec-1] ERROR com.test.app.appController - This is an error message
20:05:42.994 [http-nio-8000-exec-1] FATAL com.test.app.appController - This is a fatal message
Hibernate: //used hibernate queries are displayed to console as needed

It would be great help if someone can point out what i need to change in the code. Because of improper understanding of log4j2, i guess something needs to change in log4j2.properties

Please help!! Thanks in advance

1

1 Answers

0
votes

log4j2 switched from having a .properties file to yaml or xml. you will need to create a yaml or xml file to have log4j2 to load.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5p] %m%n" />
        </Console>
        <RollingFile name="myapp" fileName="${sys:catalina.base}/logs/myapp.out"
            filePattern="${sys:catalina.base}/logs/myapp-%d{yyyy-MM-dd}.log.gz">
            <PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5p] %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="ConsoleAppender" level="INFO"/>
            <AppenderRef ref="myapp" level="INFO"/>
        </Root>
    </Loggers>
</Configuration>