pylogrus是python的结构化记录器
pylogrus的Python项目详细描述
pylogrus是python的结构化记录器,它的灵感来自logrus 高朗图书馆https://github.com/sirupsen/logrus。幽门螺旋延伸 内置logging模块,能够生成彩色记录 日志和json格式的记录。
功能
使用此软件包,您将能够:
- 在控制台中对输出进行着色(文本格式)
- 关闭着色(文本格式)
- 在日志记录中添加额外字段
- 在日志记录中添加永久额外字段
- 为消息添加永久前缀
- 创建新的上下文实例
- 以json格式保存日志记录
- 重写日志级别的名称
- 覆盖基本元素的颜色(文本格式)
- 重写键的名称(json格式)
- 仅在记录中定义所需字段(JSON格式)
- 以zulu格式创建记录的时间
初始化
importloggingfrompylogrusimportPyLogruslogging.setLoggerClass(PyLogrus)
格式化程序
文本格式化程序
textformatter类允许通过设置 colorize参数。可关闭着色。记录时间 记录可以用zulu格式设置。只需将datefmt参数设置为'z'。
importloggingfrompylogrusimportPyLogrus,TextFormatterlogging.setLoggerClass(PyLogrus)logger=logging.getLogger(__name__)# type: PyLogruslogger.setLevel(logging.DEBUG)formatter=TextFormatter(datefmt='Z',colorize=True)ch=logging.StreamHandler()ch.setLevel(logging.DEBUG)ch.setFormatter(formatter)logger.addHandler(ch)
覆盖级别名称
您只能定义必要的更改:
formatter=TextFormatter(datefmt='Z',colorize=True)formatter.override_level_names({'CRITICAL':'CRIT','ERROR':'ERRO','WARNING':'WARN','DEBUG':'DEBU'})
或者例如:
formatter.override_level_names({'CRITICAL':'FATAL'})
覆盖颜色
textformatter有几个可以着色的基元素。你可以 通过color属性获取它们:
print(formatter.color) ... { 'asctime': '\x1b[2;37m', # time of log record 'prefix': '\x1b[0;36m', # message prefix 'field': '\x1b[0;32m', # key of extra field 'value': '\x1b[0m', # value of extra field 'debug': '\x1b[0;34m', 'info': '\x1b[0;32m', 'warning': '\x1b[0;33m', 'error': '\x1b[0;31m', 'critical': '\x1b[2;31m' }
元素的颜色可以使用cl_ux常量更改。你可以 仅为需要的元素定义新颜色。
frompylogrusimportPyLogrus,TextFormatter,CL_BLDYLW...formatter=TextFormatter(colorize=True)formatter.override_colors({'prefix':CL_BLDYLW})
jsonformatter
jsonformatter类允许以json格式保存日志记录。 在课程初始化期间,您可以:
- 以zulu格式设置日志记录的时间。只需设置datefmt参数 作为“Z”。
- 定义将出现在日志记录中的启用字段列表 通过enabled_fields参数。启用字段由 原始字段名或包含原始名称的元组 以及新的名字。新名称将覆盖 输出。
- 要在控制台中漂亮地打印json日志记录,请设置indent 和sort_keys参数(可选)。
importloggingfrompylogrusimportPyLogrus,JsonFormatterlogging.setLoggerClass(PyLogrus)logger=logging.getLogger(__name__)# type: PyLogruslogger.setLevel(logging.DEBUG)enabled_fields=[('name','logger_name'),('asctime','service_timestamp'),('levelname','level'),('threadName','thread_name'),'message',('exception','exception_class'),('stacktrace','stack_trace'),'module',('funcName','function')]formatter=JsonFormatter(datefmt='Z',enabled_fields=enabled_fields,indent=2,sort_keys=True)ch=logging.StreamHandler()ch.setLevel(logging.DEBUG)ch.setFormatter(formatter)logger.addHandler(ch)
覆盖级别名称
可以用与使用 文本格式化程序。
formatter=JsonFormatter()formatter.override_level_names({'WARNING':'WARN'})
用法
请参阅examples目录中的用法示例。
照常记录消息:
importloggingfrompylogrusimportPyLogrus,TextFormatterdefget_logger():logging.setLoggerClass(PyLogrus)...formatter=TextFormatter()...returnloggerlog=get_logger()log.debug("Using base logger")
带有额外字段的日志消息:
log.withFields({'user':'John Doe'}).debug("Message with an extra field")
在logger中添加永久字段并获取上下文实例:
log_ctx=log.withFields({'context':1})log_ctx.info("Add permanent field into current logger")
为当前记录器实例的消息添加永久前缀:
log_ctx=log_ctx.withPrefix("[API]")log_ctx.info("Add prefix as a permanent part of a message")
带有额外字段的日志消息:
log_ctx.withFields({'user':'Admin','transaction_id':str(uuid.uuid4())}).warning("Message with prefix and extra fields")