一个简单的JSON日志记录
plain-json-logging的Python项目详细描述
普通json日志记录是一个简单的json数据日志记录。在
安装:
$ pip install plain-json-logging
用法:
fromplain_json_loggingimportPlainJsonLogginglogging=newPlainJsonLogging()logging.error('this is error.)logging.warn('this is warn.')logging.info('foo').info('bar').info('baz')
结果如下所示:
^{pr2}$选项:
fromplain_json_loggingimportPlainJsonLogginglogging=newPlainJsonLogging(file=sys.stdout,strftime='%Y-%m-%d %H:%M:%S.%f%z',timezone='America/Chicago',timedelta=+540,# 9 hourstimestampname='@timestamp',messagename='@message',levelname='lev',levelinfo=0,levelwarn=1,levelerror=2,constextra={name:'Example'})
以下参数可以修改日志记录的行为方式:
file: | the writable file object. Default: sys.stderr. |
---|---|
strftime: | the format. Defalut: datetime.datetime default. |
timezone: | the TZ database name. Default: UTC. |
timedelta: | the timedelta. The minute is a unit. Default: 0. |
timestampname: | the name for timestamp. Default: timestamp. |
messagename: | the name for message. Default: message. |
levelname: | the name for level. Default: level. |
levelinfo: | the value for the level info. Default: INFO. |
levelwarn: | the value for the level warn. Default: WARN. |
levelerror: | the value for the level error. Default: ERROR. |
constextra: | the default additional items for each log (the value must be a dictionary object): Default: None. |
API标准:
PlainJsonLogging有以下三种方法:
- 信息
- 警告
- 错误
所有方法都返回PlainJsonLogging实例本身。 因此,方法链接可用于日志记录:
fromplain_json_loggingimportPlainJsonLogginglogging=newPlainJsonLogging()logging.info('foo').info('bar').info('baz')
额外有效载荷:
所有方法都可以接收额外的有效载荷。在
fromplain_json_loggingimportPlainJsonLogginglogging=newPlainJsonLogging(file=sys.stdout,strftime='%Y-%m-%d %H:%M:%S.%f%z',timezone='Asia/Tokyo',timedelta=0,timestampname='@timestamp',messagename='@message',levelname='lev',levelinfo=0,levelwarn=1,levelerror=2,constextra={name:'Example'},)logging.info('this is info.',{'infoData':'this is a extra payload for info.'})logging.warn('this is warn.',{'warnData':'this is a extra payload for warn.'})logging.error('this is error.',{'errData':'this is a extra payload for error.'})
结果如下所示:
{"@timestamp":"2020-01-13 07:17:06.370000+0900","lev":0,"@message":"this is info.","name":"Example","infoData":"this is a extra payload for info."}{"@timestamp":"2020-01-13 07:17:06.370000+0900","lev":1,"@message":"this is warn.","name":"Example","warnData":"this is a extra payload for warn."}{"@timestamp":"2020-01-13 07:17:06.370000+0900","lev":2,"@message":"this is error.","name":"Example","errData":"this is a extra payload for error."}
- 项目
标签: