SpringBoot Logback configuration error - Empty or null pattern - spring

One of my spring boot applications suddenly stopped working, after server restart. The application is failing to start, these are the messages from log file
ERROR o.s.boot.SpringApplication - Application startup failed
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.classic.PatternLayout("") - Empty or null pattern.
Server: WebSphere Application Server
Spring Boot version: 1.5.21.RELEASE
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<file>app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>app.log.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="ROLLING-FILE"/>
</root>
</configuration>
I tried setting the log parameters in the application properties file but still doesn't work, properties like logging.path, logging.file, logging.pattern.file
The application used to work fine, no code changes were made for last few months, but after server restart it stopped working, application works fine in my local machine.

For people that might still be looking for an answer. We were able to solve this by editing our logback-spring xml by removing:
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
and if present (it was in our file but not in the code posted in the question) remove:
<appender-ref ref="CONSOLE"/>

Related

Trying to connect to Sentry ver 1.7.30 with Spring Boot ver 1.5.7

Hi I am getting confused with setting up old version of Sentry(ver 1.7.30) with current Spring Boot project(ver 1.5.7).
Here's my part of Gradle:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-logging")
compile("org.springframework.boot:spring-boot-starter-actuator")
...
implementation 'io.sentry:sentry-spring:1.7.30'
implementation 'io.sentry:sentry-logback:1.7.30'
}
application-local.yml
sentry:
dsn: https://dsnPath
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- sentry -->
<configuration>
<appender name="Sentry" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<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="Sentry" />
</root>
</configuration>
When I run the project on local laptop, I can see some exception logs on console but they don't show up on Sentry. One thing I'm sure is..there's log4j2-spring.xml before I created logback-spring.xml file only for Sentry and this might be a problem?
(I followed the instruction on official page - https://docs.sentry.io/platforms/java/legacy/spring/ - and it didn't work. Maybe I made mistake regarding resolver.)
Thanks in advance!

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

Spring Boot logback-spring.xml creates a log file under path /var/log even if custom log path is defined in the xml and application.properties files

I prepared a custom RollingFileAppender configuration in the logback-spring.xml file and application.properties files. The log file soduncu.log created under path /var/app/sefa/logs. This is expected behavior for the configuration but there is an unexpected situation that soduncu.log created under default Linux log path /var/log/. I tried to prevent this unexpected log file creation but I couldn't. What is the situation here and what I did wrong?
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty name="LOG_DIR" source="logging.path" defaultValue="/var/app/sefa/logs">
</springProperty>
<appender name="ROLLING"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/soduncu.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_DIR}/soduncu-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="ROLLING" />
</root>
</configuration>
application.properties:
logging.path=/var/app/sefa/logs
logging.file=/var/app/sefa/logs/soduncu.log
logging.level.root=INFO
logging.level.org.springframework=ERROR
logging.level.com.nuvia=DEBUG
Here is the one Linux process id link to a log file under two different path:
/proc/14368/fd/1 -> /var/log/soduncu.log
/proc/14368/fd/2 -> /var/log/soduncu.log
/proc/14368/fd/6 -> /var/app/sefa/logs/soduncu.log
I am unable to replicate this behavior with your code.
Try to change
name="LOG_DIR" source="logging.path"
to
name="LOG_DIR_PATH" source="log.dir.path"
and see what happens
make sure to delete soduncu.log from var/log first.
I hope it helps.

Print only application logs

I want to print my logs of Spring Boot application, but I want to print my application logs in INFO. It is writing everything from server startup to stop.
I want to write logs in INFO, that I wrote into my application.
How to do it in Spring Boot?
Disable startup log messages by envoking .logStartupInfo(false) in your Application builder.
It should look like:
new SpringApplicationBuilder(ServiceConfiguration.class)
.logStartupInfo(false)
.run(args);
Please refer to this link for more clarity.
OR
Add logback.xml inside src/main/resources and the contents should be:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
</encoder>
</appender>
<root level="off">
<appender-ref ref="STDOUT" />
</root>
</configuration>
For more information please take a look at this link

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