使用 docker 挂载文件后,flask api 的日志没有输出了?

2019-08-06 17:33:33 +08:00
 Tracy1997

业务需求给每个 api 上了一个 log 装饰器记录日志,写入到 flask_log/app.log ,代码如下:

log_path = os.path.join(os.path.dirname(os.getcwd()), "flask_log") #没有 flask_log 文件就生成一个
if not os.path.exists(log_path):
    os.mkdir(log_path)

logging.basicConfig(level=logging.INFO)

#写入到 app.log 文件
handler = logging.handlers.RotatingFileHandler(os.path.join(log_path, "app.log"),  
                                               'a', maxBytes=1024 * 1024 * 200, backupCount=max_log_count,
                                               encoding="UTF-8")

# 定制日志格式
logging_format = logging.Formatter('pid:%(process)d - %(asctime)s - %(levelname)s '  
                                   '- %(filename)s:%(funcName)s[line:%(lineno)d] - %(message)s')
handler.setFormatter(logging_format)

# api 装饰器
def log(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            response = func(*args, **kwargs)
        except Exception as e:
            current_app.logger.error(make_log_message(request, str(e)))
            return "Sorry, some error happened. Info:\n{}".format(e), 500

        current_app.logger.info(make_log_message(request, response)
        return response

    return wrapper

在本机上测试没有问题,每次访问都会记录到 flask_log/app.log 中

但是用 docker 部署之后就会出现问题: 控制台日志输出正常, app.log 也会正常创建。

但是 app.log 一直为空不记录任何信息!

docker 挂载代码如下:

volumes:
      - /var/log/flask_log:/src/flask_log:rw  # docker 里面的 WORKDIR 为 /src
1990 次点击
所在节点    Python
6 条回复
Tracy1997
2019-08-07 08:50:29 +08:00
有没有大佬看看是挂载出了问题吗?还是 docker 哪里有问题
fyfy560
2019-08-07 09:21:16 +08:00
可以检查一下
/var/lib/docker/containers/<CONTAINER ID>/<CONTAINER ID>-json.log
Tracy1997
2019-08-07 11:00:47 +08:00
@fyfy560 谢谢!看了一下,不过容器 log 里面似乎没有什么有用的信息。
Tracy1997
2019-08-07 12:07:55 +08:00
救救孩子把
wenqiang1208
2019-08-07 15:41:37 +08:00
会不会是覆盖掉了
Tracy1997
2019-08-07 17:24:48 +08:00
@wenqiang1208 应该不是 我进到 docker 容器内看 日志是正常记录的 就是没有同步映射到宿主机

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/589576

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX