A running app can tell you how it is doing, if you ask. Spring Boot Actuator is the part of Boot that answers: a set of HTTP endpoints under /actuator that report health, metrics, the beans and request mappings that really exist, and the log levels in use. The other source is the log itself. Neovim has no dashboard for either, but both are plain text, and Neovim is good at text.
Add Actuator without editing the project
Actuator is the spring-boot-starter-actuator dependency. Module 9 does not have it, and we left the companion code untouched, so we added the jars at run time. A throwaway pom.xml in ~/shop-extras, outside the repository, lists the starter, plus the PostgreSQL driver that Part 19 needs:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId><version>4.1.2</version>
</parent>
<!-- dependencies: spring-boot-starter-actuator and org.postgresql:postgresql -->
From the repository root, copy its jars once, then pass them to spring-boot:run:
./mvnw -q -B -f ~/shop-extras/pom.xml dependency:copy-dependencies -DoutputDirectory=$HOME/shop-extras/lib
./mvnw -B -pl module-09-catalog spring-boot:run \
-Dspring-boot.run.additional-classpath-elements="$(ls ~/shop-extras/lib/*.jar | paste -sd, -)"
The option wants a comma-separated list of jars. Pointing it at the folder compiled and started the app, but /actuator answered 404. In your own project, simply add the dependency to pom.xml.
Expose what you need
By default only health is on the web. In our run info, metrics, env, loggers, beans and mappings all answered 404. Switch them on for local work:
--management.endpoints.web.exposure.include=health,info,metrics,loggers,env,beans,mappings
--management.endpoint.health.show-details=always
Keep this on a development machine. The endpoints show configuration and can change log levels.
Read the answers with curl and jq
Run these in a Neovim terminal (<leader>ft) or a pane from Part 14. They ran against the catalog app on port 8091:
curl -s localhost:8091/actuator/health | jq -c .status # "UP"
curl -s localhost:8091/actuator/metrics | jq '.names | length' # 67
curl -s localhost:8091/actuator/mappings | jq -r '[.. | objects
| select(has("handler") and (.handler|tostring|test("CatalogController")))]
| .[].handler' # ...CatalogController#catalog(Pageable)
health with details listed a db component and its status. beans listed our five beans, such as catalogController and the two repositories. This differs from :SpringBoot Beans in Part 4: that reads the source, and this asks the running app. The env endpoint hides property values as ******; --management.endpoint.env.show-values=always shows them.
To keep an answer, read it into a buffer: :read !curl -s localhost:8091/actuator/health | jq and :set ft=json.
Change a log level while the app runs
The loggers endpoint reads and sets levels. We switched on logging without a restart:
curl -s -X POST localhost:8091/actuator/loggers/org.hibernate.SQL \
-H 'Content-Type: application/json' -d '{"configuredLevel":"DEBUG"}'
The answer was 204. The next request to /catalog wrote DEBUG ... org.hibernate.SQL lines to the log. Posting {"configuredLevel":null} reset the logger to INFO.
Live hovers and an HTTP client
The VS Code Spring Boot extension shows live bean data from a running app over JMX, as the Spring Tools wiki describes. spring-boot.nvim does not: its source says it has no equivalent for the server’s live information, so use the endpoints above. We did not test the startup endpoint, which needs code changes in the app. ’s util.rest extra installs kulala.nvim for .http files. Since version 6 that plugin needs a licence token, and LazyVim issue 7270 about it was still open when we looked, so we used curl.
Write the log as JSON and follow it
Two properties write the log to a file in the ECS JSON format, one object per line; use logging.structured.format.console=ecs for the console instead:
--logging.file.name=logs/shop.log --logging.structured.format.file=ecs
The file appeared in the module folder, and its timestamps are UTC while the plain console log shows local time. jq finds problems:
jq -r 'select(.log.level=="ERROR") | [.["@timestamp"], .message] | @tsv' logs/shop.log
In Neovim, :terminal tail -F logs/shop.log followed new lines once we pressed i for terminal mode. Add | jq --unbuffered -r '[.log.level, .message] | @tsv' for readable lines. A file already open in a buffer reloads with :checktime; press G for the end. <leader>sg greps the whole project: servlet.service found four lines in the log. If you exclude logs/ in .git/info/exclude, press <a-i> in the picker to include ignored files again; ours found 0 matches until then.
lnav, a terminal log viewer, also opened a Spring log inside a Neovim terminal and recognised its levels. Its keys are in its own documentation.
Recap
- Actuator needs
spring-boot-starter-actuator; without editingpom.xml, pass the jars with-Dspring-boot.run.additional-classpath-elements, as a comma-separated list. - Only
healthis exposed by default; setmanagement.endpoints.web.exposure.includefor local work and read answers withcurlandjq. POST /actuator/loggers/<name>changes a log level at runtime, andnullresets it.spring-boot.nvimhas no live data; we tested curl, not an HTTP-client plugin.- Structured file logs are JSON lines that
jqfilters;tail -Fin a Neovim terminal follows them and<leader>sgsearches them.
Series: Neovim as a Java IDE. Previous: Flame graphs from Java Flight Recorder recordings · Next: A complete development loop in Neovim
Sources
Official documentation and well-known tutorials read while writing this article. The wording here is our own.
- Spring Boot reference: Actuator endpoints
- Spring Boot reference: Loggers endpoint
- Spring Boot reference: Metrics
- Spring Boot reference: Logging and structured logging
- Spring Boot Maven plugin: Running your application
- Spring Tools wiki: Live Application Information
- LazyVim issue 7270: kulala.nvim 6.0.0 licence change
- lnav documentation
- Baeldung: Spring Boot Actuator
- Reflectoring: Exploring a Spring Boot app with Actuator and jq
- Baeldung: Structured Logging in Spring Boot
Comments