该
logging.getLogger()已经是一个单例。(文件)
问题在于,每次调用时
myLogger(),都会向实例添加另一个处理程序,这将导致日志重复。
也许像这样?
import osimport timeimport datetimeimport loggingloggers = {}def myLogger(name): global loggers if loggers.get(name): return loggers.get(name) else: logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) now = datetime.datetime.now() handler = logging.FileHandler( '/root/credentials/Logs/ProvisioningPython' + now.strftime("%Y-%m-%d") + '.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) loggers[name] = logger return logger
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)