# -*- coding:utf-8 -*-
from logging.handlers import *
import logging
import logging.handlers
class MySocketHandler(SocketHandler):
def makePickle(self, record):
return self.format(record) + '
'
class MyDatagramHandler(DatagramHandler):
def makePickle(self, record):
return self.format(record) + '
'
class MyLog:
#key为日志里的关键字
def __init__(self,key):
LOG_FILE = key +'.log'
# handler1 = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1024 * 1024, backupCount=5)
# handler = MyDatagramHandler('192.168.x.xxx', 5000) # 实例化handler
handler3 = logging.handlers.SysLogHandler(('127.0.0.1', 514), logging.handlers.SysLogHandler.LOG_SYSLOG)#logging 发送到 syslog
fmt = '%(asctime)s %(levelname)s %(message)s' # 创建输出格式
formatter = logging.Formatter(fmt) # 实例化formatter
# handler1.setFormatter(formatter) # 为handler添加formatter
# handler.setFormatter(formatter)
handler3.setFormatter(formatter)
self.logger = logging.getLogger(key) # 获取名为tst的logger,会返回同一个logger对象
# self.logger.addHandler(handler1)
# self.logger.addHandler(handler) # 为logger添加handler
self.logger.addHandler(handler3) #为logger添加fh
self.logger.setLevel(logging.DEBUG)
def info(self,msg):
self.logger.info(msg)
def error(self,msg):
self.logger.error(msg)
def exception(self,msg):
self.logger.exception(msg)
def warning(self,msg):
self.logger.warning(msg)
def critical(self,msg):
self.logger.critical(msg)
def debug(self,msg):
self.logger.debug(msg)
def log(self,msg):
self.logger.log(msg)