Load Logback config from the library in Spring Boot application - spring-boot

I'm trying to build a logging library which will be used across my services. I would like it to come with a default Logback configuration (predefined appenders). Is there a way to tell the application to load that config when the library is added to the project?
Ideally i would like to have this in the library, in application.yml
logging:
config: classpath:logback.xml
Application should use that configuration, and also allow to load custom application Logback config if developer wants to override the one.

Related

SpringBoot: activate log4j2 ScriptSupport

I use log4j2 for logging my SpringBoot application. I activate the yml file by 'config.classpath=log4j2.yml'.
I would like to use a ScriptFilter within the Log4j2 configuration. To do this, the corresponding language (e.g. groovy or javascript) must be activated in the current log4j2 version by the system property log4j2.Script.enableLanguages. Therefore I start the application with -Dlog4j2.Script.enableLanguages=javascript. Unfortunately, the error message appears when using the ScriptFilter: ERROR Script support is not enabled
Apparently the Log4j2 property is not being edited correctly. How can script support for log4j2 be activated within a SpringBoot application?

Spring Log4j2 xml file location

using Spring 2.0.5 with Log4j2
Have found works as expected if I place the file log4j2.xml in the resources folder.
However, have found the log4j2 option of monitorInterval="60" to be super useful as if some production issue can increase logging on some class without a restart. However if log4j2.xml is embedded in the jar of course it cannot be modified. so far the best I have been able to do is use 2 copies of log4j2.xml one in the resources and the other in the folder running the jar from.
I can then run:
java -Dlog4j.configurationFile=log4j2.xml -jar myapp.jar
it seems to work the same if started from the maven spring-boot plugin or from Eclipse.
Have tried a number of things such as setting the classpath, the absolute file name etc. but no luck
My question is how can I remove the log4j2.xml file from the resources folder and only specify it on the command line when starting spring?
I am not entirely clear on your question but I can provide the following information which I hope addresses it:
Log4j 2.12.0 added support for Spring Cloud Config and enhanced the support for Spring Boot. As of that version you can include a file named log4j2.system.properties and place any system properties you want defined there. They will be set before Log4j initializes. So you can specify the full URL to the configuration there if you want instead of the command line. You can also add the definition to a file named log4j2.component.properties.
The support for Spring Cloud Config allows you to place the configuration in your Spring Cloud Config server. See Spring CLoud Config support for more details.
Spring Boot initializes logging at least 3 times. The first is usually because the SpringApplication class declares a Logger so logging is initialized before anything else happens. The configuration for that will use "normal" log4j 2 initialization. After that Spring influences how logging initialization occurs primarily because Spring Boot sets the class path to include the jars inside BOOT-INF/lib directory inside your Spring Boot jar.
By using one of the configuration options I outlined above you can move the logging configuration outside of your application and you should not require a logging configuration in the resources directory. In fact, if you look at the sample Spring Cloud Config Application in Log4j you will see it does not include a configuration file in it.

Passing configuration from yaml file to spring boot application without rebuilding it

How to pass configuration through yaml file to a running spring boot application so that there is no need of rebuilding the application and changes are reflected while the application is runnig?
I think this can help you:
How to hot-reload properties in Java EE and Spring Boot?
Take a look at Spring Boot Cloud Config. It allows you to manage your config files centralized and has the ability to push new configurations to the connected applications.

How -Dlogging.level JVM argument is handled in spring boot, spring cloud

I have a spring cloud application, which under the covers uses spring boot.
In the bootstrap phase of the app I am using some classes from the apache commons config library under: org.apache.commons.configuration
My application is started with this flag per the spring docs:
-Dlogging.level.org.apache.commons.configuration=INFO
Despite this, i am still getting DEBUG level logs emitted during the bootstrap phase of spring....
I am lost as to how to properly specify the log level of INFO specified for the bootstrap phase of spring boot.
Ensure you are not setting debug=true in application properties or not passing --debug flag to the application.
In application.properties set:
debug=false
The configuration of spring boot has a specific order, so probably somewhere in the application or bootstrap configuration files the logging level is configured to DEBUG.
Source:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Can a Library jar read properties from Spring Cloud Config Server?

I have a shared Library that needs some properties from a .properties file. This shared Library will be used by a Spring Boot application. We already have a Spring Cloud Config server which is serving the properties to this Spring Boot application.
Now, we want the properties for the Library also to be fetched from the Config Server, rather than packaging it's properties file along with the library jar file.
Can I let my Library file pick-up properties from the Spring Cloud Config server ? The Spring Cloud Config server documentation states the following.. seeing it, looks like only a 'Spring Boot' application can talk to the Spring Cloud Config Server to fetch properties from there.
Any pointers or suggestions ?
can be used with any application running in any language
To use these features in an application, just build it as a Spring
Boot application that depends on spring-cloud-config-client
Ref: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_client
No, a Library cannot talk to the Spring Cloud Config Server to get the properties file. Instead the config server should have those Library file's properties define din the using application's properties file.
Library: x.jar
Application: y.jar
Property file in Config Server: y.properties / y.yml
This property file will have the properties for classes present both in x.jar and in y.jar

Resources