【问题标题】:Python Logging - save to file with formattingPython Logging - 使用格式化保存到文件
【发布时间】:2019-10-14 09:20:33
【问题描述】:

我有 python 的日志记录模块配置了各种颜色编码的行。 但是,当使用文件处理程序导出到日志文件然后tail -f 实时跟踪文件时,颜色会丢失。

有没有办法将颜色格式导出到输出文件,例如tailless 和 co。能正常显示吗?

python 端的配置是通过以下方式完成的:

coloredlogs.install(
logger=_logger, fmt='%(asctime)s %(message)s', use_chroot=False,
field_styles={'asctime': {'color': 'cyan'}}, datefmt='%d.%m.%Y %H:%M',
level_styles={
    'info': {'color': 'white', 'bold': False},
    'warn': {'color': 'blue', 'bold': True},
    'error': {'color': 'magenta', 'bold': False}
}

)

谢谢

【问题讨论】:

  • 能否请您发布 sn-p 您如何配置彩色编码线的日志记录?您是否正在使用包coloredlogs、其他一些包、一些手工解决方案……?
  • 当然,我刚刚编辑了问题

标签: python linux bash ubuntu


【解决方案1】:

您可以通过设置isatty 参数来禁用自动检测是否应该使用彩色格式化程序(默认:在终端会话中)。

https://coloredlogs.readthedocs.io/en/latest/api.html#coloredlogs.install

简短的例子

import coloredlogs, logging

_logger = logging.getLogger(__name__)

coloredlogs.install(
    logger=_logger, fmt='%(asctime)s %(message)s', use_chroot=False,
    field_styles={'asctime': {'color': 'cyan'}}, datefmt='%d.%m.%Y %H:%M',
    level_styles={
        'info': {'color': 'white', 'bold': False},
        'warn': {'color': 'blue', 'bold': True},
        'error': {'color': 'magenta', 'bold': False}
    },
    isatty=True
)

_logger.debug("a debug message")
_logger.info("an info message")
_logger.warning("a warning message")
_logger.error("an error message")
_logger.critical("a critical message")

当您将输出重定向到文件时,该文件将包含 ANSI 转义序列。

00000000  1b 5b 33 36 6d 31 34 2e  31 30 2e 32 30 31 39 20  |.[36m14.10.2019 |
00000010  31 35 3a 31 35 1b 5b 30  6d 20 1b 5b 33 37 6d 61  |15:15.[0m .[37ma|
00000020  6e 20 69 6e 66 6f 20 6d  65 73 73 61 67 65 1b 5b  |n info message.[|
00000030  30 6d 0a 1b 5b 33 36 6d  31 34 2e 31 30 2e 32 30  |0m..[36m14.10.20|
00000040  31 39 20 31 35 3a 31 35  1b 5b 30 6d 20 1b 5b 31  |19 15:15.[0m .[1|
00000050  3b 33 34 6d 61 20 77 61  72 6e 69 6e 67 20 6d 65  |;34ma warning me|
00000060  73 73 61 67 65 1b 5b 30  6d 0a 1b 5b 33 36 6d 31  |ssage.[0m..[36m1|
00000070  34 2e 31 30 2e 32 30 31  39 20 31 35 3a 31 35 1b  |4.10.2019 15:15.|
00000080  5b 30 6d 20 1b 5b 33 35  6d 61 6e 20 65 72 72 6f  |[0m .[35man erro|
00000090  72 20 6d 65 73 73 61 67  65 1b 5b 30 6d 0a 1b 5b  |r message.[0m..[|
000000a0  33 36 6d 31 34 2e 31 30  2e 32 30 31 39 20 31 35  |36m14.10.2019 15|
000000b0  3a 31 35 1b 5b 30 6d 20  61 20 63 72 69 74 69 63  |:15.[0m a critic|
000000c0  61 6c 20 6d 65 73 73 61  67 65 0a                 |al message.|

【讨论】:

  • 太棒了,谢谢。需要注意的一件事,这适用于cattail,但不适用于less
  • @Ido_f 对于less,您可以运行它,例如python demo.py 2>&1 | less -R。 (请参阅man less 并搜索 RAW)
【解决方案2】:

这方面的选择很少,您可以使用sed 函数,例如: echo "WARN"|sed 's#WARN#\x1b[34m&#;'

或者你可以使用grc 这个porpuse,我在阅读日志时使用它

【讨论】:

  • 感谢您的回复和编辑建议。您基本上建议对输出进行搜索和替换,对吗?我应该可以直接设置记录器来做到这一点,对吧?
  • @Ido_f 不,你可以用 grc 配置一切,查看 grc 的 readme 部分,它有一些解释,超级好用,我认为非常好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2013-08-10
相关资源
最近更新 更多