0
我建立一個Logger類的項目,我已經指定了一個輸出,呈現日誌格式Python日誌記錄 - 額外輸出?
2017-05-16 13:29:17,573 - Geospatial - INFO - Using only New York Geolocations
2017-05-16 13:29:18,325 - Server - INFO - Successfully launched AppLogs consumer
但控制檯也打印這一行,我不想要的。
INFO:Server:Successfully launched AppLogs consumer
該記錄儀打印正確的輸出到輸出文件。我想也許這與處理程序有關,但我無法通過我嘗試的修改來阻止它。任何想法將不勝感激。
這裏的類:
import os
import logging
import logging.config
import yaml
class Logger():
#Types of Logs: debug, info, warning & error
def __init__(self, name):
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)
if 'file_name' in cfg['logging']:
file_name = cfg['logging']['file_name']
else:
file_name = 'application'
if 'folder_name' in cfg['logging']:
folder_name = cfg['logging']['folder_name']
else:
folder_name = 'logs'
if not os.path.exists('{}/'.format(folder_name)):
os.makedirs(folder_name)
file_name = os.path.join(os.getcwd(),
'{}/{}.log'.format(folder_name, file_name))
name = name.replace('.log','')
if name.lower() in cfg['logging']:
if cfg['logging'][name.lower()] == 'info':
level = logging.INFO
elif cfg['logging'][name.lower()] == 'debug':
level = logging.DEBUG
elif cfg['logging'][name.lower()] == 'error':
level = logging.ERROR
else:
level = logging.DEBUG
else:
level = logging.DEBUG
logger = logging.getLogger('{}'.format(name))
logger.setLevel(level)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.FileHandler(file_name)
handler.setFormatter(formatter)
handler.setLevel(level)
logger.addHandler(handler)
ch = logging.StreamHandler()
ch.setLevel(level)
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
self._logger = logger
def get(self):
return self._logger