3
我有一個利用多處理來提高效率的python程序和一個爲每個進程創建一個記錄器的函數。該記錄器功能如下:Python日誌記錄setLevel()沒有生效
import logging
import os
def create_logger(app_name):
"""Create a logging interface"""
# create a logger
if logging in os.environ:
logging_string = os.environ["logging"]
if logging_string == "DEBUG":
logging_level = loggin.DEBUG
else if logging_string == "INFO":
logging_level = logging.INFO
else if logging_string == "WARNING":
logging_level = logging.WARNING
else if logging_string == "ERROR":
logging_level = logging.ERROR
else if logging_string == "CRITICAL":
logging_level = logging.CRITICAL
else:
logging_level = logging.INFO
logger = logging.getLogger(app_name)
logger.setLevel(logging_level)
# Console handler for error output
console_handler = logging.StreamHandler()
console_handler.setLevel(logging_level)
# Formatter to make everything look nice
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(console_handler)
return logger
我的處理功能是這樣的:
import custom_logging
def do_capture(data_dict_access):
"""Process data"""
# Custom logging
LOGGER = custom_logging.create_logger("processor")
LOGGER.debug("Doing stuff...")
但是,不管是什麼日誌環境變量設置爲,我還是收到了調試日誌消息安慰。爲什麼我的日誌記錄級別沒有生效,當然,對setLevel()的調用應該停止記錄調試消息?
首先:'其他if'應該是'elif' –
@HaiVu奇怪,我不是從得到一個語法錯誤。無論如何改變了,仍然沒有區別 – CyberJacob
我發佈了一個解決方案,但仍試圖找出爲什麼你的代碼不工作。 –