How to configure Spring Cloud logging? - java

I have a Spring Boot application with Spring Cloud components, namely org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config.
The component produces logs at WARN level when the application starts which I want to suppress.
Setting a log level in the application.yml like
logging:
level:
org.springframework.cloud.aws.paramstore.AwsParamStorePropertySourceLocator: ERROR
did not work. Apparently its because this cloud component works at bootstrap time before the Spring Boot context starts and the application.yml is read.
How can I suppress this component logs in the best way?

Related

Use Spring Boot Cloud Config to change application.properties in classpath

I have to change my custom defined spring properties (defined via #ConfigurationProperties beans) during runtime of my Spring Boot application.
Is there any elegant way of doing this using Spring Cloud Config?
I don't want to use an external application.properties in a git repository (as the spring boot application gets shipped to customers and I dont' want to create a git repository for everyone of them).
I just want to access and change the local application.properties (the one in the classpath, located in src/main/resources) file in my Spring container or (if thats not possible) in the Spring Cloud Config Server, which I could embed into my Spring Boot app. Is that possible somehow?
BTW: The goal is to create a visual editor for the customers, so that they can change the application.properties during runtime in their spring boot app.
Spring Boot supports profile based application configuration. Just add application-<profile>.properties file. Then just when running the application select a profile depending on the environment making use of spring.profiles.active.
-Dspring.profiles.active=dev
This will run the application with application-dev.properties file (overriding the default application.properties, i.e you can just leave the common stuff in the default file and change the rest depending on the env)
On a side note, having a repo for configuration is not a must. You could just place them in the class path and give a search-location.
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:configs/
It actually is possible and in the end quite easy to achieve. It just took me a whole day to get all the information together. Maybe this helps someone:
You basically just need Spring Actuator, but for a certain endpoint, you also need the spring cloud dependency. (to make Post requests to the /env endpoint of Spring Actuator)
To alter your config at runtime, just add the following to your application.properties:
management.endpoints.web.exposure.include: env,refresh
management.endpoint.env.post.enabled: true //this property is only available when spring cloud is added as dependency to your project
If you (like me) don't need the feature of an externalized config, then you also have to add the following (otherwise, your Spring app will not start and throw an error that some config is missing)
spring.cloud.config.enabled: false
Now, if you send a POST request to /actuator/env endpoint with an object in the HTTP body in the form of {"name":"...", "value":"..."} (name is the name of a config property), then your config gets changed. To check that, you can do a GET request to /actuator/env/[name_of_config_property] and see that your config property has changed. No need to restart your app.
Don't forget to secure the /actuator endpoint in your SecurityConfig if you use a custom one.
It seems to me that you neither need the #RefreshScope annotation at your config classes nor the /actuator/refresh endpoint to "apply" the config changes.
Maybe what your looking for could be achieved with Spring cloud config and spring cloud bus. It's explained here: https://cloud.spring.io/spring-cloud-config/reference/html/#_push_notifications_and_spring_cloud_bus
In summary, any change on configuration sent an event to the spring cloud bus and you can then reload app context or configuration with new properties.

Profile specific logging in Spring Boot

We have a spring boot application using log4j2.
We want to use different log levels for different profiles. After lot of struggles, I found out that this is not possible with log4j2 but it is possible with logback. I don't want to use logback though.
Reason this is not possible with log4j2 is - spring boot initializes log4j2 well before it loads application properties.
So, I have initialized log4j2 with all the default values in log4j2.xml and after the application has started up, I'm getting the logger context and changing the log levels as per the profile specific log levels programmatically.
Though this is working, just wanted to understand if there is any better way.
According to this blog, separating log4j2 logging between profiles is possible. I also did it.
Why don't you create log4j2.xml based on profiles?. like below
spring:
config:
activate:
on-profile: dev
logging:
config: classpath:log4j2-dev.xml
---
spring:
config:
activate:
on-profile: sandbox
logging:
config: classpath:log4j2-sb.xml

Change Spring Webclient log level from DEBUG to WARN or ERROR

I have a very simple java application (not spring boot application), in which I am using Spring WebClient to parallely make HTTP calls. I have initialised it in global as below :
private static final WebClient webClient = WebClient.create();
and using it across my application. Now the issue is that when it starts, it prints a message as following :
21:10:59.417 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
After that it starts to print all(a ton of) debug logs, which hamper my capability to test and application
Is there any way to change the log level if WebClient is used as above, and not in SpringApplication ? I have tried all solutions offered in Disable Spring Boot Webclient logs, and nothing works.
You can set the level in application.properties like
logging.level.root= INFO

How to disable login in Spring Boot Netflix' eureka?

I'm setting up a microservice architecture using Spring Boot and Eureka. I followed several tutorials. However, the Eureka server is showing a login when trying to open the dashboard. Also, registration of a client fails (I think because of the enabled security).
How can I disable the security?
This is my configuration:
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.environment=dev
server.port=1112
I'm using a .properties file instead of .yml. What I tried is to add the property
security.basic.enabled=false
but the result was the same. So I tried to configure Spring Boot Security by myself but this caused 404 HTTP errors because the '/login'-controller was not found (and I don't want to develop this at the moment).
You have added two starter security dependencies in your Gradle. Those are configuring basic username password security. Please remove them.
you can also exclude the SecurityAutoConfiguration in your spring application main class
#SpringBootApplication(exclude = {SecurityAutoConfiguration.class})

how to set debug logging for spring framework using java configuration

I have seen a video whereby they enable debug logging for spring framework in a spring boot project. they created an application.properties file and entered logging.level.org.springframework = debug in the file.
I would like to do same for my application but I am not using spring boot. I have added dependencies like log4j but I am not sure about the settings.
Can someone share a link please?
thanks.

Categories

Resources