import os
import time
import logging
class logger(object):
def __init__(self,logger):
#创建一个logger
self.logger = logging.getlogger(logger)
#设置级别
self.logger.setlevel(logging.DEBUG)
#创建一个handler用于写入日志文件
#年月日
rq = time.strftime('%Y%m%d',time.localtime(time.time()))
log_path1 = os.path.dirname(os.path.abspath('.')+'/logs/'
log_name = log_path1+ rq +".logs"
fh = logging.StreamHandler(log_name)
fh.setLevel(logging.info)
# 再创建一个handler用于输出控制台
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
#定义handler的输出格式
formatter = logging.Formatter("%(asctime)s-%(levelname)s-%(message)s-%(name)s-line:%(lineno)d")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
#给logger添加handler
self.logger.addhandler(fh)
self.logger.addhandler(ch)
def getlog(self):
return self.logger
if __name__=='__main__':
logger1 = logger()
logger1.__init__()
logger1.getlog()