如何将日志文件保存到Docker卷中 [英] How to save logfiles into a docker volume

查看:106
本文介绍了如何将日志文件保存到Docker卷中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序。我将项目导出为.war文件,构建一个docker容器并运行它。

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

然后,我创建了一个logback.xml,该日志将与timestemp一起保存在我的HDD中。我也在这里找到了解决方案。

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>

可以,我的所有日​​志都将保存在Windows环境中的 / opt / docker-busapi下/ data / logs(我创建了文件夹结构)

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

我现在也可以通过dockerfile中的-v参数挂载额外的卷:

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

因为如果我检查了docker容器,该卷已装入。
通过 docker inspect busmicroservice我得到:

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"
    }

但是我的日志不会保存在我的日志文件夹中。
我想路径仍然有问题。我必须在 docker run命令中输入哪个路径?

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?

推荐答案

看起来像您的文件不一致路径名,在某些地方您已经定义了:

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

/opt/docker-busapi/data/logs/

在其他人中,您则有:

/opt/docker-busmicroservice/data

然后在Docker容器中,您是说应该在容器中使用它:

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

/logs

如果您希望所有文件进入 / opt / docker-busapi的主机目录 / data / logs / 然后,首先确保该目录存在并且您对其设置了正确的权限,然后确保您的应用程序使用 / 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.

然后在您运行时:

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

然后您应该很好。

这篇关于如何将日志文件保存到Docker卷中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