Duplicate logging in GCP Logging Viewer - spring

I am using Spring MVC Java in the application and the logs are an output of the application hosted on GCP. The logback.xml is defined in consul. I am getting duplicate logs in the output of the application
[][]2020-02-09T19:14:43,675+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
[][]2020-02-09T19:14:43,675+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
10.0.27.1 - - [09/Feb/2020:19:14:43 +0700] "GET /mobile-api/sys-info/healthcheck HTTP/1.1" 200 83 0.092
[][]2020-02-09T19:14:53,668+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
[][]2020-02-09T19:14:53,668+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
10.0.27.1 - - [09/Feb/2020:19:14:53 +0700] "GET /mobile-api/sys-info/healthcheck HTTP/1.1" 200 83 0.083
[][]2020-02-09T19:15:03,656+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
[][]2020-02-09T19:15:03,656+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
10.0.27.1 - - [09/Feb/2020:19:15:03 +0700] "GET /mobile-api/sys-info/healthcheck HTTP/1.1" 200 83 0.088
[][]2020-02-09T19:15:13,661+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
[][]2020-02-09T19:15:13,661+07:00 WARN
c.g.s.b.s.spring.SessionInterceptor - Invalid session, let's create new session
Here is my configuration file: logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scanPeriod="1 minutes" scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%X{key}][%X{user}]%date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", Asia/Jakarta} %-5level
%logger{35} - %replace(%msg){'\n', ' '}%nopex%n
</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

Logging with Java Spring in GCP is covered in the official Spring webpage. In 11. Stackdriver Logging Support. You need to add the corresponding dependencies mentioned in the page and change your logback.xml to:
<configuration>
<include resource="org/springframework/cloud/gcp/logging/logback-appender.xml" />
<root level="INFO">
<appender-ref ref="STACKDRIVER" />
</root>
</configuration>

Related

SpringBoot Logback configuration error - Empty or null pattern

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"/>

How to activate spring's internal logging in a Spring application, which is not a Spring Boot application

In the spring source code I see:
boolean debug = logger.isDebugEnabled();
What should I do to make debug true?
With Spring Boot, if I add the property logging.level.root=DEBUG, it works. But not in Spring.
Tested on springboot 2.1.9.RELEASE
Set root log level directly in config file application.yml
logging:
level:
root: trace
create file "logback.xml" under resources directory in maven project.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="false" scanPeriod="1 seconds">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger{36} [%file : %line] - %msg%n</pattern>
</encoder>
</appender>
<root level="trace">
<appender-ref ref="stdout"/>
</root>
</configuration>
debug="true" in config will output many useful debug log,such as
10:02:15,511 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/win/proj/my_web/manage/target/classes/logback.xml]
10:02:15,909 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to TRACE
10:02:15,909 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [stdout] to Logger[ROOT]
if you see below output Propagating, it means you have config log level in somewhere, like logging.level.root=info in application.properties
10:02:17,026 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator#495b0487 - Propagating INFO level on Logger[ROOT] onto the JUL framework

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>

How to configure logs to be only in JSON format in spring boot app?

In my spring boot app the following configuration is used for adding JSON logs via logback-spring.xml:
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>time</timestamp>
<message>msg</message>
<thread>thread</thread>
<logger>logger</logger>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
</encoder>
</appender>
<root level="all">
<appender-ref ref="consoleAppender" />
</root>
</configuration>
However, when running the app both versions of logs (non-JSON & JSON) are traced:
2017-08-08 07:31:49.718 INFO 6849 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 10000 (http/1.1)
{"time":"2017-08-08T07:31:49.718+02:00","msg":"Jetty started on port(s) 10000 (http/1.1)","logger":"org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer","thread":"main","level":"INFO","HOSTNAME":"ilya-ThinkPad-X1-Carbon-4th","caller_class_name":"org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer","caller_method_name":"start","caller_file_name":"JettyEmbeddedServletContainer.java","caller_line_number":144}
Is is possible to configure logback / logstash encoder, so that only JSON version would be available in the logs and non-JSON would be skipped ?
oh, stupid me - one should just remove the line from logback config which adds base logging:
<include resource="org/springframework/boot/logging/logback/base.xml"/>

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