pyhon模块之日志模块

#Auther Bob
#--*--coding:utf-8--*--

import logging

#python一共有5个级别的日志,debug、info、warning、error、critical,其中用下面的方式,debug和info的日志不会打印出来
# logging.debug('time is over')
# logging.info('password is error')
# logging.warning('you have 3 times')
# logging.error('you have no time')
# logging.critical('server is down')

# ==================================================================================================================




#输出最简单的日志到文件中,这里的还不带任何格式,下面这段代码的意思是,输出日志到log1.txt中,但是是打印info级别以上的日志
# logging.basicConfig(filename='log1.txt',level=logging.INFO)
# logging.debug('time is over')
# logging.info('password is error')
# logging.warning('you have 3 times')
# logging.error('you have no time')
# logging.critical('server is down')


#查看log1.txt中的内容,只有info及info以上的日志
# INFO:root:password is error
# WARNING:root:you have 3 times
# ERROR:root:you have no time
# CRITICAL:root:server is down


# ===============================================================================================================



#下面我们在丰富一下上面的例子,可以修改和优化日志打印的格式,format中的参数asctime是时间,message就是日志的内容

# logging.basicConfig(filename='log2.txt',level=logging.INFO,format='%(asctime)s,%(message)s',datefmt='%m/%d/%Y:%H:%M:%S %p')
# logging.debug('time is over')
# logging.info('password is error')
# logging.warning('you have 3 times')
# logging.error('you have no time')
# logging.critical('server is down')

#我们来看下log2.txt中的内容
# 04/11/2017:10:01:37 AM,password is error
# 04/11/2017:10:01:37 AM,you have 3 times
# 04/11/2017:10:01:37 AM,you have no time
# 04/11/2017:10:01:37 AM,server is down


# ======================================================================================================

#上面要么就输出到屏幕上,要么就输出到文件中,如果我们想同时输出到屏幕和文件中,该怎么办呢?

#1,先注册一个全局的日志对象
log_obg = logging.getLogger('root')
log_obg.setLevel(logging.WARNING)

#2,创建一个打印到屏幕的日志对象,和该日志对象的级别,如果该级别低于全局的级别,则全局的级别生效,如果该级别高于全局的级别
#则该级别生效
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)

#3,创建一个打印到文件的日志对象,和该日志对象的级别,如果该级别低于全局的级别,则全局的级别生效,如果该级别高于全局的级别
#则该级别生效
fh = logging.FileHandler('log3.txt')
fh.setLevel(logging.WARNING)

#3,创建一个日志的格式的对象
format_obg = logging.Formatter('%(asctime)s -%(name)s -%(levelname)s -%(message)s')

# %(name)s 这里就是logging.getLogger('root')的'root'
# %(levelno)s 这里就是日志的级别DEBUG, INFO,WARNING, ERROR, CRITICAL

# %(levelname)s
# %(pathname)s 这里就是文件的路径名
# %(filename)s 这里就是文件的名称
# %(module)s 模块的名称
# %(lineno)d 行数
# %(funcName)s 函数的名称
# %(created)f 日志记录创建时间
# %(asctime)s 时间
# %(msecs)d 秒
# %(thread)d 线程id
# %(threadName)s 线程名称
# %(process)d 进程id
# %(message)s 日志内容


#4、对打印到屏幕的日志设定格式
ch.setFormatter(format_obg)

#5、对打印到文件的日志设定格式
fh.setFormatter(format_obg)


#在全局的日志对象中注册打印到屏幕的日志对象
log_obg.addHandler(ch)

#在全局的日志对象中注册打印到文件的日志对象
log_obg.addHandler(fh)


# log_obg.debug('time is over')
# log_obg.info('password is error')
# log_obg.warning('you have 3 times')
# log_obg.error('you have no time')
# log_obg.critical('server is down')


#输出到屏幕的日志
# 2017-04-11 10:25:03,241 -root -ERROR -you have no time
# 2017-04-11 10:25:03,241 -root -CRITICAL -server is down


#输出到文件的日志
# 2017-04-11 10:25:03,241 -root -WARNING -you have 3 times
# 2017-04-11 10:25:03,241 -root -ERROR -you have no time
# 2017-04-11 10:25:03,241 -root -CRITICAL -server is down


#Auther Bob
#--*--coding:utf-8--*--
import logging



#z注册一个全局的日志对象
log_obj = logging.getLogger("root")
log_obj.setLevel(logging.DEBUG)



#注册一个打印到屏幕上的日志对象
sh = logging.StreamHandler()
sh.setLevel(logging.ERROR)

#注册一个打印到文件的日志对象
fh = logging.FileHandler("db.txt")
fh.setLevel(logging.ERROR)


#设定日志打印的格式
log_format = logging.Formatter("%(name)s-%(message)s-%(levelno)s-%(thread)d-%(threadName)s-%(process)d-%(asctime)s",datefmt='%m/%d/%Y:%H:%M:%S %p')
# log_format = logging.Formatter("%(name)s-%(message)s-%(levelno)s-%(thread)d-%(threadName)s-%(process)d-%(asctime)s")

#在打印到屏幕的日志对象中应用日志格式
fh.setFormatter(log_format)

#在打印到文件的日志对象中应用日志格式
sh.setFormatter(log_format)



#将打印到屏幕和日志的对象注册到全局的日志对象中
log_obj.addHandler(fh)
log_obj.addHandler(sh)

while True:
    log_obj.debug('time is over')
    log_obj.info('password is error')
    log_obj.warning('you have 3 times')
    log_obj.error('you have no time')
    log_obj.critical('server is down')

  

原文地址:https://www.cnblogs.com/bainianminguo/p/6687032.html