Spring boot with Thymeleaf static assets getting repetitive log statements - spring-boot

I'm converting from JSPs to Thymeleaf while converting a SOA service to Spring Boot. I'm wondering if I have not configured something correctly as I continue to get these statements:
o.s.b.a.e.m.EndpointHandlerMapping - Looking up handler method for path /css/bootstrap.min.css
o.s.b.a.e.m.EndpointHandlerMapping - Looking up handler method for path /img/gizmonicInstitute.jpg
o.s.b.a.e.m.EndpointHandlerMapping - Did not find handler method for [/img/gizmonicInstitute.jpg]
o.s.b.a.e.m.EndpointHandlerMapping - Did not find handler method for [/css/bootstrap.min.css]
Within my .html file (located within the /resources/templates directory)
<link th:href="#{/css/bootstrap.min.css}"
href="../../css/bootstrap.min.css" rel="stylesheet" media="screen"/>
.
:
<img src="/img/gizmonicInstitute.jpg"/>
With in my spring boot startup, I see ResourceHttpRequestHandler mapped as follows:
o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Is there something I'm not configuring? The pages are being discovered and are rendering fine.. just that these messages are littering my logs.

Turns out, that the existing SLF/Logback configuration which we had in an logback.xml file enabled root to be level info:
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="central"/>
</root>
Requiring a configuration level set for spring:
logging.level.org.springframework.*: WARN
Once I set the configuration level to WARN or above, these messages disappeared (which I interpreted as a misconfiguration with my spring boot migration). Shout out to #AndyWilkinson for directing my attention toward log messaging levels.
Update #1
A note that once the logback.xml is used, setting configuration level via properties does not seem to work. So I had to add this to logback.xml:
<logger name="org.springframework" level="WARN"/>

Related

Spring logging priority

The Spring logging documentation is available here and describes the different logging possibilities. However, something I have not been able to find is the priority of the different configurations. More concretely, in application.properties I enabled logging for showing basic authentication failures by doing:
logging.level.org.springframework.security.web.authentication.www=DEBUG
This works fines and enabled the logs I expect to see. Then I checked if I could override the configuration above via log4j2.xml by including:
<Logger name="org.springframework.security.web.authentication.www" level="ERROR" additivity="false">
<AppenderRef ref="CONSOLE"/>
</Logger>
The change in log4j2.xml did not cause any effect. So my impression is that the logging configuration included in applications.properties takes priority over log4j2.xml (or it is read after). The only note I can see in the documentation that may be related is:
Since logging is initialized before the ApplicationContext is created,
it is not possible to control logging from #PropertySources in Spring
#Configuration files. The only way to change the logging system or
disable it entirely is through System properties.
So my assumption is that the following will happen:
Logging is configured according to log4j2.xml.
Spring loads the Application context and then the logging configuration in application.properties is taken and "wins".
So my question is, if someone knows how this is supposed to work without guessing (a link will be appreciated). NOTE: I am interested because I want to be sure that no log4j2.xml will disable the spring-security logging.
Thanks in advance!
application.properties/application.yaml take precedence over log4j2.xml - search for precedence here
Environment variables take precedence over application.properties/application.yaml
A fuller precedence list is here

Can Ktor have different log levels per package/file

In spring boot, it's possible to specify a different log level at the package or even file level.
logging.level.com.company.project.monitors=DEBUG
logging.level.com.company.project.controllers=INFO
logging.level.com.company.project.controllers.utils=WARN
Is there anything similar in Ktor where we can set different logging for individual areas of the app (without writing a bunch of code)?
What you describe isn't Spring Boot specific feature as such, but of the underlying logging library both Spring Boot and Ktor use by default.
In order to achieve the same behaviour in Ktor, add the following lines to your logback.xml file:
<logger name="com.company.project.monitors" level="DEBUG" />
<logger name="com.company.project.controllers" level="INFO" />
<logger name="com.company.project.controllers.utils" level="WARN" />
The logback.xml should be located in your src/main/resources directory, if you're using Gradle as your build tool.
You can read more about Logback support in Ktor here: https://ktor.io/docs/logging.html#add_dependencies

spring boot logback refresh

