0

I use spring boot 2.3.1 version, and I have an issue with application logs. My logback-spring.xml file as below,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="APP_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/var/log/app.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} %-5level %-12X{process} %-30([%thread]) %-29logger - %m%n</Pattern>
        </encoder>
    </appender>
    <logger name="com.sample" level="DEBUG" additivity="false"/>
    <logger name="org.springframework" level="INFO"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="APP_LOGGER"/>
    </root>
</configuration>

My controller is,

package com.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class Controller {

  private static final Logger LOGGER = LoggerFactory.getLogger(Controller.class);

  @GetMapping("/")
  public void greetings(){
    LOGGER.info("AAAAA....");
    LOGGER.debug("DDDD...");
    LOGGER.warn("WWW...");
    LOGGER.error("EEEE....")
  }

}

I tried adding the below in application.properties file,

logging.level.root=DEBUG
logging.level.org.springframework=INFO
logging.level.com.sample=DEBUG

no luck, and also tried with other alternative approach as below, and added file path logging.file.name=/var/log/app.log in application.properties.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    
    <logger name="org.springframework.web" level="INFO"/>
    <logger name="com.sample" level="DEBUG"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

app.log file is creating and I see only spring related logs, not application logs in the app.log file.

1 Answer 1

1

Your configuration works well. Remove the logging configuration from the application.properties. And you need to hit the baseURL(example: http://localhost:8080/) for watching the logs defined inside the controller. I modified the logback.xml a little bit for using a variable for the log directory. Here it is:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_PATH" value="/home/none47/logs"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="APP_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/app.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} %-5level %-12X{process} %-30([%thread]) %-29logger - %m%n</Pattern>
        </encoder>
    </appender>
    <logger name="com.sample" level="DEBUG" additivity="false"/>
    <logger name="org.springframework" level="INFO"/>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="APP_LOGGER"/>
    </root>
</configuration>
1
  • yeah that's strange but i made mixed approach, console appender from Spring Console appender and File appender from ch.qos. It got working now. Thanks for your input. Commented Jan 30, 2021 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.