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.
Related
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
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.
I am using Spring boot.
I want to check which logging implementation is printing the message - I know with Spring boot default is Logback, and I have excluded it as mentioned in this post so mostly Logback will not be printing the messages, but I want to show it as a proof that Logback implementation is not printing and probably Log4j is printing.
Basically I need an API which I can call and I can get the details of which is the logging implementation, the way we can know Java version etc.
You can enforce Spring Boot to use a certain implementation by setting this property:
org.springframework.boot.logging.LoggingSystem
with any of:
org.springframework.boot.logging.logback.LogbackLoggingSystem
org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
org.springframework.boot.logging.java.JavaLoggingSystem
none (to swith off completely)
This explicit configuration would be your proof.
To check configuration you can install spring actuator framework. Through web endpoints all config params can be queried.
I am configuring logback programmatically. I am also using Spring Boot, and I would like to set the logs location in application.properties. I know in XML there is a springProperty tag, where is this in the Java API?
in spring-boot you can define logging config in application.properties
logging.path = /path/to/folder/log
logfile would then be named spring.log
[Using logging.path] Writes spring.log to the specified directory.
Names can ?be an exact location or relative to the current directory.
Here: is nice article about logging with springboot
I was trying to understand if I have some properties in application.yml and a few in application.properties will my application read from both these files?
According to Spring documentation - Change the Location of External Properties of an Application:
No matter what you set in the environment, Spring Boot always loads application.properties as described above. By default, if YAML is used, then files with the ‘.yml’ extension are also added to the list.
In which order properties are considered is explained in the chapter Spring documentation - Externalized Configuration.
If in doubt what files have been loaded I recommend to set the log level to DEBUG which shows the loaded configuration files in the log.
There is a good article here, that describes how both of these can be read using the #ConfigurationProperties annotation.
#ConfigurationProperties supports both .properties and .yml files.
#ConfigurationProperties support JSR-303 bean validation – javax.validation
Hope this helps!