即便是做一个很小的程序,日志模块也是很有必要的,因为只用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 (DEBUG, INFO, WARNING,ERROR, CRITICAL). |
| 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
本文介绍了Python的日志模块及其使用方法,包括不同级别的日志输出、日志格式的设置、如何将日志输出到文件以及如何在多个模块中使用日志。通过实例展示了如何在实际项目中应用这些技巧。
157

被折叠的 条评论
为什么被折叠?



