Profile specific logging in Spring Boot - java

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

Related

How to configure Spring Cloud logging?

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?

Use Spring Boot with explicitly configuration

Is it possible to use Spring Boot so that all configurations are explicitly in the main class?
For example, is it possible to tell spring-boot to print all autoconfigurations make by #SpringBootApplication so that I can copy paste in my main class?
Or is it possible to copy then from somewhere into the main?
You can have Spring Boot create a report (a list of auto configurations) simply by enabling debug mode in your application.properties file:
debug = true
The auto-configuration report contains information about the classes that Spring Boot found on the classpath and configured automatically. It also shows information about classes that are known to Spring Boot but were not found on the classpath.
And, because you've set debug=true in application.properties or application.yml, so you will see it in your output.
There is no way of doing this. Either you embrace the devil and suffer the consequences latter if you need to personalize something unpredictable by spring boot developers or you don't use it's magic.

Log4j to enable dynamically based on environment

I have a requirements to enable the console logs for Dev and SIT instances but not in production.
Like if(ENV=SIT) do logging otherwise not
And this should be dynamic so if requires I can enable the console logs in production as well .
It's means I want to have a variable in log4j.xml .
I gone through different filters that that doesn't deal with environment.
Please help with sample.
You can use springProfile in log4j Profile-specific Configuration
about spring profile: Spring profile

Where does Spring Boot store its default logging settings

I'm creating a Java Spring Boot 2.X application. In my configuration (application.yml), I have added the following property:
logging:
file: ${spring.applicaton.name}.log
This seems to work out of the box. However, I'm curious where the underlying default log configurations reside. Using google I found out that Spring Boot uses logback, but I cannot see a logback-spring.xml file. Also this other question seems to mention log4j2 instead of logback - does that work because they added a dependency on log4j2 (I have not added any dependency except for the sprint boot starter). what I also observed is that the two main logging mechanisms seem to be a rolling file appender and the console logger. Where is this defined?
My question is: where does Spring Boot pick up its default log configuration? I found these configuration files but I'm not sure if they are correct. Their naming convention and syntax is not what I expected. how do you choose a specific logging implementation - by dependencies or by configuration?
The default Logback configuration is stored in:
spring-boot-2.1.1.RELEASE.jar
/org/springframework/boot/logging/logback/base.xml
The default Log4j2 configuration is stored in:
spring-boot-2.1.1.RELEASE.jar
/org/springframework/boot/logging/log4j2/log4j2-file.xml
The default Java Util Logging configuration is stored in:
spring-boot-2.1.1.RELEASE.jar
/org/springframework/boot/logging/java/logging-file.properties
Note: The version of the jar file varies, of course.
Spring defaults to Logback. Read the Spring documentation for how to specify a different implementation and for how to configure it.

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