Filter logs in spring-boot application - spring

In application.yml of my spring-boot application, I have the following code:
logging:
file: test.log
pattern:
file: "%d %-5level [%thread] %logger : %msg%n"
I would write something like
logging:
level:
org: hibernate: *
I would to filter and gather all log messages of all levels (ERROR, DEBUG, INFO, etc) ifrom only one specific package (org.hibernate) in "test.log". It is possible?

Yes, you can define log level for each of your/third library packages:
Example for application.yml:
# Fine-tuning a specific logger (for a single class)
logging:
level:
yourpackage: INFO
org:
hibernate: ALL
springframework: INFO
Example for application.properties:
# Fine-tuning a specific logger (for a single class)
logging.level.yourpackage.controllers.HomeController = INFO
# Specify the level for spring boot and hibernate's loggers
logging.level.org.springframework = INFO
logging.level.org.hibernate = ALL
Update 1: Only logging hibernate.* to a log file
If you want to log only one specific Java package to your log file add a logback.xml to your src/main/resources folder and add the following settings:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>hibernateLogs.log</file>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="ALL">
<appender-ref ref="FILE" />
</logger>
</configuration>
This config will log all of your hibernate logs to a file.

Related

How to clear log file before executing spring boot app?

I have been using the default spring logging configuration where I only specify filename in the application.properties file.
logging.file.name=app.log
But this by default appends logs when I start the application from cmd line "java -jar abc.jar"
I tried to search for the property which clears the file before starting application every time but couldn't find it. How should I clear the log file before starting the app?
Spring Boot uses Logback framework as as a default Logger.
You can use environnement variable via application.properties file to set some logging properties but logback xml configuration provides more powerfull features.
When a file in the classpath has one of the following names, Spring Boot will automatically load it over the default configuration:
logback-spring.xml
logback.xml
logback-spring.groovy
logback.groovy
So You can just put the code snippet below in src/main/resources/logback-spring.xml file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>app.log</file>
<append>false</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="Console" />
</root>
</configuration>
The line <append>false</append> does the job.
if you wanna log more information than those avaible with the encoder pattern <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> please check logback documention https://logback.qos.ch/manual/layouts.html#logback-access

Add datetime to log file name

I have some logs that I want to write to a file in a spring-boot microservice.
In my "application.yml" I have the below line which is working fine. But I also want to add date and time to the file name. How do I do that?
logging:
file: logs/${spring.application.name}.log
Ideally, I want the filename to be something like this:
logs/spring-boot-application_YYYY_MM_DD_HH_MM
A similar question has been answered here: How to include date in log file's name with Spring Boot / slf4j?
Basically if you want more control over the logging setup, you should have your own logback.xml under src/main/resources and configure the naming format similar to:
<FileNamePattern>LogFile.%d{yyyy-MM-dd}.log</FileNamePattern>
If you are using spring-boot , you could easily specify it in application.yml or application.properties and configuring logback xml to enable Rolling File Appender as :
spring:
logging:
file: logs/dev_app.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
level:
org.springframework.web: DEBUG
guru.springframework.controllers: DEBUG
org.hibernate: DEBUG
and specify the time based rolling policy for the file in :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="ROLLIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="ROLLIN" />
</root>
<logger name="org.springframework.web" level="INFO"/>
</configuration>
Doc
Official Spring Doc

Logging pattern in spring boot application

How do I set the logging file pattern to something like server.log.2017-12-22.gz?
As of now in my application.properties file, I have set the logging pattern to:
logging.pattern.file= "%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n"
logging.file=/var/opt/VS_Logs/server.log
But I need to store the files in the following format: server.log.2017-12-22.gz
As soon as you want custom rolling and triggering policies, you can no longer rely on Spring boot's logging configuration, and you have to use the vendor specific logging configuration. Here is an example using Logback and a TimeBasedRollingPolicy:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>server.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>server.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Logback will automatically gzip it when you use the .gz extension. If you save this file as logback.xml and put it on your classpath, Spring boot will automatically detect it, otherwise, you can use the logging.config property:
logging.config=classpath:logback.xml

How can I use YAML to configure multiple log files for Logback/Springboot?

I'm rewriting a small DropWizard application to run on SpringBoot.
My DW app has the following logging config which works:
logging:
level: INFO
appenders:
- type: file
currentLogFilename: /var/log/paas/console.log
archivedLogFilenamePattern: /var/log/paas/console.log-%d.gz
archivedFileCount: 7
loggers:
com.myorg:
level: DEBUG
appenders:
- type: file
currentLogFilename: /var/log/paas/paas.log
archivedLogFilenamePattern: /var/log/paas/paas.log-%d.gz
archivedFileCount: 7
This config separates my application and console messages into two separate logs.
When I try using this same configuration with SpringBoot, it has no effect. I'm able to write everything to a single log with the following config, but I really need to have two separate logs:
logging:
level:
org.springframework.web: INFO
com.myorg: DEBUG
file: /var/log/paas/paas.log
Is it not possible to do this with LogBack and YAML? Or is there an alternate syntax that will give me the same result I'm getting for my DropWizard app?
Spring Boot's YAML configuration of Logback only allows a single file appender. In order to configure Logback to use multiple file appenders you'll have to provide an explicit logback.xml or logback-spring.xml. If you remove the logging confoguraiotn from your application.yaml file and then add either a logback.xml or a logback-spring.xml to the root of your runtime classpath then Logback will be configured from that file.
Here's an example, using a logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<file>/var/log/paas/console.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/paas/console.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>...</pattern>
</encoder>
</appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/paas/paas.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/paas/paas.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>...</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="DEBUG_FILE"/>
</root>
</configuration>

Springboot sending logs to fluentd not working

I need some help for the following problem.
I have a spring boot application and I would like to configure a fluentd appender using logback.
I've created a file called logback.xml in my src/main/resources with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date - %level - [%thread] - %logger - [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FLUENT_TEXT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<tag>dab</tag>
<label>normal</label>
<remoteHost>localhost</remoteHost>
<port>24224</port>
<maxQueueSize>20</maxQueueSize>
</appender>
<logger name="org.com" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FLUENT_TEXT" />
</root>
</configuration>
In my build.gradle I have :
compile 'org.fluentd:fluent-logger:0.3.1'
compile 'com.sndyuk:logback-more-appenders:1.1.0'
When I launch the app using gradle bootRun I have the following message:
10:56:33,020 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - Attempted to append to non started appender [STDOUT].
10:56:33,020 |-WARN in ch.qos.logback.more.appenders.DataFluentAppender[FLUENT_TEXT] - Attempted to append to non started appender [FLUENT_TEXT].
10:56:33,028 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - Attempted to append to non started appender [STDOUT].
Exception in thread "main" 10:56:33,028 |-WARN in ch.qos.logback.more.appenders.DataFluentAppender[FLUENT_TEXT] - Attempted to append to non started appender [FLUENT_TEXT].
java.lang.NullPointerException
at ch.qos.logback.more.appenders.DataFluentAppender$FluentDaemonAppender.close(DataFluentAppender.java:72)
I've found here https://github.com/spring-projects/spring-boot/blob/master/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc something saying that logback.xml is loaded too early so I need to use a file called logback-spring.xml.
I've did it and it's like the file is never loaded, no error but nothing gets to my fluetd socket.
Any idea how to solve it ?
Thanks.
C.C.
When running your springboot application, load a 'spring' profile.
One way of doing it would be via the command line, see below.
-Dspring.profiles.active=spring

Resources