import logging # 引入logging模块
import os.path
import time
log = logging.getLogger()
fh = logging.FileHandler('www.log')
ch = logging.StreamHandler()
format = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] '
'- %(levelname)s: %(message)s')
fh.setFormatter(format)
ch.setFormatter(format)
log.addHandler(fh)
log.addHandler(ch)
log.setLevel(logging.DEBUG)
log.debug('this is a logger debug message')
log.info('this is a logger info message')
log.warning('this is a logger warning message')
log.error('this is a logger error message')
log.critical('this is a logger critical message')
# 将信息打印到控制台上
# logging.debug(u"苍井空")
# logging.info(u"麻生希")
# logging.warning(u"小泽玛利亚")
# logging.error(u"桃谷绘里香")
# logging.critical(u"泷泽萝拉")
# logging.basicConfig(filename='test.log',filemode='w',level=logging.DEBUG,
# format=' %(levelname)s %(asctime)s %(message)s', datefmt='%m-%d-%Y %I:%M:%S %p')
# logging.debug('debuggggggggggggg')
# logging.info('infooooooooooooooooo')
# logging.warning('warninggggggggggggggg')
# logging.error('errorrrrrrrrrrrrr')
# logging.critical('criticalllllllllllll')
# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO) # Log等级总开关
# 第二步,创建一个handler,用于写入日志文件
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd())
log_name = 'test.log'
print(log_name)
logfile = log_name
fh = logging.FileHandler(logfile, mode='a')
fh.setLevel(logging.DEBUG) # 输出到file的log等级的开关
# 第三步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)
# 日志
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')