I'm new to Spring Boot and I just have setup the logback to write logs to a custom log file. Everything works fine, I have successfully writing custom log messages into the file, however when an exception is raised the error and the stacktrace is not being written to the file.
src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<timestamp key="timestamp" datePattern="yyyy-MM-dd"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/app-${timestamp}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- retain 30 days logs -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
src/main/resources/application.properties
logging.config=classpath:logback.xml
The main class:
@SpringBootApplication
public class DemoApplication {
private static final Logger logger = Logger.getLogger("DEMO");
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
logger.log(Level.INFO, "main called");
throw new IllegalArgumentException("This is a test");
}
}
And the stacktrace which is not in the log file:
Exception in thread "main" java.lang.IllegalArgumentException: This is a test
00:06:54.179 [main] ERROR DEMO - This is an error
at com.demo.DemoApplication.main(DemoApplication.java:18)
P.S: I have tried to change the root level logging: <root level="ERROR">
but the issue still persist, I can't see the exception in the log file.
By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback. xml for Logback), but you can set the location of the config file by using the "logging. config" property.
Spring Boot supports Log4j 2 for logging configuration if it is on the classpath. If you are using the starters for assembling dependencies that means you have to exclude Logback and then include log4j 2 instead. If you aren't using the starters then you need to provide jcl-over-slf4j (at least) in addition to Log4j 2.
While there are a number of logging options for Java, the Spring Boot chose to use Logback for the default logger. Like many things in Spring Boot, Logback, by default, gets configured with sensible defaults. Out of the box, Spring Boot makes Logback easy to use.
The default logging configuration in Spring Boot is a Logback implementation at the info level for logging the output to console. The first info log is printed, followed by a seven-line banner of Spring and then the next info log. The debug statement is suppressed.
1.you must use slf4j + logback, it seem you are using java util logging 2. try catch exception and log the error, otherwise the logback can't catch the exception
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
private static final Logger logger = LoggerFactory.getLogger("DEMO");
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
logger.info("main called");
try {
throw new IllegalArgumentException("This is a test");
} catch(Exception e) {
logger.error("", e);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With