Updated logging filter to check against an array of unwanted strings.

This commit is contained in:
2024-08-17 19:32:01 -05:00
parent 89b6fa3ccc
commit 7103b1ee72
+5 -5
View File
@@ -20,21 +20,21 @@ GPT_4O = "gpt-4o"
GPT_4O_MINI = "gpt-4o-mini" GPT_4O_MINI = "gpt-4o-mini"
class StringFilter(logging.Filter): class StringFilter(logging.Filter):
def __init__(self, string_to_filter): def __init__(self, strings_to_filter):
super().__init__() super().__init__()
self.string_to_filter = string_to_filter self.strings_to_filter = strings_to_filter
def filter(self, record): def filter(self, record):
return self.string_to_filter not in record.getMessage() return not any(string in record.getMessage() for string in self.strings_to_filter)
string_to_filter = 'unwanted_string' # Change this to the specific string you want to filter out unwanted_strings = ['unwanted_string1', 'unwanted_string2'] # Add other strings you want to filter out here
# Set up logging to console and file # Set up logging to console and file
logging.basicConfig(level=logging.INFO, handlers=[ logging.basicConfig(level=logging.INFO, handlers=[
logging.StreamHandler(), logging.StreamHandler(),
logging.FileHandler('logs/output.log', mode='a') logging.FileHandler('logs/output.log', mode='a')
]) ])
logging.getLogger().addFilter(StringFilter(string_to_filter)) logging.getLogger().addFilter(StringFilter(unwanted_strings))
# Set up Telegram bot # Set up Telegram bot
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')