I'm running a Spring Boot 2.1.6.RELEASE application using spring-boot-starter-actuator to expose the application's logfile over HTTP. My application.properties looks as follows (runnable example here):
spring.application.name=actuator-test
spring.application.instance_id=${random.value}
management.endpoints.web.exposure.include=*
logging.file=./target/logs/${spring.application.name}/${spring.application.instance_id}.log
When I run the application and call up http://localhost:8080/actuator/logfile I get a 404 error, even though the endpoint is listed in http://localhost:8080/actuator:
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
...
"logfile": {
"href": "http://localhost:8080/actuator/logfile",
"templated": false
},
...
}
}
The logfile itself is successfully generated and stored in the expected path.
I've found out that this has something to do with the use of placeholders in the logging.file property definition. As soon as I replace logging.file=./target/logs/${spring.application.name}/${spring.application.instance_id}.log with something like logging.file=./target/logs/test.log, everything works as expected.
Comment From: morth
Further investigation showed that this has more to do with the use of a random value in the logfile name than with the use of placeholders.
Working:
spring.application.name=actuator-test
spring.application.instance_id=foo
management.endpoints.web.exposure.include=*
logging.file=./target/logs/${spring.application.name}/${spring.application.instance_id}.log
Not working:
spring.application.name=actuator-test
spring.application.instance_id=${random.value} # or ${random.uuid} or ${random.int}
management.endpoints.web.exposure.include=*
logging.file=./target/logs/${spring.application.name}/${spring.application.instance_id}.log
or
management.endpoints.web.exposure.include=*
logging.file=./target/logs/${random.int}.log
Comment From: morth
The reason for this is that the ${random.value} expression is re-evaluated every time that http://localhost:8080/actuator/logfile is called.
It's evaluated first on application startup (determining the logfile name on the file system). Every time the endpoint is accessed and tries to resolve the logfile name in LogFileWebEndpoint.getLogFileResource() a new random value is generated, thus the logfile cannot be found.
Comment From: mxoop
我没配置随机值,也是报404