Spring Boot 3: Can't set logging.pattern.console in application.yaml - spring

I'm trying to add a "correlation-id" to the logs that are written by my Spring-Boot application.
For that I've taken the spring-boot logback default value and added it to my application.yaml, which looks like this:
logging:
pattern:
console: ${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}
But now my application isn't starting anymore and fails with:
Could not copy file '/my-project/src/main/resources/config/application.yaml' to '/my-project/build/resources/main/config/application.yaml'.
Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed:
SimpleTemplateScript62.groovy: 1: Unexpected input: '(' # line 1, column 10.
out.print("""spring:
Putting "" around the pattern doesn't help
replacing ":-" with ":" doesn't make a difference
Removing all the variables (and putting "" around the pattern) works
Setting the pattern as an environment variable works too
Is there something missing in my approach? I would prefer to have this change in my application.yaml instead of an environment variable so that not everyone needs to configure it locally to have the same log output as it is expected on production.
I'm using Spring-Boot 3.0.2 with gradle 7.6

Related

Yamllint exclude # symbol check in application.yaml file

I have used linter to lint spring-boot framework application.yaml file:
---
spring:
application:
name: #project.name#
#project.name# value allows spring-boot application to resolve application name from pom.xml file.
However when I run linter (yamllint ./src/main/) I get an error:
Error: ./src/main/resources/application.yaml:4:11: [error] syntax error: found character '#' that cannot start any token (syntax)
Is there any rule how can I exclude special characters check?
That's a syntax error. yamllint uses PyYAML for parsing, which yields this error. PyYAML obviously cannot ignore a syntax error, and by extension, yamllint cannot ignore it.
Your best bet is to preprocess the file to replace all referenced variables, e.g.
sed -E 's/#([^#]*)#/_\1_/g' application.yaml | yamllint -
This replaces the # characters around variable references with _, which keeps the line length.

logging.level.root doesn't work in springboot

I want to setting the default logging level to error on springboot
enter image description here
But the console still has the dubug and info output. It seems that logging.level.root=error doesn't work.
Be carefull if you are using Spring Boot Devtools, the properties defined in $HOME/.config/spring-boot folder will override all other properties as specified in Spring Boot documentation : https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html
I found a environment variable named debug,even though its value is a string not true,which caused the problem.Actually,I tried to remove the variable before,but I didn't restart the eclipse.Now,I remove the varibale named DEBUG and restart the eclipse,and it success.

how to preserve quotes when referencing a value using placeholders in a YAML file with Spring Cloud Config?

I have a password ending in a colon, say abc:, that I want to store encrypted in GIT and decrypt with Spring Cloud Config.
Here's the expected YAML file :
spring:
datasource:
password: "abc:"
Here's the encrypted YAML file :
spring:
datasource:
password: "{cipher}blablabla"
And here's what I receive from Spring Cloud Config after decryption :
spring:
datasource:
password: abc:
Which is interpreted as a key instead of a value by the YAML parser.
Is there a way to tell Spring Cloud Config that I want to surround the decrypted value in quotes or something similar ?
EDIT
My mistake, Spring Could Config server actually adds quotes when needed after decrypting the string. So the result is as expected :
spring:
datasource:
password: 'abc:'
The issue is that when I reference this value using a placeholder the quotes disappear in the process :
a_key:
another_key: ${spring.datasource.password}
becomes when processed by Spring Could Config server :
a_key:
another_key: abc:
So the question really is : how to preserve quotes when referencing a value using placeholders in a YAML file with Spring Cloud Config?
The only way I found so far is to leave the placeholder resolution to runtime by escaping the $ character :
a_key:
another_key: \${spring.datasource.password}
The resulting YAML served by Spring Cloud Config server is then :
a_key:
another_key: ${spring.datasource.password}
Link to Spring Cloud Config documentation here

Spring Boot yaml configuration doesn't load second key

I'm trying to create a config file in yaml for my Spring Boot project.
For a test I just setup some values:
user:
test: hello
Now I'm trying to get the information of user.test, but I get the error message Could not resolve placeholder 'user.test' in value "${user.test}"
The strange thing is, that if my config files just has user.test: hello, then everything works fine.
Is there something that I have to setup before to work with yml files as properties?

Spring command line JSON config containing array

I am using Grails 3 Elasticsearch plugin with Springs external JSON configuration by setting spring.application.json as system property.
The properties are available in the application but I can't find a way to initialize an array properly.
What I am trying to accomplish is to override the default values of the hosts property specified in my application.yml:
environments:
development:
elasticSearch:
client:
hosts:
- {host: "myhost.com", port: 9300}
- {host: "anotherhost.com", port: 9300}
I am setting the property from the command line as follows:
-Dspring.application.json={"environments":{"development":{"elasticSearch":{"client":{"hosts":[{"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}]}}}}}
I would expect environments.development.elasticSearch.client.hosts to contain an array like it does when initialized from the application.yml, but in fact environments.development.elasticSearch.client containes host[0] and host[1], where each contains the host and the port. The host array from the yml file is still there.
How can I achieve the same behavior using the command line as with the application.yml file?
I believe you can do this the same way that you would if it was set in a .properties file, using a list:
-Denvironments.developmet.elasticSearch.client.hosts={"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}
and I believe you can also do it as an environment variable...
set ENVIRONMENTS_DEVELOPMENT_ELASTICSEARCH_CLIENT_HOSTS='{"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}'
There may need to be some quotes around parts of these depending on the shell you are in, the OS, etc.

Resources