spring boot logback refresh - spring-boot

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.

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

How to disable logs of Exposed Framework?

I have a kotlin desktop application with gradle builder.
I added Exposed ORM framework for my sqlite DB.
Then I noticed this framework generates a lot of logs that I don't want to see in console (I want to see only my logs generated io.github.microutils:kotlin-logging).
Is there any way to disable logs from Exposed using gradle properties?
To disable (or change logging level) you have to check your logger framework implementation documentation. Both kotlin-logging and slf4j (which used by kotlin-logging) just provide facades for logging.
For example, if you use logback you could update your configuration to show only warns and errors from an Exposed:
<configuration>
// another code here
<logger name="Exposed" level="warn" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
</configuration>

Disable trace logging for tracer package for ElasticSearch Java Rest Client

I am doing a bulk indexing operation through ElasticSearch 6.3 Java Rest Client.
Data is getting indexed correctly however restclient is printing extensive logs in the file. see below:
2018-08-14 16:25:42,614 DEBUG [User=] [tracer] curl -iX POST 'http://1.2.3.4:9202/_bulk?timeout=1m' -d '{"create":{"_index":"ms","_type":"doc","_id":"24218000","version":-4}}
{"create":{"_index":"ms","_type":"doc","_id":"24217999","version":-4}}
{"data":"test","year":2018,"corrid":"24217999","mode":"USPS","emailaddress":"","mimeType":"application/octet-stream","title":"some title","type":"app","maildate":"2018-01-02","code":"abx","direction":"out","quarter":0,"no":"222876"}
{"create":{"_index":"ms","_type":"doc","_id":"24218345","version":-4
How can I disable or limit this?
This document talks about a little bit, but does not give enough information about it clearly.
I agree that their documentation is vague.
It sounds like you have an entry like
<logger name="tracer" level="TRACE" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
or
tracer.level=TRACE
somewhere in commons-logging configuration being imported. At the very least, I think you can set the level to WARN or ERROR to quiet the chatter.
As xeraa mentioned, you may also have logger levels set too high for org.elasticsearch.client, org.elasticsearch.client.RestClient, org.elasticsearch.client.sniffer, etc.

Spring boot with Thymeleaf static assets getting repetitive log statements

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

Resources