From 5f86f43637878be0ce031edb2835bc3aa666b56c Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sat, 17 Aug 2024 19:30:18 -0500 Subject: [PATCH 1/4] Added filter to logging configuration to exclude logs containing the string 'DEBUG' --- telegram_inference_bot.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 97f3b02..04076d3 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -19,12 +19,20 @@ client = OpenAI() GPT_4O = "gpt-4o" GPT_4O_MINI = "gpt-4o-mini" -# Set up logging to console and file +class NoDebugFilter(logging.Filter): + def filter(self, record): + return "DEBUG" not in record.getMessage() + +# Set up logging to console and file with filter logging.basicConfig(level=logging.INFO, handlers=[ logging.StreamHandler(), logging.FileHandler('logs/output.log', mode='a') ]) +# Add filter to the handlers +for handler in logging.getLogger().handlers: + handler.addFilter(NoDebugFilter()) + # Set up Telegram bot TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') From 89b6fa3ccc2ef0d213339d3ef0892edc6bba9298 Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sat, 17 Aug 2024 19:30:38 -0500 Subject: [PATCH 2/4] Added a logging filter to suppress logs containing a specific string. --- telegram_inference_bot.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 04076d3..9aeb7e8 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -19,19 +19,22 @@ client = OpenAI() GPT_4O = "gpt-4o" GPT_4O_MINI = "gpt-4o-mini" -class NoDebugFilter(logging.Filter): - def filter(self, record): - return "DEBUG" not in record.getMessage() +class StringFilter(logging.Filter): + def __init__(self, string_to_filter): + super().__init__() + self.string_to_filter = string_to_filter -# Set up logging to console and file with filter + def filter(self, record): + return self.string_to_filter not in record.getMessage() + +string_to_filter = 'unwanted_string' # Change this to the specific string you want to filter out + +# Set up logging to console and file logging.basicConfig(level=logging.INFO, handlers=[ logging.StreamHandler(), logging.FileHandler('logs/output.log', mode='a') ]) - -# Add filter to the handlers -for handler in logging.getLogger().handlers: - handler.addFilter(NoDebugFilter()) +logging.getLogger().addFilter(StringFilter(string_to_filter)) # Set up Telegram bot TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') From 7103b1ee72f671a57edcfdee3fa45a6aa028c60f Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sat, 17 Aug 2024 19:32:01 -0500 Subject: [PATCH 3/4] Updated logging filter to check against an array of unwanted strings. --- telegram_inference_bot.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 9aeb7e8..64d64b1 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -20,21 +20,21 @@ GPT_4O = "gpt-4o" GPT_4O_MINI = "gpt-4o-mini" class StringFilter(logging.Filter): - def __init__(self, string_to_filter): + def __init__(self, strings_to_filter): super().__init__() - self.string_to_filter = string_to_filter + self.strings_to_filter = strings_to_filter 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 logging.basicConfig(level=logging.INFO, handlers=[ logging.StreamHandler(), logging.FileHandler('logs/output.log', mode='a') ]) -logging.getLogger().addFilter(StringFilter(string_to_filter)) +logging.getLogger().addFilter(StringFilter(unwanted_strings)) # Set up Telegram bot TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') From bda3b044e4aad92221021eba199d5c1dd93b6235 Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sat, 17 Aug 2024 19:32:26 -0500 Subject: [PATCH 4/4] Modify logging filter to exclude multiple strings. --- telegram_inference_bot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 64d64b1..f535faa 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -25,16 +25,16 @@ class StringFilter(logging.Filter): self.strings_to_filter = strings_to_filter def filter(self, record): - return not any(string in record.getMessage() for string in self.strings_to_filter) + return not any(s in record.getMessage() for s in self.strings_to_filter) -unwanted_strings = ['unwanted_string1', 'unwanted_string2'] # Add other strings you want to filter out here +strings_to_filter = ['unwanted_string_1', 'unwanted_string_2'] # Change these to the specific strings you want to filter out # Set up logging to console and file logging.basicConfig(level=logging.INFO, handlers=[ logging.StreamHandler(), logging.FileHandler('logs/output.log', mode='a') ]) -logging.getLogger().addFilter(StringFilter(unwanted_strings)) +logging.getLogger().addFilter(StringFilter(strings_to_filter)) # Set up Telegram bot TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')