InfoEngi
InfoEngi

Reputation: 323

How to save java.util.logging output in docker volume

i have a java application. I export my project as a .war file, build a docker container and run it.

The structure is the following. For the logging i use the library from java.

import java.util.logging.Logger;

In my application i define my variable:

private static final Logger logger = Logger.getLogger(BusController.class.getName());

And for the output i use for example:

logger.warning("User "+XYZ+" not found!");

When i start my dockerfile, i mount an extra volume for the logs.

REGISTRY=xxxxx.net
VERSION=latest
IMAGE=busMicroservice
SERVICE=busmicroservice
LOCAL_DATA_PATH=/opt/docker-busmicroservice/data

docker run -p xxxx:xxxxx -d -v $LOCAL_DATA_PATH:/logs --name $SERVICE --hostname $SERVICE $REGISTRY/$IMAGE:$VERSION

After the docker container has started successfully , i can check my volume with

docker inspect busmicroservice

and can see, that my volume is included. But what do i have to do now, that the logs will be saved in my folder "/logs"? What do i have to implement in my java application?

Thank you in advance!

--------- EDIT ----------

I have a second thread now for an additional question: How to save logfiles into a docker volume

Upvotes: 1

Views: 3100

Answers (1)

sanath meti
sanath meti

Reputation: 6539

You need to use log4j file appender. Edit log4j.properties

log4j.appender.default.file=org.apache.log4j.FileAppender
log4j.appender.default.file.append=true
log4j.appender.default.file.file=/log/logfile.log
log4j.appender.default.file.threshold=INFO
log4j.appender.default.file.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.layout.ConversionPattern=%-5p %c: %m%n

When application starts it will write log in /log/logfile.log file

For complete sample with java application can refer below. https://dzone.com/tutorials/java/log4j/log4j-file-appender-example-1.html

Upvotes: 2

Related Questions