-2

Is String manipulation possible in YAML file??

The configuration file application.yml in a Spring Boot application is reading the version from the pom file as

<properties>
    <revision>10.10.11</revision>
</properties> 

The YAML file

logging:
  file:
    name: @revision@/app.log

The issue is, how to remove the dots from the revision value i.e.

"10.10.11" → “101011“

like

name: @revision@.replace('.', '')/app.log

, so that a log file can be generated on a folder without dots

Kishore_2021
  • 645
  • 12
  • 38

1 Answers1

1

For the general case, you could use SpEL which allows to call Java methods:

name: '#{"@revision@".replace(".", "")}'

You would need the outer quotes to tell yml that # does not start a comment, and to quote @revision@ so that it is interpreted as a String by SpEL.

The problem is that it does not seem to work with logging.file.name because it is read by LoggingApplicationListener & LogFile which does not seem to interpret SpEL.

It does not seem easy to customize this through Spring Boot configuration, but you could instead define your own listener (possibly based on the one above) to define your own naming scheme.

The following question might also help: register custom log appender in spring boot starter

Didier L
  • 18,905
  • 10
  • 61
  • 103
  • If using the above *name* value, getting the error `Logging system failed to initialize using configuration from 'null' java.util.regex.PatternSyntaxException: Illegal repetition near index 0 #{"10.10.11".replaceAll(".", " ")}/app.log.\d{4}-\d{2}-\d{2}.\d{1,2}.gz` – Kishore_2021 Feb 24 '22 at 13:48
  • 1
    @Kishore_2021 indeed, sorry about that, I had tested it with a different property. I’m afraid it is not possible to do what you ask using only yml config then. I think you will have to implement your own configuration instead of relying on Spring Boot then. – Didier L Feb 24 '22 at 16:44
  • We can't use our own configuration, because the log must be created before the start of the Spring Boot Application, and own configuration will be work after the application run, Isn't it?? Is my understanding correct? – Kishore_2021 Feb 24 '22 at 17:35
  • 1
    @Kishore_2021 I have updated the answer with an alternative possible approach for this specific case – Didier L Feb 24 '22 at 19:09