1

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

3
  • 1
    Maybe read a tutorial? Short story - create a logging configuration file and point your application at it. Commented Dec 12, 2017 at 14:02
  • yes, i know how to save my logging into a .txt file. But how to point to $LOCAL_DATA_PATH:/logs? Because otherwise i save it locally on a windows machine Commented Dec 12, 2017 at 14:07
  • Write the logs to /logs. I don't really understand your question. Commented Dec 12, 2017 at 14:07

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I tried your solution, and partially it worked, but now i have restructured my code and because of this i have a new problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.