Docker中的Python登录 [英] Python Logging in Docker

查看:123
本文介绍了Docker中的Python登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试在Ubuntu Web Server上的Docker容器中运行python脚本。我正在尝试查找Python Logger Module生成的日志文件。以下是我的Python脚本

I am test running python Script in Docker Container on Ubuntu Web Server. I am trying to find the Log file generated by Python Logger Module. Below is my Python Script

import time
import logging



def main():

    logging.basicConfig(filename="error.log", level=logging.DEBUG)

    start_time = time.time()
    logging.debug("Program starts running at %d", start_time)


    i = 0
    while i < 1000:
        print(i)
        i += 1

    while_time = time.time() - start_time

    logging.debug("Program ends running at %d", while_time)

    start_time = time.time()

    logging.debug("Program starts running at %d", start_time)

    for x in range(0, 100):
        print(x)

    if_time = time.time() - start_time

    print('While took - %s Seconds' % while_time )
    print('If took - %s Seconds' % if_time )

    logging.debug("Program ends running at %d", start_time)


main()

我搜索并发现Docker文件生成json格式的日志文件,格式为 / var / lib / docker / container / {con_id} / {con_id} .log
此日志文件仅包含标准输出而且我找不到Python生成的日志文件。有什么方法可以检索文件。

I have searched and found that Docker file produces Log file in json format in /var/lib/docker/container/{con_id}/{con_id}.log This log file only includes the stdout and I cannot find the Log file generated by Python. Is there any way to retrieve the file.

推荐答案

您已指定文件'error.log'在命令 logging.basicConfig(filename = error.log,level = logging.DEBUG)中用于 logger 将您的日志放入。此文件位于容器内部,并且由于容器是无状态的,因此必须在关闭容器电源后将日志文件安装在本地计算机中的某个位置才能访问。您可以阅读以了解更多信息。

You have specified file 'error.log' in command logging.basicConfig(filename="error.log", level=logging.DEBUG) for logger to put your logs into. This file is located inside your container and since containers are stateless, you have to mount the log file somewhere in your local machine in order to have access after powering off your container. You can read this for more information.

顺便说一句,如果您想在日志文件已经打开的情况下对其进行访问,则可以使用Docker的 exec 选项通过该容器制作一个交互式外壳并找到日志:

BTW, if you want to access the log file while it's already up, you can use exec option of the docker to make an interactive shell through the container and find the logs:

docker exec -it ${container_name}

文档将对于 exec 命令行选项很有帮助。

This documentation will helpful for exec commandline option.

这篇关于Docker中的Python登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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