I know that with config server and refresh endpoint, it is possible to dynamically change the logging level in spring boot application. To get control over the log rotation policy and json encoding for file, I decided to use logback. But this will stop me from dynamically changing the logging level.
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
This means that only info will be written to console/file. But what if I want to change it to debug/trace during runtime?
EDIT
I still dont understand the root level tag. But the logback seems to be taking the log level from application.properties, which basically answers my question.
You can change loggig levle using spring-boot-acutuator endpiont.
To check loging level call this GET method endpoint:
http://host:port/contextpath/actuator/loggers
To check root level loggers call this GET method:
http://host:port/contextpath/actuator/loggers/root
To change root log level call this POST method endpoint:
http://host:port/contextpath/actuator/loggers/root
header: content-type: application-json
body:
{"configuredLevel": "TRACE"}
By calling this endpoint u can change root log level.

How to remove Spring Data CustomConversions warnings from application startup?

I have an application with the following Spring dependencies:
starterBase : 'org.springframework.boot:spring-boot-starter:2.2.1.RELEASE',
starterActuator: 'org.springframework.boot:spring-boot-starter-actuator:2.2.1.RELEASE',
starterJpa : 'org.springframework.boot:spring-boot-starter-data-jpa:2.2.1.RELEASE',
starterTest : 'org.springframework.boot:spring-boot-starter-test:2.2.1.RELEASE',
starterWeb : 'org.springframework.boot:spring-boot-starter-web:2.2.1.RELEASE',
elasticsearch : 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.2.1.RELEASE'
In the moment that I added the elasticsearch dependency, the following Warnings appeared when I start the application:
WARN [main] o.s.data.convert.CustomConversions.register - Registering converter from class org.springframework.data.geo.Point to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might wanna check you annotation setup at the converter implementation.
WARN [main] o.s.data.convert.CustomConversions.register - Registering converter from interface java.util.Map to class org.springframework.data.geo.Point as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
WARN [main] o.s.data.convert.CustomConversions.register - Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might wanna check you annotation setup at the converter implementation.
WARN [main] o.s.data.convert.CustomConversions.register - Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoPoint as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
I debugged the code, and in spring-data-commons:2.2.1-RELEASE in CustomConversions.java, there is a private method with name 'register' in line 196, and its javadoc mentions the Mongo types, and it is strange, because we are not using Mongo. Is this Mongo reference correct?
But the main question is, is there any way to avoid/remove these warnings?
This code was refactored into spring data commons in April 2017, and the comment was copied from the original place and not adapted. So this is no mongo specific stuff here.
As for the warnings, all you can do at the moment is ignore them, we'll check if we need these at all.
Addition:
there is an issue for that, the corrsponding PR is in the pipeline of being processed. So hopefully these warnings will be dealed with soon.
I fixed it with adding to my application.yml:
logging.level.org.springframework.data.convert.CustomConversions: ERROR
If you use log4j2, you may ignore this error by adding a specific log level for this package, something like the following.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Loggers>
<Root level="info">
<!-- <AppenderRef ref="........"/> -->
</Root>
<Logger name="org.springframework.data.convert.CustomConversions" level="ERROR"></Logger>
</Loggers>
</Configuration>

Define Spring 5 Logging Level

I migrated a web-application (deployed on a Web-Server) from Spring 4 to Spring 5. That works fine. No problems in production environment.
But there is a issue in my development-environment:
The development server is running with the JVM option org.slf4j.simpleLogger.defaultLogLevel=debug. The problem is, Spring "picks up" that option and as a result Spring controllers talk a lot via SLF4J. The amount of debug-output overwhelmed other debug logging. I tried to set separate logging levels for Spring by JVM options like logging.level.org.springframework.web={info|error|warn|trace}.
Any ideas or solutions? Thank you in advance for your help.
EDIT (see Belistas comment) / Examples:
DEBUG org.springframework.web.servlet.view.JstlView - Added model object 'project' of type [.Project] to request in view with name 'specs-draft'
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/css/style.css] are [/resources/**]
Since everytime a object is added to the model Spring logs with level debug, there is a lot of output.
Amir Pashazadeh provided the hint that helps to solve the problem.
Using Logback instead of Simple Logger
Defining a logger for package "org.springframework" in the logback configuration file.
My solution was to ignore Spring debug logging and proceed Spring info logging only by a console output appender.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level %logger{16} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="info" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
That's it. Thank you all for your help.

Resources