Define Spring 5 Logging Level - spring

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.

Related

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

Structured Stackdriver logs - adding MDC to logs

Added MDC to logs to be able track specific error logs in Stackdriver Dashboard and Logging Console. Current implementation is working fine on local machine but on cloud it is not - just don't include my MDC to log entry. The problem is that I cannot figure out what might be a problem at all.
Local logs output(contains "contextKey": "someValue"):
{"traceId":"615b35dc7f639027","spanId":"615b35dc7f639027","spanExportable":"false","contextKey":"someValue","timestampSeconds":1552311117,"timestampNanos":665000000,"severity":"ERROR","thread":"reactor-http-nio-3","logger":"com.example.someservice.controller.MyController", ...}
Kubernetes container log of the same service(no "contextKey": "someValue" in this log entry):
{"traceId":"8d7287fa0ebdacfce9b88097e290ecbf","spanId":"96967afbe05dbf0e","spanExportable":"false","X-B3-ParentSpanId":"224dcb9869488858","parentId":"224dcb9869488858","timestampSeconds":1552312549,"timestampNanos":752000000,"severity":"ERROR","thread":"reactor-http-epoll-2","logger":"com.example.someservice.controller.MyController","message":"Something went wrong","context":"default","logging.googleapis.com/trace":"projects/my-project/traces/8d7287fa0ebdacfce9b88097e290ecbf","logging.googleapis.com/spanId":"96967afbe05dbf0e"}
My logback.xml:
<configuration>
<appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.springframework.cloud.gcp.autoconfigure.logging.StackdriverJsonLayout">
<projectId>${projectId}</projectId>
</layout>
</encoder>
</appender>
<springProfile name="local,dev,test">
<root level="INFO">
<appender-ref ref="CONSOLE_JSON"/>
</root>
</springProfile>
</configuration>
Controller which trigger log creation with defined MDC:
#RestController
#RequestMapping("v1/my-way")
#Slf4j
public class MyController {
#GetMapping
public void read() {
MDC.put("contextKey", "someValue");
log.error("Something went wrong");
MDC.remove("contextKey")
}
}
Your MDC fields should be available at "labels" node of log entity. Did you check there?
Also at Google Console in Logs Viewer it's nice to set MDC fields to be shown in logs records. For that set "labels.YouCustomMDCField: at View Options -> Modify custom fields (at right top of the screen)
Update: However you're using different approach than me for log to Stackdriver. Im using
<appender name="STACKDRIVER" class="com.google.cloud.logging.logback.LoggingAppender">
<log>
${log_file_name}
</log>
</appender>
and it's do the trick (me also has Spring).

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