Logback programmatic configuration - java

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

Related

Externalized configuration setting out of fat-JAR file

I am looking for a way to purely externalize some configuration settings in Spring boot application. For example: when double clicked on the fat-JAR file then it loads configuration from that, say myConfig.config, file which is in the same folder in which the fat-JAR file is. Then read the configuration from there and deploy the web-app. One use case is reading the port number from the config file and start the web-app on port number specified in the config file. If port number needs to be changed then only config file needs to be updated and restart the web-app.
I know that it is possible in .NET. I tried this link[1], but it is specifying config file in command line. Also, the #PropertySource can be used but again it winds up being in fat-JAR. There is Spring Cloud Config as well but I think that it would be overkill for small application. There are lots of tutorials available but they use one of the above mentioned method.
So, Is there any way to achieve that?
If yes, then what are the steps/link for that?
[1] Springboot externalizing log4j configuration
All you need to do is place an application.properties file at the same level as your jar. Spring Boot will find and use the application.properties w/o anything extra.

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.

Spring Boot application.yml and application.properties

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!

Is there a way to externalize Spring [Boot] app's log4j2 config on Tomcat?

Our Spring Boot application uses log4j2 for logging, but the admins of the server we are going to deploy it to require that:
the logging be fully configurable on their end, i.e., there must be a log4j2.xml file that they can edit to adjust logging formats, files, levels etc., and
The configuration must not be lost or overridden when a new version of the app is deployed (the upload war -> stop tomcat -> delete webapp folder -> start Tomcat) process will be automated.
Ideally, the path to log4j2.xml should be set in the webapps context .xml file — since that's where the DB connection config is. But I can't seem to find a way to make this work. All search results talk about using META-INF/web.xml or application.properties files (not an option, since they get overwritten on deploy), and the only SO question I could find (Spring application without web.xml log4j configuration) did not work for me ("No Configuration was provided") exception on startup.
Set the environment variable logging.config to be the location of your log4j2.xml file.
It's also recommended that you call it log4j2-spring.xml
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
section "Custom log configuration"

Does spring boot support using both properties and yml files at the same time?

I have a spring boot application and I want to use both a yml file for my application properties and also a plain application-${profile}.properties file set to configure my application.
So my question is can this be done and if so, how do you configure spring boot to look for both the yml file and the properties and merge them into one set per environment?
As to why I want/need to use both, it is because I like the flexibility and ease of use of yml files but an internal component (for encryption) requires using the properties file set.
I did see this point being made YAML files can’t be loaded via the #PropertySource annotation
but nothing stating whether both can be used together.
Please provide detailed configuration (XML or Java config) on how to get this working.
TIA,
Scott
I can answer my own question, as it just works as you would expect. The application.yml file and the appropriate application-${profile}.properties both get loaded and merged into the environment. Spring boot just makes this work naturally.
Yes You can use both at same time in same project.
When you use both YML and properties at same time, say for example
application.yml and application.properties at same time in same
project, first application.yml will be loaded, later
application.properties will be loaded.
Important point to be noted is that if application.yml and
application.properties have same keys for example in
application.yml has spring.app.name = testYML and
application.properties has spring.app.name = testProperties at same
time in same project, then application.yml value will be overwritten
by application.properties value since it is loading at last.
And the value in spring.app.name = testProperties.
Yes, you can run both without doing any configuration.
In Spring Boot, it picks .properties or .yaml files in the following sequences :
application-{profile}.{properties|yml}
application.{properties|yml}

Categories

Resources