Python日志logging

logging

用于便捷记录日志且线程安全的模块

1、单文件日志

import logging
  
  
logging.basicConfig(filename='log.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=10)
  
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')
logging.log(10,'log')

封装后:
import logging
import logging.handlers

class LogFactory(object):
def __init__(self, filename):
self.filename = filename
self.log = logging.getLogger(self.filename)
self.log.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(self.filename, maxBytes=100*1024*1024, backupCount=1000)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(threadName)s [%(module)s.%(funcName)s, Line:%(lineno)d] %(message)s')
handler.setFormatter(formatter)
self.log.addHandler(handler)
# self.log.error(msg)

def msg(self, msg):
self.log.info(msg)






if __name__ == '__main__':

log = LogFactory("sm_article.log")

try:
log.msg("Begin to load articles and topics...")
log.msg('HHHHHHHHHHH')

except Exception as e:
log.msg("Load catch error:%s" % e)
import logging

# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
屏幕输出的错误信息

 

日志等级:

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

注:只有【当前写等级】大于【日志等级】时,日志文件才被记录。

日志记录格式:

2、多文件日志

对于上述记录日志的功能,只能将日志记录在单文件中,如果想要设置多个日志文件,logging.basicConfig将无法完成,需要自定义文件和日志操作对象。

# 定义文件
file_1_1 = logging.FileHandler('l1_1.log', 'a')
fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")
file_1_1.setFormatter(fmt)

file_1_2 = logging.FileHandler('l1_2.log', 'a')
fmt = logging.Formatter()
file_1_2.setFormatter(fmt)

# 定义日志
logger1 = logging.Logger('s1', level=logging.ERROR)
logger1.addHandler(file_1_1)
logger1.addHandler(file_1_2)


# 写日志
logger1.critical('1111')
日志一
# 定义文件
file_2_1 = logging.FileHandler('l2_1.log', 'a')
fmt = logging.Formatter()
file_2_1.setFormatter(fmt)

# 定义日志
logger2 = logging.Logger('s2', level=logging.INFO)
logger2.addHandler(file_2_1)
日志二

少了一句:

logger2.warning('warning')

如上述创建的两个日志对象

  • 当使用【logger1】写日志时,会将相应的内容写入 l1_1.log 和 l1_2.log 文件中
  • 当使用【logger2】写日志时,会将相应的内容写入 l2_1.log 文件中

根据时间进行日志切割

import logging
import os
import logging.handlers


class Logger(logging.Logger):
    """
    # my_log = Logger()
    #
    # # 输出日志
    # # log.info("日志模块消息!")
    # # log.debug("日志模块调试消息!")
    # my_log.error("日志模块错误消息!")
    """

    def __init__(self, filename=None):
        super(Logger, self).__init__(self)
        # 日志文件名
        if filename is None:
            filename = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "zk_css.log")
        self.filename = filename

        # 创建一个handler,用于写入日志文件 (每天生成1个,保留30天的日志)
        # fh = logging.handlers.TimedRotatingFileHandler(self.filename, 'D', 1, 5)
        fh = logging.handlers.WatchedFileHandler(self.filename)
        fh.suffix = "%Y%m%d-%H%M.log"
        fh.setLevel(logging.DEBUG)

        # 定义handler的输出格式
        formatter = logging.Formatter(
            '[%(asctime)s] - %(filename)s [Line:%(lineno)d] - [%(levelname)s]-[thread:%(thread)s]-[process:%(process)s] - [%(message)s]')
        fh.setFormatter(formatter)

        # 给logger添加handler
        self.addHandler(fh)


try:
    logpath = Config().get_content("log")["logpath"]
except Exception as e:
    logpath = ""
if os.path.exists(logpath):
    my_log = Logger(filename=logpath)
else:
    my_log = Logger()


my_log.error("adsfadf")

 

 

错误日志发邮件

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import logging, logging.handlers


class EncodingFormatter(logging.Formatter):
def __init__(self, fmt, datefmt=None, encoding=None):
logging.Formatter.__init__(self, fmt, datefmt)
self.encoding = encoding



errlog = logging.getLogger()
sh = logging.handlers.SMTPHandler("mail host (smtp host)",
'谁发的',
'发给谁',
"标题",
credentials=('用户名', '密码'),
secure=()
)
errlog.addHandler(sh)
sh.setFormatter(EncodingFormatter('%(message)s', encoding='utf-8'))

errlog.error(u'你收到邮件了吗?')

 

 

 

 

总结

 

好吧,我承认我懒得二次更改了,发现自己的也不行,网上别人写的也不是我所需要的,而且千篇一律

最好无奈去看了官方文档,ok懂了,

然,最后总结了方法放在了github上面:https://github.com/renfanzi/Python-Tornado-Template

 

posted @ 2016-07-05 16:55  我当道士那儿些年  阅读(733)  评论(0编辑  收藏  举报