Kamitonishe
Kamitonishe

Reputation: 48

Can I log to an external file using a docker in spring java app?

I have spring app running inside a docker container and I use log4j for logging. Is it possible to write logs to external file? (I'm pretty new to docker)

In addition, I would not want to send logs to stdout (and then output to a file using the docker), is it possible to solve the problem in other ways? If it is not possible, how can I configure logging levels so that the docker can understand them and output?

Upvotes: 1

Views: 1284

Answers (1)

Oreste Viron
Oreste Viron

Reputation: 3805

To change the logging file path in spring, you can add :

logging:
  file: /my/path/file.log

in your application.yaml

The logging file will be created inside your container. If you want your file to be accessible outside your container, create a volume in your docker image : https://docs.docker.com/storage/volumes/

For exemple :

-v /my/real/path/:/my/container/path/

Docker doesn't know about logging level, but you can put environment variables in the application.yaml :

logging.level.root: ${LOGGING_LEVEL}

And then pass it to docker image with -e :

-e LOGGING_LEVEL=DEBUG

Upvotes: 3

Related Questions