【问题标题】:Python - Different logs should be in different files, but appear on the same filePython - 不同的日志应该在不同的文件中,但出现在同一个文件中
【发布时间】:2017-12-23 01:08:01
【问题描述】:

我有一个 Web 应用程序,具有最少的日志记录功能。后端使用 Falcon(在 Python 上)在 Apache 上运行。

每个请求都有如下代码

msg = 'user: {usr} running {req} {req_uri:<30} | from: {loc_ip}:{loc_port} '.format(
        usr=req.env['REMOTE_USER'],
        req=req.env['REQUEST_METHOD'],
        loc_ip=req.env['REMOTE_ADDR'],
        loc_port=req.env['REMOTE_PORT'],
        req_uri=req.env['REQUEST_URI'])

    log_name = 'logs/{remote_ip}/{remote_ip}_{day}.log'.format(remote_ip=req.env['REMOTE_ADDR'], day=datetime.datetime.now().date().strftime('%d_%m_%Y'))
    os.makedirs(os.path.dirname(log_name), exist_ok=True)
    logging.basicConfig(filename=log_name, level=logging.DEBUG, format='[%(asctime)s] - %(levelname)s - [%(module)s:%(lineno)d] %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
    logging.info(msg)

我从 2 个不同的 IP(IPx 和 IPy)访问该应用程序 - 所以“logs”下应该有 2 个文件夹,“IPx”文件夹和“IPy”文件夹,每个文件夹中都有一个日志文件..

但是在我访问网络后,我看到了 2 个文件夹,但其中只有一个文件夹中有一个日志文件(比如说 IPx 文件夹中的 IPx_day.log),但是在查看了 IPx_day.log 文件后,我看到了:

[TIME] - INFO - [FILE] user: user1 running GET /domain | from: IPy:PORTy
// Other log statments..
[TIME] - INFO - [FILE] user: user2 running GET /domain | from: IPx:PORTx

这两行都在文件文件中 - IPx_day.log

有人知道为什么这两行出现在同一个文件中吗?

谢谢。

【问题讨论】:

    标签: python apache logging falconframework


    【解决方案1】:

    [我会将此添加为评论,但我没有这样做的声誉。]

    我认为这可能与仅配置根记录器的 logging.basicConfig() 有关。我认为您必须使用多个记录器,或者最好将多个处理程序附加到一个记录器。

    https://docs.python.org/3/library/logging.html#logging.basicConfig https://docs.python.org/3/library/logging.handlers.html

    【讨论】:

      【解决方案2】:

      从你的文字来看,我理解的是

      1. 您正在分别创建 2 个不同的文件夹和日志文件 目录。
      2. 您从 IPx 运行应用程序,并在 IPx.log 中看到日志,但 当您从 IPy 运行应用程序时,IPy 的日志位于 IPx.log 中。

      如果正确,请检查一下

      1. 一直在传递正确的IP[remote_IP]?
      2. 打印 IP 和 log_name 并检查您获取的 IP 和日志目录。

      我想,需要更改路径。除非你在基本路径中[脚本来源]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多