From cd62ca9a1f696c5632406226053a2aff5c7d7f72 Mon Sep 17 00:00:00 2001 From: Shane Date: Wed, 7 May 2025 13:21:59 +1000 Subject: [PATCH] try --- foodie_engagement_tweet.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/foodie_engagement_tweet.py b/foodie_engagement_tweet.py index af98fc9..607c6d0 100644 --- a/foodie_engagement_tweet.py +++ b/foodie_engagement_tweet.py @@ -34,7 +34,24 @@ CURRENT_YEAR = "2025" # Explicitly set the current year for the prompt def setup_logging(): """Initialize logging with pruning of old logs.""" try: + # Ensure the logs directory exists os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True) + + # Configure logging first + logging.basicConfig( + filename=LOG_FILE, + level=logging.DEBUG, + format='%(asctime)s - %(levelname)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S' + ) + console_handler = logging.StreamHandler() + console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) + console_handler.setLevel(logging.DEBUG) # Show DEBUG messages in console too + logging.getLogger().addHandler(console_handler) + logging.getLogger("openai").setLevel(logging.WARNING) + logging.getLogger("tweepy").setLevel(logging.WARNING) + + # Now that logging is configured, prune old logs if the file exists if os.path.exists(LOG_FILE): with open(LOG_FILE, 'r') as f: lines = f.readlines() @@ -57,21 +74,21 @@ def setup_logging(): with open(LOG_FILE, 'w') as f: f.writelines(pruned_lines) + logging.info("Logging initialized for foodie_engagement_tweet.py") + except Exception as e: + # Fallback to console-only logging if file logging fails logging.basicConfig( - filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) console_handler = logging.StreamHandler() console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) + console_handler.setLevel(logging.DEBUG) logging.getLogger().addHandler(console_handler) logging.getLogger("openai").setLevel(logging.WARNING) logging.getLogger("tweepy").setLevel(logging.WARNING) - logging.info("Logging initialized for foodie_engagement_tweet.py") - except Exception as e: - print(f"Failed to setup logging: {e}") - sys.exit(1) + logging.error(f"Failed to setup file logging to {LOG_FILE}: {e}. Falling back to console-only logging.") def acquire_lock(): """Acquire a lock to prevent concurrent runs."""