Change jooq log level to WARN during unit test - spring-boot

I want to change jooq log level to WARN so that the below log does not show up during unit tests
2022-11-10T04:38:33.3808516Z 04:37:24.486 [com.sp.analytics.tools.snowflake.JooqBindingsGenerator.main()] DEBUG org.jooq.meta.AbstractTypedElementDefinition - Type mapping : PUBLIC.FACT_SERVICE_CHARGE_TOMBSTONE.ROW_VERSION with type INTEGER
2022-11-10T04:38:33.3809963Z 04:37:24.486 [com.sp.analytics.tools.snowflake.JooqBindingsGenerator.main()] DEBUG org.jooq.meta.AbstractTypedElementDefinition - Type mapping : PUBLIC.FACT_SERVICE_CHARGE_TOMBSTONE.UPDATED_AT with type TIMESTAMP
I have added the <logger name="org.jooq" level="WARN"/> in my logback-test.xml. But the above DEBUG logs are still coming.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<root level="WARN"/>
<!-- SnapshotVerifier prints detailed snapshot on failure. Suppress that!-->
<logger name="au.com.origin.sn.SnVerifier" level="ERROR"/>
<logger name="com.sp" level="DEBUG"/>
<logger name="org.jooq" level="WARN"/>
<!-- Suppress the noise from Kafka in tests where broker is not available. !-->
<logger name="org.apache.kafka.clients.NetworkClient" level="ERROR"/>
</configuration>
Versions
logback - 1.2.3
slf4j - 1.7.30
spring-boot - 2.5.8
jooq - 3.14.6
Any suggestion on how can I change the log level of Jooq logger?

Related

Open Telemetry Logger MDC auto-instrumentation

The following Open Telemetry starters have been added to the Spring Boot project (v2.7.2) to instrument the application:
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-starter</artifactId>
<version>1.22.1-alpha</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-jaeger-spring-boot-starter</artifactId>
<version>1.22.1-alpha</version>
</dependency>
Traces and spans are successfully exported to a Jaeger collector. The problem is those traces and spans cannot be correlated with log statements because logs don't contain the current trace_id and span_id.
By following the documentation I added the logging.pattern.level property to application.properties but it seems like information about the current span is not injected into the logging event's MDC copy.
logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
For example:
log.info(
"traceId: {}, spanId: {}",
Span.current().getSpanContext().getTraceId(),
Span.current().getSpanContext().getSpanId());
2023-01-25 12:21:36.774 trace_id= span_id= trace_flags= INFO 34272 --- [nio-8080-exec-2] h.c.DemoController : traceId: 1bccb6a4fea8345026ca87a202f0091f, spanId: c59a5d44ee40cd2c
Have I missed anything?
opentelemetry-spring-boot-starter is missing this feature right now (version 1.22.1-alpha), but it can be added very easily.
Firstly, MDC Instrumentation for Logback dependency should be added to the project.
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-logback-mdc-1.0</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
Then add a custom logback.xml configuration to the classpath, which will automatically inject context information from the span context to logging events.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Default logback configuration -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<!-- -->
<appender name="OTEL" class="io.opentelemetry.instrumentation.logback.v1_0.OpenTelemetryAppender">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</appender>
<root level="INFO">
<appender-ref ref="OTEL"/>
</root>
</configuration>
Finally, add the property to application.properties (as mentioned in the question):
logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p

Logs in Spring Boot not showing in terminal

I have a project in Spring Boot where I am adding logs with the #Slf4j annotation, but I can't get them to show up in the terminal when starting the project.
This is the log: log.info("totalVacations: " + totalVacations);
When I run the command mvn spring-boot: run, in terminal I can't see the message.
Very likely you are missing the configuration of the appender, if you are also using LOG4J and you don't already have it, you can create a file named: log4j2.xml within the application's class path, and then create some basic configuration like:
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<RollingFile name="RollingFile"
fileName="./logs/spring-boot-logger-log4j2.log"
filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches
10 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<!-- LOG "com.baeldung*" at TRACE level -->
<Logger name="com.baeldung" level="trace"></Logger>
</Loggers>
</Configuration>
The key here is to set an appender to the console so that the logs are pushed there, and then at the logger section establish which level are you trying to get at the console appender, according to your example that should be INFO
If you are using just plain SLF4J with Logback go to src/main/resources/application.properties and add the following line:
logging.level.com=INFO
The ".com" is referencing the package to where this will apply, in this case is everything inside src/main/java/com.
and at the logback-spring.xml try this basic config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="your.name" level="INFO" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
</configuration>
You can find a very nice tutorial with more info here:
https://www.baeldung.com/spring-boot-logging
https://springframework.guru/using-logback-spring-boot/
Check whether #Slf4J import is from Lombok or some other library. I'm sure lombok #Slf4J default provides lowercase log and not LOG.

Is it compatible rsyslog with Logback-SLF4J?

I'm using spring-boot-starter-web like framework. These, use Logback (without implement other libraries) to manage logs with SLF4J like a facade. I need to use it with rsyslog but the official doc refers only to Syslog.
I tried to use the Syslog implementation since Syslog inherit for rsyslog but not found ( i attach my logback-spring.xml below ).
Any idea?
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property resource="application.properties" />
<!-- Syslog , make sure that syslog are enabled in the OS-->
<appender name="RSYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>127.0.0.1</syslogHost>
<facility>LOCAL0</facility>
<port>514</port>
<throwableExcluded>true</throwableExcluded>
<suffixPattern>%package.yes.rest %m thread:%t priority:%p category:%c
exception:%exception:%msg</suffixPattern>
</appender>
<logger name="package.yes.rest" level="info" additivity="false">
<appender-ref ref="RSYSLOG"/>
</logger>
</configuration>
Bonus clip: I see the choose of change Logback to Log4j2 too, but it's more stable use the inherit Logback.

Cannot suppress debug logging of framework classes with spring boot and logback

I am working on a spring boot microservice application that uses logback for logging.
The standard logback.xml is in the default place (main/resources) and is obviously used as I can add logger-statements and they are effective.
Its content looks like this (standard apart from removed comments and the two additional logger directives):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework" level="INFO"/>
<logger name="my.app.package.MyClass" level="DEBUG"/>
</configuration>
However, even though I put this into the logback.xml file:
<logger name="org.springframework" level="INFO"/>
I get tons of debug log output from framework classes like this here:
2015-09-16 10:49:40.733 DEBUG 96549 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'contextAttributes': no URL paths identified
What is wrong with my setup? Why are framework classes not respecting my logback config?

Spring and Log4j ver 2 - XML configuration example

I am trying to configure Log4j 2 in Spring XML configuration file for the first time (but unsuccessfully). I need to create two appenders - one for logging into console (>=DEBUG) and another for logging into database via JDBCAppender (>= INFO).
There is a problem because I don't know how to set another log level logger that differs from root logger.
Thank you for sharing some XML configuration sample. Thanks in advance!
You can set the level on the appender ref.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<appender name="A">
...
</appender>
<appender name="B">
...
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="A" level="info"/>
<appender-ref ref="B" level="debug"/>
</root>
</loggers>
</configuration>

Resources