官方文档:https://docs.python.org/2/library/logging.html
logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有
filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger; 默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。
只打印到控制台
# coding:utf-8
import logging
import sys
# 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
LOG_FORMAT = "%(asctime)s File "%(filename)s",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename):
#logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
#formatter = logging.Formatter(LOG_FORMAT);
#handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('').addHandler(handler);
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
#console = logging.StreamHandler();
#console.setLevel(logging.INFO);
#console.setFormatter(formatter);
#logging.getLogger('').addHandler(console);
initLogging("mylog.txt")
logging.info("Hello world")
只打印到文件
# coding:utf-8
import logging
import sys
# 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
LOG_FORMAT = "%(asctime)s File "%(filename)s",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename):
logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
#formatter = logging.Formatter(LOG_FORMAT);
#handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('').addHandler(handler);
#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
#console = logging.StreamHandler();
#console.setLevel(logging.INFO);
#console.setFormatter(formatter);
#logging.getLogger('').addHandler(console);
initLogging("mylog.txt")
logging.info("Hello world")
同时打印到控制台和文件
# coding:utf-8
import logging
import sys
# 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
LOG_FORMAT = "%(asctime)s File "%(filename)s",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename):
logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
formatter = logging.Formatter(LOG_FORMAT);
#handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('').addHandler(handler);
#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
console = logging.StreamHandler();
console.setLevel(logging.INFO);
console.setFormatter(formatter);
logging.getLogger('').addHandler(console);
initLogging("mylog.txt")
logging.info("Hello world")