[译]logging.basicConfig()

这个模块为应用与库定义了实现灵活的事件日志系统的函数与类.

使用标准库提提供的 logging API 最主要的好处是,所有的 Python 模块都可能参与日志输出(logging),包括你的日志消息和第三方模块的日志消息。

这个模块提供许多强大而灵活的功能。如果你对 logging 不太熟悉的话, 掌握它最好的方式就是查看它对应的教程

该模块定义的基础类和函数都列在下面。

  1. Loggers 记录器暴露了应用程序代码直接使用的接口。
  2. Handlers 处理程序将日志记录(由记录器创建)发送到适当的目标。
  3. Filters 过滤器提供了更精细的设施,用于确定要输出的日志记录。
  4. Formatters 格式化程序指定最终输出中日志记录的样式。
  • logging.basicConfig(**kwargs)函数

为logging系统设置基础配置,通过创建一个含有默认[Formatter](https://docs.python.org/3.7/library/logging.html的 StreamHandler#logging.Formatter)并将其降入根logger。

如果root logger并未定义特殊handler,debug(), info(), warning(), error() and critical() 会自动调用 basicConfig()

如果root logger已经配置handler,那此函数不执行任何操作。

  • 常用参数

FormatDescription
filename指定文件名
filemode指定文件打开方式,‘w’或’a’, Defaults to 'a'.
format指定输出的格式和内容。
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
datefmt指定时间格式
styleIf format is specified, use this style for the format string. One of '%', '{' or '$'for printf-style, str.format() or string.Template respectively. Defaults to '%'.
level指定日志级别。
logging.WARNING 默认
logging.INFO
streamUse the specified stream to initialize the StreamHandler. Note that this argument is incompatible with filename - if both are present, a ValueError is raised.
handlersIf specified, this should be an iterable of already created handlers to add to the root logger. Any handlers which don’t already have a formatter set will be assigned the default formatter created in this function. Note that this argument is incompatible with filename or stream - if both are present, a ValueError is raised.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值