我是python的新手。希望有人可以帮助我。我正在建立一个结构如下的项目。
main.py -sqlconn.py -runlogic.py
我阅读了有关根记录器的信息,并希望使用它,以便可以将属性层叠到所有模块。因此,我在main.py中定义了记录器,如下所示。
我的要求也是使所有INFO级别都进入sysout。 错误级别警报与sysout一起发送到数据库表。
问题:直到我收到数据库连接异常之前,它都可以正常工作。有点使记录器陷入无限循环,日志处理程序不断尝试连接并登录到数据库。
下面是用于登录到数据库的Customhandler代码。
# Define root logging settings to propagate properties and handlers to child modules
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# Create logging formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Create error handler for logging to Database table with a higher logging level.
log_db_handler = LogDbHandler(logsfclient, runid, jobid, stepid)
log_db_handler.setLevel(logging.ERROR)
sysout_handler = logging.StreamHandler(sys.stdout)
sysout_handler.setLevel(logging.INFO)
# Add it to the handler
log_db_handler.setFormatter(formatter)
sysout_handler.setFormatter(formatter)
# Add the handler to the logger
root_logger.addHandler(log_db_handler)
root_logger.addHandler(sysout_handler)
class LogDBHandler(logging.Handler):
'''
Customized logging handler that puts error logs to the snowflake database table.
'''
def __init__(self, sfclient, runid, jobid, stepid):
logging.Handler.__init__(self)
self.counter =0
self.runid = runid
self.jobid = jobid
self.stepid = stepid
self.snowflakeclient= sfclient
config = ConfigurationManager().get_config()
def emit(self, record):
# Set current time
self.counter +=1
current_tmstmp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.log_msg = str(record.msg)
self.log_msg = self.log_msg.strip()
self.log_msg = self.log_msg.replace('\'', '\'\'')
# Generate the SQL statement to udpate error log in details table
updatesql = "UPDATE {} SET STATUS ='FAILED', STEP_END_DATE='{}', ERROR_LOG='{}' WHERE JOB_ID = {} AND RUN_ID = {} AND STEP_ID={}".format(self.joblogdtlstbl, current_tmstmp, self.log_msg, self.jobid, self.runid, self.stepid)
print ("Sql generated for logging: {}".format(updatesql))
try:
print ("Logging error in job dtl table ..")
print "Invoke counter {}".format(self.counter)
self.snowflakeclient.exec_query(updatesql)
except Exception as e:
print ("Log handler failed to write into database..".format(str(e))) ### This has been marked as info so that it doesnt trigger the handler if db fails to connect.