使用logging.basicConfig
进行配置
#!/usr/local/bin/python# -*- Coding:utf-8 -*-import logging # 通过下面的方式进行简单配置输出方式与日志级别logging.basicConfig(filename='logger.log', level=logging.INFO) logging.deBUG('deBUG message')logging.info('info message')logging.warning('warn message')logging.error('error message')logging.critical('critical message')
代码创建了一个logger.log文件,文件中写入如下内容
INFO:root:info messageWARNING:root:warn messageERROR:root:error messageCRITICAL:root:critical message
如果多次运行该函数,log文件会持续追加
basicConfig
可以包含有如下参数,下面是官方文档
Do basic configuration for the logging system. This function does nothing if the root logger already has handlers configured, unless the keyword argument *force* is set to ``True``. It is a convenIEnce method intended for use by simple scripts to do one-shot configuration of the logging package. The default behavIoUr is to create a StreamHandler which writes to sys.stderr, set a formatter using the BASIC_FORMAT format string, and add the handler to the root logger. A number of optional keyword arguments may be specifIEd, which can alter the default behavIoUr. filename SpecifIEs that a fileHandler be created, using the specifIEd filename, rather than a StreamHandler. filemode SpecifIEs the mode to open the file, if filename is specifIEd (if filemode is unspecifIEd, it defaults to 'a'). format Use the specifIEd format string for the handler. datefmt Use the specifIEd date/time format. style If a format string is specifIEd, use this to specify the type of format string (possible values '%', '{', '$', for %-formatting, :meth:`str.format` and :class:`string.Template` - defaults to '%'). level Set the root logger level to the specifIEd level. stream Use the specifIEd stream to initialize the StreamHandler. Note that this argument is incompatible with 'filename' - if both are present, 'stream' is ignored. handlers If specifIEd, this should be an iterable of already created handlers, which will be added to the root handler. Any handler in the List which does not have a formatter assigned will be assigned the formatter created in this function. force If this keyword is specifIEd as true, any existing handlers attached to the root logger are removed and closed, before carrying out the configuration as specifIEd by the other arguments. Note that you Could specify a stream created using open(filename, mode) rather than passing the filename and mode in. However, it should be remembered that StreamHandler does not close its stream (since it may be using sys.stdout or sys.stderr), whereas fileHandler closes its stream when the handler is closed.
2. 使用fileHandler和Formatter进行配置步骤如下
创建logger对象创建fileHandler和Formatter对象使用fileHandler和Formatter对logger进行配置示例代码如下:
# -*- enCoding:utf-8 -*-import logging# create loggerlogger_name = "example"logger = logging.getLogger(logger_name)logger.setLevel(logging.DEBUG)# create file handlerlog_path = "./log.log"fh = logging.fileHandler(log_path)fh.setLevel(logging.WARN)# create formatterfmt = "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s"datefmt = "%a %d %b %Y %H:%M:%s"formatter = logging.Formatter(fmt, datefmt)# add handler and formatter to loggerfh.setFormatter(formatter)logger.addHandler(fh)# print log infologger.deBUG('deBUG message')logger.info('info message')logger.warning('warn message')logger.error('error message')logger.critical('critical message')
注意: %(levelname)s
是python风格的格式化字符串,参考文章python格式化字符串
以上是内存溢出为你收集整理的Python logging的使用全部内容,希望文章能够帮你解决Python logging的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)