Pankaj Kanti
Pankaj Kanti

Reputation: 93

Logback not creating Log File in SpringBoot

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>true</withJansi>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger - %msg%n</Pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/logs/tomcat/LogBackDemo/logback/app_LogBackDemo.log</file>

        <withJansi>true</withJansi>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>/logs/tomcat/LogBackDemo/logback/app_LogBackDemo.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>10</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>

        </rollingPolicy>

    </appender>

    <logger name="com.logBack.demo" level="debug"
            additivity="false">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="error" additivity="false">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

This is my logback-spring.xml file but when I am running applications logs are displaying properly in Console but log file is not creating at specified location. I am using windows system so Ideally log file should create in c:/logs/tomcat/LogBackDemo/logback/app_LogBackDemo.log location.

Upvotes: 1

Views: 5020

Answers (2)

Nu&#241;ito Calzada
Nu&#241;ito Calzada

Reputation: 1948

I had the same problem. I just delete logback.xml from the fileSystem and create it again and it works

Upvotes: 0

Anand Varkey Philips
Anand Varkey Philips

Reputation: 2073

I used your exact file (removed <withJansi>true</withJansi> ) and it was creating log file when the log configuration file kept inside resources folder(src\main\resources\logback-spring.xml). I had modified com.logBack.demo (logger name) as per my package name and then it printed log.

You can use below properties and easily setup logging including log-file in spring boot with application.property file.

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

If you want to use special xml files (which i dont prefer), you can check this, https://www.baeldung.com/spring-boot-logging

Please let me know if you are still facing troubles

Upvotes: 2

Related Questions