Logback file and access spring boot configuration - java

How can I put into spring boot app properties logback-file.xml and logback-access.xml at the same time? And if I can, logs will write in one file, or in different?

See the available Spring Boot properties.
You will find generic logging.* ones as well as server.tomcat.accesslog.* OR server.jetty.accesslog.* depending on your embedded servlet container.

Core logs will be printed based on logback-spring.xml, and access-logs (logs written whenever someone accesses any of your API's) will be printed based on logback-access.xml.
In application.properties, use logging.* for core/server logs and server.(yourserver).accesslog.* for access logs.

Related

CompositeConfiguration in log4j2 using springboot

I am working in spring boot project and I stuck in an issue. Following is the problem.
I have two log4j2 configuration file. Log4j2-1.yml and Log4j2-2.yml. Log4j2-1.yml consist the common configuration to avoid repetition duplicate configuration while Log4j2-2.yml consists application specific log config. I need to use these two log4j2 configuration in my application at same time. Please help me to configure these files in my application.\
I tried log4j2.configurationFile also but could not succeed.
Spring Boot has a custom logging configuration process that occurs just after the Log4j2 automatic configuration (its exact logic is in Log4j2LoggingSystem and is documented in Core Features). You can analyze the process by setting -Dlog4j2.debug=true.
To create a composite configuration you need to use two Spring Boot properties (cf. core properties):
logging.config=classpath:Log4j2-1.yml
logging.log4j2.config.override=classpath:Log4j2-2.yml

Changing Tomcat properties dynamically in Spring Boot

Generally, I specify Tomcat properties in the application.properties file of my Spring Boot application. Is there any way to change some of those properties (which can be changed) dynamically, in the runtime? May be, by hitting an endpoint?
If not, is there any other way to make them dynamic from within the application code (and not the properties file)?
There are some libraries that can be configured both from the application.properties and dynamically as well (using endpoints). Does Tomcat provide any such mechanism?
Yes, there is. You can configure and hit endpoints to reconfigure:
https://cloud.spring.io/spring-cloud-config/reference/html/

How to disable write operation for Jolokia in Spring boot?

I have a Spring boot (v1.5.9) based application with Jolokia provided by Spring boot actuator.
Jolokia works fine. I can read values, for example:
http://localhost:8080/jolokia/read/java.lang:type=ClassLoading/Verbose
gives me:
{"request":{"mbean":"java.lang:type=ClassLoading","attribute":"Verbose","type":"read"},"value":false,"timestamp":1527859447,"status":200}
What I want is to disable writing operations, for example:
http://localhost:8080/jolokia/write/java.lang:type=ClassLoading/Verbose/true
The spring boot configuration looks like this:
management.security.enabled=false
management.endpoints.jmx.exposure.exclude=*
management.endpoints.web.exposure.include=jolokia,metrics
management.endpoint.jolokia.config.policyLocation=classpath:/jolokia.xml
And the Jolokia's policy in WEB-INF\classes\jolokia.xml (in resulting war, according to https://jolokia.org/reference/html/security.html) contains:
<restrict>
<commands>
<command>read</command>
<command>list</command>
<command>version</command>
<command>search</command>
</commands>
</restrict>
Despite this I see the following note in application's log:
jolokia: No access restrictor found, access to any MBean is allowed
And the write operation from the example above is working fine.
What I'm doing wrong? Should I put the policy file somewhere else? Is it possible to configure Jolokia's policy directly from the Spring boot configuration?
It looks like you've inadvertently used Spring Boot 2.0 configuration properties with Spring Boot 1.5.x. In 1.5 you should use jolokia.config.policyLocation. There's a little more information in the reference documentation.

Expose logs in Spring Boot Admin without having log file

I am currently using Spring Boot Admin to manage a Spring Boot application. SBA exposes correctly the logs of the application in the tab "Log". As part of transitioning the deployment to Docker I would like to continue forwarding our logs to stdout and ELK, but to stop logging to a rotating log file.
After removing from Spring Boot application.properties file the property "logging.file" I could check that, as expected, the rotating log file was not created. However this configuration change had the side-effect of removing from SBA the "Log" tab exposing the logs.
My question is: is there any way I could still expose logs through Spring Boot Admin without having to have a log file for the application?
Spring Boot admin needs the logfile acuator endpoint. The endpoint needs a file on the disk. It either serves the file from loging.file or endpoint.logfile.external-file.
Here is a simple workaround:
Add a link to a kibana query for the application (or similar) to your info endpoint l. It is rendered as hyperlink in SBA. This would be a convenient way for the users to find the log output.

Separate properties file for multiple environments

I am trying to have separate property files for prod and dev environment.
I have two property files application-prod.properties, application-dev.properties placed in classpath:/config
I added VM option -Dspring.profiles.active=dev
According to what I understand from the documentation and many other references on the web, on accessing Spring Environment as environment.getProperty("") the property in "application-dev.properties" should be loaded. However, I am getting null and as it seems the property files are not read by Spring.
I also tried defining both the files in #PropertySource. Doing this, the file defined second is picked up and the corresponding property is returned. Spring is not choosing the file based on the active profile.
Am I missing something?
I also came across a issue raised through some SO questions, but I am not sure if it refers to the same problem.
Right, so documentation you are pointing to is from Spring Boot project. That is not the same as Spring Framework. If you are not using Spring Boot, -Dspring.profiles.active=dev wouldn't work.
You have two options:
Introduce Spring Boot to your project ans turn on auto-configuration (#SpringBootApplication or #EnableAutoConfiguration).
Use plain Spring Framework features like PropertyPlaceholderConfigurer, but it doesn't give you same flexibility as Spring Boot features and you will need to create some boilerplate code to handle various envs.

Categories