还有一个python记录器,它简化了json文件日志记录并美化了控制台输出
lazylog的Python项目详细描述
懒虫
另一个python记录器的目标是:
- 简化日志设置-下至配置和单行
- 最小-单个文件,无依赖关系
- 支持不同日志级别、不同格式的控制台和文件日志记录 以及文件在框外旋转
- 美化align控制台输出,使其更具可读性
- 允许简单的json日志记录(todo)
- “修复某些默认日志记录工具问题”:
- 通过重新添加前导码或转义来处理消息中的新行 新行字符
- 根据日志级别为控制台添加颜色
- 美化基本结构(列表、dict和元组)
- 处理多用户部署的日志文件权限
- 支持简单的日志模拟(到stringio中)
安装
您可以通过pip
:
pip install lazylog
或者您可以下载项目中的代码:
curl https://raw.githubusercontent.com/urban-1/lazylog/master/lazylog/__init__.py > /path/to/lazylog.py
用法
注意:有关此自述文件的漂亮颜色,请访问 readthedocs version
note2:您可以在examples
文件夹中找到此部分的所有源代码
基础知识
在最简单的形式中,可以使用:
初始化lazylog
# Import the Logger class
from lazylog import Logger
# Initialize with all defaults
Logger.init()
这将创建一个仅限控制台的记录器,并将所有值设置为其默认值:
{
'color': True,
'splitLines': True,
'level': logging.DEBUG,
'pretty': True
}
color
:指示lazylog
为不同级别使用不同颜色splitLines
:通过在每个 行level
:像往常一样,设置终端的日志记录级别pretty
:使修饰dict、list和tuple更具可读性
初始化后,您可以像往常一样使用logging
模块-没有什么特别的:
logging.debug("This is an example log line")logging.info("This logger handles\nNew\nLines")logging.warning({"also":"handles basic structures","like":list("lists"),"and":("colors","!")})logging.error("Errors stick out")logging.critical("but critical sticks out more...")
输出应该如下所示:
08-05-2018 21:40:16.943 13859:139943539918656 DEBUG console 17 : This is an example log line08-05-2018 21:40:16.943 13859:139943539918656 INFO console 18 : This logger handles
08-05-2018 21:40:16.943 13859:139943539918656 INFO console 18 : New
08-05-2018 21:40:16.943 13859:139943539918656 INFO console 18 : Lines08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : (dict) {
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 'and': (
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 'colors',
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : '!'
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : ),
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 'like': [
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 'l',
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 'i',
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 's',
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 't',
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 's'
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : ],
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : 'also': 'handles basic structures'
08-05-2018 21:40:16.943 13859:139943539918656 WARNING console 19 : }08-05-2018 21:40:16.943 13859:139943539918656 ERROR console 20 : Errors stick out08-05-2018 21:40:16.943 13859:139943539918656 CRITICAL console 21 : but critical sticks out more...
定制
您可以自定义lazylog
,并禁用任何您不喜欢的功能,因此
# InittermSpecs={"level":logging.DEBUG,"splitLines":False,"pretty":False}Logger.init(LOGDIR,termSpecs=termSpecs)# Use ...logging.debug("You can remove all the fancy stuff:")logging.info("... keeping\n each message\n in its own line")logging.warning({"and":"flatten structures","like":list("lists")})
给您:
08-05-2018 21:19:40.688 11639:140599510849344 DEBUG console_customi 18 : You can remove all the fancy stuff:08-05-2018 21:19:40.689 11639:140599510849344 INFO console_customi 19 : ... keeping\n each message\n in its own line08-05-2018 21:19:40.689 11639:140599510849344 WARNING console_customi 20 : {'like': ['l', 'i', 's', 't', 's'], 'and': 'flatten structures'}
…初始化时使用:
termSpecs={"level":logging.DEBUG,"splitLines":True,"pretty":False}
给您:
08-05-2018 21:30:05.312 12648:140218785630016 ERROR console_customi 25 : However,
08-05-2018 21:30:05.312 12648:140218785630016 ERROR console_customi 25 : You can choose to split
08-05-2018 21:30:05.312 12648:140218785630016 ERROR console_customi 25 : lines08-05-2018 21:30:05.312 12648:140218785630016 CRITICAL console_customi 26 : ['but', 'not', 'prettify\nstructs']
当然,您可以禁用所有内容,回到默认值logging
。
唯一不同的是日志格式:
08-05-2018 21:30:05.312 12648:140218785630016 INFO console_customi 31 : Boooriiiing
最后,在init()
函数中,可以重写默认格式和日期
通过传递fmt
和datefmt
参数格式化。默认值为:
DATEFORMAT='%d-%m-%Y %H:%M:%S'LOGFORMAT='%(asctime)s.%(msecs)03d%(process)s:%(thread)u%(levelname)-8s%(module)15.15s%(lineno)-4s: %(message)s'
文件
如果您(开发人员)不是运行代码的人,那么
可能需要一个日志文件!如果您的应用程序是cli应用程序,则可能是最终用户
不应该看到所有的调试信息,而应该只看到警告和错误。Python
日志功能支持多个处理程序同时工作和lazylog
允许您轻松使用此功能。要定义文件记录器,请执行以下操作:
termSpecs = {"level": logging.DEBUG}
fileSpecs = [{"filename": LOGFILE, "level":logging.DEBUG}]
Logger.init(LOGDIR, termSpecs=termSpecs, fileSpecs=fileSpecs)
上面在LOGDIR/LOGFILE
中创建一个文件,默认设置为:
{'format':'console''backupCount':20'maxBytes':10000000# 10MB'color':False,'splitLines':True,'pretty':False}
backupCount
:是我们保存的文件数format
:控制正在使用哪个日志格式化程序。默认情况下 使用颜色格式化程序,因此选项color
、splitLines
和pretty
也受支持。其他值包括:default
和json
稍后我们将看到
上述设置将在文件中生成以下输出:
08-05-2018 15:57:24.118 16142:140509479982912 DEBUG logfile 23 : ^---same as console, this is an example log line 08-05-2018 15:57:24.118 16142:140509479982912 INFO logfile 24 : This logger handles 08-05-2018 15:57:24.118 16142:140509479982912 INFO logfile 24 : New 08-05-2018 15:57:24.118 16142:140509479982912 INFO logfile 24 : Lines 08-05-2018 15:57:24.119 16142:140509479982912 WARNING logfile 25 : {'but': 'Flattens structs by default'} 08-05-2018 15:57:24.119 16142:140509479982912 ERROR logfile 26 : Errors DONT stick out - color is not used
使用splitLines: False
可以获得:
# Code: # logging.info("Like console\nYou can avoid\nsplitting lines") 08-05-2018 15:57:24.119 16142:140509479982912 INFO logfile 42 : Like console\nYou can avoid\nsplitting lines
当使用pretty: True
时,您将得到:
# Code: # logging.info({"or": "enable prettifying!"}) 08-05-2018 15:57:24.120 16142:140509479982912 INFO logfile 55 : (dict) { 08-05-2018 15:57:24.120 16142:140509479982912 INFO logfile 55 : 'or': 'enable prettifying!' 08-05-2018 15:57:24.120 16142:140509479982912 INFO logfile 55 : }
json格式
当我们需要将日志索引到数据库或
流式传输,通常用于机器到机器的通信。目前,
lazylog
不支持终端上的json日志记录,但确实支持它
对于文件。要启用它,请使用以下命令初始化:
fileSpecs=[{"filename":LOGFILE,"level":logging.DEBUG,"format":"json"}]
支持以下日志记录方式:
logging.info("Simple str message")logging.warning("Message with metadata",extra={"user":"nwj12"})logging.debug({"what":"dict-based logging"},extra={"user":"asd32"})logging.info(["anything","json","serializable","see OBJECT"],extra={"foo":"bar"})
结果将是(日志文件中的每一行):
{"filename":"logfile.py","module":"logfile","timestamp":1525799704.8904743,"message":"Simple str message","thread":140193498228544,"levelname":"INFO","process":27529}{"filename":"logfile.py","user":"nwj12","module":"logfile","timestamp":1525799704.890644,"message":"Message with metadata","thread":140193498228544,"levelname":"WARNING","process":27529}{"filename":"logfile.py","user":"asd32","module":"logfile","what":"dict-based logging","timestamp":1525799704.8907733,"message":"","process":27529,"levelname":"DEBUG","thread":140193498228544}{"filename":"logfile.py","timestamp":1525799704.8909438,"module":"logfile","thread":140193498228544,"foo":"bar","message":"","process":27529,"levelname":"INFO","object":["anything","json","serializable","see OBJECT"]}
鳍ally,一个可以有多个不同格式和日志级别的日志文件。
这可以在初始化状态下完成,也可以稍后使用addFileLogger
方法:
# On init:fileSpecs=[{"filename":LOGFILE,"level":logging.DEBUG,"format":"json"},{"filename":LOGFILE2,"level":logging.INFO}]Logger.init(LOGDIR,termSpecs=termSpecs,fileSpecs=fileSpecs)# Later-on:fileSpecs2={"filename":LOGFILE2,"level":logging.INFO}Logger.addFileLogger(fileSpecs2)
默认格式
我真的不建议…但你会得到
# Code: # logging.info("You\n can set the \n format to\n default") # logging.warning("But I don't like it...") 08-05-2018 15:57:24.120 16142:140509479982912 INFO logfile 70 : You can set the format to default 08-05-2018 15:57:24.120 16142:140509479982912 WARNING logfile 71 : But I don't like it...
致谢
这个项目经过相当长一段时间的零碎代码拼凑而成 时间(和“按要求”)。我重写了很多零件,整理和包装 以可重复使用的形式。但是,很多其他人的代码都包含在 您将在源代码中的注释中看到,如果适用,将给予信用。 仅提及一些(对于那些不阅读源代码的人):
- 合并指令:https://stackoverflow.com/a/20666342/3727050
- json文件日志:https://github.com/madzak/python-json-logger
- 美化结构:http://stackoverflow.com/questions/3229419/pretty-printing-nested-dictionaries-in-python