一个简单的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."}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java中的第10个解决方案   Java中泛型的super和extends组合   Java Android OpenGL ES 2.0片段着色器texture2D不工作   在JList elementJava上按下时打开弹出窗口   字典Java数据结构允许对象上的布尔标志和排序?   java我该如何停止为AWS关系数据库服务收费?   java如何检查Google定位准确度是否提高?   java使用Mockito将方法存根在与被测类相同的类中(CUT)   java IntelliJIdea disable inspection:参数的实际值始终为   java CXF+多态POST数据   java命名空间“ads”未绑定   Sun(1999)的“Java编程语言的代码约定”的编码风格过时了吗?   JAVA中的隐式接口、类转换   javafx如何配置Java登录应用程序代码   java如何在滚动窗格中设置选项卡以及如何显示选项卡标题   java中等式和条件运算符的优先级   java Spring WS无法返回JAXB响应   错误响应上的java jaxws处理程序行为   java Hibernate HQL映射查询