日志文件在应用程序重新启动时重置,而不是将新日志附加到它
Log files get reset on the restart of the application instead of appending the new logs to it
import logging
def setup_logger(logger_name, log_file, level=logging.DEBUG):
l = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s')
fileHandler = logging.FileHandler(log_file, mode='w')
fileHandler.setFormatter(formatter)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
l.setLevel(level)
l.addHandler(fileHandler)
l.addHandler(streamHandler)
我正在尝试将不同的日志(级别)写入不同的文件,该功能工作正常,但是每当重新启动应用程序时,日志文件都会重置,而不是将数据附加到其中。
#method called from the different method
setup_logger('logger', 'login.log')
setup_logger('logger_market', 'transaction.log')
logger = logging.getLogger('logger')
logger_market = logging.getLogger('logger_market')
logger.info(f'Test for the login.log file')
logger_market.info(f'this to test')
我刚开始登录 python,文档目前对我来说有点复杂。
我觉得你不需要设置mode
只需使用以下内容:fileHandler = logging.FileHandler(log_file)
import logging
def setup_logger(logger_name, log_file, level=logging.DEBUG):
l = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s')
fileHandler = logging.FileHandler(log_file, mode='w')
fileHandler.setFormatter(formatter)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
l.setLevel(level)
l.addHandler(fileHandler)
l.addHandler(streamHandler)
我正在尝试将不同的日志(级别)写入不同的文件,该功能工作正常,但是每当重新启动应用程序时,日志文件都会重置,而不是将数据附加到其中。
#method called from the different method
setup_logger('logger', 'login.log')
setup_logger('logger_market', 'transaction.log')
logger = logging.getLogger('logger')
logger_market = logging.getLogger('logger_market')
logger.info(f'Test for the login.log file')
logger_market.info(f'this to test')
我刚开始登录 python,文档目前对我来说有点复杂。
我觉得你不需要设置mode
只需使用以下内容:fileHandler = logging.FileHandler(log_file)