python 日志使用(基础版)

来源:互联网 发布:景观建模软件 编辑:程序博客网 时间:2024/04/30 00:21

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

日志的级别:

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

默认的级别是WARNING

简单例子

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

WARNING:root:Watch out!

输出到文件

import logginglogging.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.pyimport loggingimport mylibdef main():    logging.basicConfig(filename='myapp.log', level=logging.INFO)    logging.info('Started')    mylib.do_something()    logging.info('Finished')if __name__ == '__main__':    main()

# mylib.pyimport loggingdef do_something():    logging.info('Doing something')

日志中加变量

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

日志格式的设置

import logginglogging.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 consoleINFO:So should thisWARNING:And this, too

加入时间

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

日志的可用属性

Attribute nameFormatDescriptionargsYou shouldn’t need to format this yourself.The tuple of arguments merged into msg to produce message.asctime%(asctime)sHuman-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)fTime when the LogRecord was created (as returned by time.time()).exc_infoYou shouldn’t need to format this yourself.Exception tuple (à la sys.exc_info) or, if no exception has occurred, None.filename%(filename)sFilename portion of pathname.funcName%(funcName)sName of function containing the logging call.levelname%(levelname)sText logging level for the message ('DEBUG''INFO''WARNING','ERROR''CRITICAL').levelno%(levelno)sNumeric logging level for the message (DEBUGINFOWARNING,ERRORCRITICAL).lineno%(lineno)dSource line number where the logging call was issued (if available).module%(module)sModule (name portion of filename).msecs%(msecs)dMillisecond portion of the time when the LogRecord was created.message%(message)sThe logged message, computed as msg % args. This is set whenFormatter.format() is invoked.msgYou 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)sName of the logger used to log the call.pathname%(pathname)sFull pathname of the source file where the logging call was issued (if available).process%(process)dProcess ID (if available).processName%(processName)sProcess name (if available).relativeCreated%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.thread%(thread)dThread ID (if available).threadName%(threadName)sThread name (if available).




参考:

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

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

原创粉丝点击