import logging
from logging.handlers import SMTPHandler
from logging import StreamHandler
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%F %H:%M:%S',
filename='myapp.log',
filemode='w')
#################################################################################################
#定义一个SMTPHandler,将ERROR级别或更高的日志信息发送到指定邮箱,并将其添加到当前的日志处理对象#
handler = SMTPHandler(('smtp.163.com',25), 'liujun3420@163.com', ['liujun3420@163.com'], 'Test SMTPHandler', ('liujun3420', '****'))
handler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s(%(msecs)-3d)|%(process)-5d [%(module)s:%(lineno)s] %(levelname)-8s: %(message)s',datefmt='%F %X')
handler.setFormatter(formatter)
logging.getLogger('').addHandler(handler)
#定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#
console = StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s(%(msecs)-3d)|%(process)-5d [%(module)s:%(lineno)s] %(levelname)-8s: %(message)s',datefmt='%F %X')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
#################################################################################################
logging.debug('This is debug message')
logging.info('This is info message')
logging.critical('This is critical message')
# logging.StreamHandler: 日志输出到流,可以是sys.stderr、sys.stdout或者文件
# logging.FileHandler: 日志输出到文件
# 日志回滚方式,实际使用时用RotatingFileHandler和TimedRotatingFileHandler
# logging.handlers.BaseRotatingHandler
# logging.handlers.RotatingFileHandler
# logging.handlers.TimedRotatingFileHandler
# logging.handlers.SocketHandler: 远程输出日志到TCP/IP sockets
# logging.handlers.DatagramHandler: 远程输出日志到UDP sockets
# logging.handlers.SMTPHandler: 远程输出日志到邮件地址
# logging.handlers.SysLogHandler: 日志输出到syslog
# logging.handlers.NTEventLogHandler: 远程输出日志到Windows NT/2000/XP的事件日志
# logging.handlers.MemoryHandler: 日志输出到内存中的制定buffer
# logging.handlers.HTTPHandler: 通过"GET"或"POST"远程输出到HTTP服务器
网友评论