【问题标题】:python logging alternatives [closed]python日志记录替代品[关闭]
【发布时间】:2011-04-22 03:33:22
【问题描述】:

Python logging module 使用起来很麻烦。有没有更优雅的选择?与桌面通知集成将是一个加分项。

【问题讨论】:

  • 看起来很有希望:packages.python.org/Logbook
  • 您觉得日志模块在哪些方面比较麻烦?您认为它的功能缺乏什么?
  • @NathanDavis 刚刚开始,你需要写大约 6 行......也想添加到标准输出 - 如果你能让它工作的话,那就是另外 4 行。

标签: python logging notifications


【解决方案1】:

您可以查看Twiggy,这是构建日志模块的更 Pythonic 替代方案的早期尝试。

【讨论】:

  • 似乎死了...上次提交是一年多以前
  • 不是死了,而是动了。文档位于 twiggy.readthedocs.org,开发位于 Github github.com/wearpants/twiggy
  • 截至今天,它似乎不适用于 Python 3.x - 也不适用于 2.x,github 自述文件说:“master 目前处于不断变化中,可能已损坏。”
  • @SHernandez:我今天在 3.6 上试了一下,效果很好。不确定这有多强大,但至少示例运行正常。
【解决方案2】:

结帐logbook,使用起来更好。

评论中提到了日志,但它值得自己回答。

【讨论】:

  • 试过了。它与 stdlib 日志记录有相同的问题:在我看来,文档不好。而且用户少得多,所以没有谷歌搜索结果。我回到 stdlib 日志记录,发现 stackoverflow.com/questions/7507825/… 最有帮助。
【解决方案3】:

您可能想看看pysimplelog。它是纯 python,使用非常简单,可 pip 安装并提供您需要的东西

from pysimplelog import Logger
L=Logger()
print L
>>> Logger (Version 0.2.1)
>>> log type  |log name  |level     |std flag  |file flag |
>>> ----------|----------|----------|----------|----------|
>>> debug     |DEBUG     |0.0       |True      |True      |
>>> info      |INFO      |10.0      |True      |True      |
>>> warn      |WARNING   |20.0      |True      |True      |
>>> error     |ERROR     |30.0      |True      |True      |
>>> critical  |CRITICAL  |100.0     |True      |True      |

L.info('I am an info')
>>> 2016-09-26 15:01:17 - logger <INFO> I am an info

L.warn('I am a warning')
>>> 2016-09-26 15:01:17 - logger <WARNING> I am a warning

L.error('I am an error')
>>> 2016-09-26 15:01:17 - logger <ERROR> I am an error

使用这些参数,将自动为您创建和更新一个“simplelog.log”文件

【讨论】:

  • +1 嘿,看起来很酷!但是请注意:我不建议在已知whether that is safe 之前使用函数定义之外的日志记录
【解决方案4】:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.handlers
from logging.config import dictConfig

logger = logging.getLogger(__name__)

DEFAULT_LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
}
def configure_logging(logfile_path):
    """
    Initialize logging defaults for Project.

    :param logfile_path: logfile used to the logfile
    :type logfile_path: string

    This function does:

    - Assign INFO and DEBUG level to logger file handler and console handler

    """
    dictConfig(DEFAULT_LOGGING)

    default_formatter = logging.Formatter(
        "[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s",
        "%d/%m/%Y %H:%M:%S")

    file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8')
    file_handler.setLevel(logging.INFO)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)

    file_handler.setFormatter(default_formatter)
    console_handler.setFormatter(default_formatter)

    logging.root.setLevel(logging.DEBUG)
    logging.root.addHandler(file_handler)
    logging.root.addHandler(console_handler)



[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module

您可以使用控制台和文件配置日志文件,我认为桌面通知不是一个好主意,您可以从控制台和日志文件中查看日志信息

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2010-12-21
    • 2012-10-23
    • 2010-12-05
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多