Spring boot startup logs to file - spring-boot

Is it anyway to send startup logs to file, right now all logs up to the statement "Started Application in...." goes to stdout, I want all logging to file.
My logback config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="logs/app${PID}.log"/>
<appender name="AI-APPENDER"
class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="AI-APPENDER"/>
</root>
</configuration>

You need to configure your logging framework. Assuming you're just using the default from spring-boot then that's LogBack. So have a look at their docs or have a search, there are many useful resources (like this one)
If you add a logback.xml file with the following content to your resources folder you should get logging to both console and file (called application.log) in the same format as you now see for just console.
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>application.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
You probably want to use a RollingFileAppender as this will allow you to create new files when the log file gets big.

Related

How do I disable logging from javers?

Currently working in a large spring-boot application, it would be preferred to disable logging if possible to reduce noise.
sample of the logs
NFO org.javers.core.JaversBuilder mappingStyle: FIELD
NFO org.javers.core.JaversBuilder loading GroovyAddOns ...
NFO org.javers.core.JaversBuilder using fake InMemoryRepository, register actual Repository implementation via JaversBuilder.registerJaversRepository()
NFO org.javers.core.JaversBuilder JaVers instance started in 116 ms
How do I disable logging from javers?
You can set log level for Javers in logback.xml like this :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.javers.SQL" level="DEBUG"/>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
With this configuration you should only see DEBUG log messages displayed in pattern above.
If you want to completly disable logging you can use this :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.javers.SQL" level="OFF"/>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
The instruction: level="OFF" tells Logback to disable all log output for a given logger, in your case that is javers logger.

Camel (Spring Boot) custom log

Is there a way to log different routes in a single (human readable) log?
I need to log some routes in spring boot (using camel) in a txt file. These routes do some unmarshall/marshall stuff and i need to track that
Logback.xml is the easiest way to implement a human readable logger in springboot. It comes out of the box with Spring Boot and no configurations are required.
Create a logback.xml file in the resources folder and paste the following in it. You are set.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="logs" />
<!--Console Appender-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %green([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n
</pattern>
</encoder>
</appender>
<!--Rolling File Appender-->
<appender name="File-Logger" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/myLog.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${LOG_PATH}/archived/log_%d{dd-MM-yyyy}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>60</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="File-Logger"/>
</root>
</configuration>

Writing log in file and display it on console at the same time

here is my logback-spring.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<file>./target/log/app-debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log/target/app-debug.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<file>./target/log/app-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./target/log/app-error.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="DEBUG_FILE"/>
</root>
<root level="ERROR">
<appender-ref ref="ERROR_FILE"/>
</root>
</configuration>
Now everything log correctly but I am unable to see INFO level on my console while running the app. I still have
logging:
level:
root: INFO
file:
name: ./log/app.log
in my .yml file but there is no whatsoever logging on console.
Can I write the log in file and display it on the console at the same time?
You can use ConsoleAppender in your existing logback config. Below is a small snippet for your reference,
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
And you can control the root log level and also include the reference of your appender "DEBUG_FILE" as well to print in the console in addition to the implementation which you have done to write in a file. Below snippet is set to info to match your use case,
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="DEBUG_FILE" />
</root>

log message print twice with java logback

I use logback to print log in my SpringBoot application. When I check out the log file, I found that, all log message print twice! It is so strange .
Yeah, I have found some answer similar to my issue. But maybe they are not what I want.
here is my logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="LOG_HOME" value="${user.home}/app/logs"/>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-[%thread]-%-5level-%logger{50}: %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/cloud-sync-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file should be at most 10MB, keep 3 days worth of history, but at most 1GB -->
<maxFileSize>10MB</maxFileSize>
<maxHistory>3</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender">
<discardingThreshold >0</discardingThreshold>
<queueSize>256</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref ="FILE"/>
</appender>
<root level="INFO">
<appender-ref ref="Console"/>
<appender-ref ref="FILE"/>
<appender-ref ref="ASYNC"/>
</root>
</configuration>
How can I make sure log messages appear only once. Thank you.
It is because you have added ASYNC and FILE for INFO.
Please remove one of them based on your requirement.

SLF4J+Logback to create a file if doesn't exists/ if deleted

I have been researching for this for a long time and doesn't get a solution at all. My requirement is to create a log file if it is not there at the place.
Below is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="/home/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%p\t%d{dd MMM yyyy HH:mm:ss,SSS}\t%r\t%c\t[--%t--]\t%m%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_PATH}/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.example" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="debug">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
And I have tried changing the configuration to configuration debug="true", but no luck.
What is the right way to implement this ?
Please don't write to check on file permissions or ask me to use Log4j, I changed my application to use slf4j instead of log4j. If i delete the file also, it should create
You should use ch.qos.logback.core.FileAppender for file appending.
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile-${timestamp-by-second}.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
Since you've said that you worked with Log4j previously make sure you didn't exclude "spring-boot-starter-logging" from your .pom in tryings to avoid conflicts between Log4j and slf4j.
I'm using the same settings in logback.xml and everything is working fine
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>
<appender name="testServiceFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/test_service_%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<Pattern>%-5level %logger{35} - %msg %n</Pattern>
</encoder>
</appender>
<logger name="com.mycompany.test" additivity="false">
<level value="INFO"/>
<appender-ref ref="testServiceFileAppender"/>
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
</root>
</configuration>
in pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Resources