我有一个log.py模块,该模块至少在另外两个模块(server.py和device.py)中使用。它具有以下全局变量:fileLogger = logging.getLogger()fileLogger.setLevel(logging.DEBUG)consoleLogger = logging.getLogger()consoleLogger.setLevel(logging.DEBUG)file_logging_level_switch = { 'debug': fileLogger.debug, 'info': fileLogger.info, 'warning': fileLogger.warning, 'error': fileLogger.error, 'critical': fileLogger.critical}console_logging_level_switch = { 'debug': consoleLogger.debug, 'info': consoleLogger.info, 'warning': consoleLogger.warning, 'error': consoleLogger.error, 'critical': consoleLogger.critical}它具有两个功能:def LoggingInit( logPath, logFile, html=True ): global fileLogger global consoleLogger logFormatStr = "[%(asctime)s %(threadName)s, %(levelname)s] %(message)s" consoleFormatStr = "[%(threadName)s, %(levelname)s] %(message)s" if html: logFormatStr = "<p>" + logFormatStr + "</p>" # File Handler for log file logFormatter = logging.Formatter(logFormatStr) fileHandler = logging.FileHandler( "{0}{1}.html".format( logPath, logFile )) fileHandler.setFormatter( logFormatter ) fileLogger.addHandler( fileHandler ) # Stream Handler for stdout, stderr consoleFormatter = logging.Formatter(consoleFormatStr) consoleHandler = logging.StreamHandler() consoleHandler.setFormatter( consoleFormatter ) consoleLogger.addHandler( consoleHandler )和:def WriteLog( string, print_screen=True, remove_newlines=True, level='debug' ): if remove_newlines: string = string.replace('\r', '').replace('\n', ' ') if print_screen: console_logging_level_switch[level](string) file_logging_level_switch[level](string)我LoggingInit从调用server.py,它会初始化文件和控制台记录器。然后WriteLog,我从各地进行呼叫,因此有多个线程正在访问fileLogger和consoleLogger。我的日志文件是否需要任何进一步的保护?文档指出线程锁由处理程序处理。
添加回答
举报
0/150
提交
取消