Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

java - How to save logfiles into a docker volume

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

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!");

Then i have created a logback.xml, that the logs will be saved at my HDD with a timestemp. I found the solution for this also here.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<include resource="org/springframework/boot/logging/logback/base.xml"/>

<logger name="org.springframework.web" level="INFO"/>

<timestamp key="Timestamp" timeReference="contextBirth" datePattern="yyyy-MM-dd'_'HH-mm-ss"/>

<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS}  - %msg%n</pattern>
    </encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/opt/docker-busapi/data/logs/busicroservice-${myTimestamp}.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d{yyyy-MM-dd_HH:mm:ss} - %msg%n</Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <FileNamePattern>busmicroservice.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <MaxFileSize>30MB</MaxFileSize>
    </triggeringPolicy>
</appender>

<logger name="mypackage" level="INFO" additivity="false">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</logger>

That works, all my logs will be saved in my windows environment under "/opt/docker-busapi/data/logs" (i created the folder structure)

I also could mount my extra volume now via the -v parameter in my dockerfile:

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

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

Because if i check my docker container, the volume is mounted. Via "docker inspect busmicroservice" i get:

    "Mounts": [
    {
        "Type": "bind",
        "Source": "/opt/docker-busapi/data",
        "Destination": "/logs",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    }

But my logs won't be saved in my logs folder. I guess there is still something wrong with the path. Which Path do i have to put in my "docker run" command?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you're not consistent on your file path names, in some places you have this defined:

/opt/docker-busapi/data/logs/

While in others, you have:

/opt/docker-busmicroservice/data

And then in the Docker container, you're saying that this should be used in the container:

/logs

If you want all your files to go to the host directory of /opt/docker-busapi/data/logs/ then, first be sure that directory exists and you have the correct permissions set on it, and then ensure that your application uses /logs as it's directory to save in.

Then when you run:

docker run -p xxxx:xxxxx -d -v /opt/docker-busapi/data/logs:/logs --name my_service --hostname my_hostname image:version

Then you should be good to go.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...