python 日志使用(基础版)

即便是做一个很小的程序,日志模块也是很有必要的,因为只用print输出的东西太混乱 了,用日志就可以很好的分类,以及记录统一的时间等。

日志的级别:

级别使用情景
DEBUG详细信息,用于调试
INFO确认程序正常执行
WARNING程序仍在执行,但是发生了隐含的不不希望发生的事情,可能会导致未来的错误
ERROR程序某些功能可能未执行成功
CRITICAL严重错误,导致程序无法继续运行

默认的级别是WARNING

简单例子

import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
结果:

WARNING:root:Watch out!

输出到文件

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

命令行可以输入以下参数进行设置级别

--log=INFO

多个模块输出日志

# myapp.py
import logging
import mylib

def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)
    logging.info('Started')
    mylib.do_something()
    logging.info('Finished')

if __name__ == '__main__':
    main()

# mylib.py
import logging

def do_something():
    logging.info('Doing something')

日志中加变量

import logging
logging.warning('%s before you %s', 'Look', 'leap!')

日志格式的设置

import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')
结果:

DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too

加入时间

import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')

日志的可用属性

Attribute name Format Description
args You shouldn’t need to format this yourself. The tuple of arguments merged into msg to produce message.
asctime %(asctime)s Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).
created %(created)f Time when the LogRecord was created (as returned by time.time()).
exc_info You shouldn’t need to format this yourself. Exception tuple (à la sys.exc_info) or, if no exception has occurred, None.
filename %(filename)s Filename portion of pathname.
funcName %(funcName)s Name of function containing the logging call.
levelname %(levelname)s Text logging level for the message ('DEBUG''INFO''WARNING','ERROR''CRITICAL').
levelno %(levelno)s Numeric logging level for the message (DEBUGINFOWARNING,ERRORCRITICAL).
lineno %(lineno)d Source line number where the logging call was issued (if available).
module %(module)s Module (name portion of filename).
msecs %(msecs)d Millisecond portion of the time when the LogRecord was created.
message %(message)s The logged message, computed as msg % args. This is set whenFormatter.format() is invoked.
msg You shouldn’t need to format this yourself. The format string passed in the original logging call. Merged with args to produce message, or an arbitrary object (see Using arbitrary objects as messages).
name %(name)s Name of the logger used to log the call.
pathname %(pathname)s Full pathname of the source file where the logging call was issued (if available).
process %(process)d Process ID (if available).
processName %(processName)s Process name (if available).
relativeCreated %(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
thread %(thread)d Thread ID (if available).
threadName %(threadName)s Thread name (if available).





参考:

http://docs.python.org/2/howto/logging.html#logging-basic-tutorial

http://www.crifan.com/summary_python_logging_module_usage/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

day walker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值