try
This commit is contained in:
+26
-31
@@ -21,6 +21,7 @@ from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Define constants
|
||||
REFERENCE_DATE_FILE = ENGAGEMENT_REFERENCE_DATE_FILE
|
||||
LOCK_FILE = "/home/shane/foodie_automator/locks/foodie_engagement_tweet.lock"
|
||||
LOG_FILE = "/home/shane/foodie_automator/logs/foodie_engagement_tweet.log"
|
||||
@@ -31,27 +32,32 @@ URL = "https://insiderfoodie.com"
|
||||
URL_SHORTENED_LENGTH = 23 # Twitter's shortened URL length
|
||||
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)
|
||||
# Setup logging at the very start
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
# 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'
|
||||
)
|
||||
# Clear any existing handlers (in case another script configured the logger)
|
||||
logger.handlers = []
|
||||
|
||||
# Console handler
|
||||
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)
|
||||
console_handler.setLevel(logging.DEBUG)
|
||||
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
|
||||
logger.addHandler(console_handler)
|
||||
|
||||
# Now that logging is configured, prune old logs if the file exists
|
||||
# File handler
|
||||
try:
|
||||
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
|
||||
file_handler = logging.FileHandler(LOG_FILE, mode='a')
|
||||
file_handler.setLevel(logging.DEBUG)
|
||||
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
|
||||
logger.addHandler(file_handler)
|
||||
logging.info("File logging initialized successfully.")
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to setup file logging to {LOG_FILE}: {e}. Continuing with console-only logging.")
|
||||
|
||||
# Prune old logs if the file exists
|
||||
try:
|
||||
if os.path.exists(LOG_FILE):
|
||||
with open(LOG_FILE, 'r') as f:
|
||||
lines = f.readlines()
|
||||
@@ -73,22 +79,12 @@ def setup_logging():
|
||||
logging.info(f"Skipped {malformed_count} malformed log lines during pruning")
|
||||
with open(LOG_FILE, 'w') as f:
|
||||
f.writelines(pruned_lines)
|
||||
except Exception as e:
|
||||
logging.warning(f"Failed to prune log file {LOG_FILE}: {e}")
|
||||
|
||||
logging.info("Logging initialized for foodie_engagement_tweet.py")
|
||||
except Exception as e:
|
||||
# Fallback to console-only logging if file logging fails
|
||||
logging.basicConfig(
|
||||
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.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."""
|
||||
@@ -434,7 +430,6 @@ def main():
|
||||
lock_fd = None
|
||||
try:
|
||||
lock_fd = acquire_lock()
|
||||
setup_logging()
|
||||
post_engagement_tweet()
|
||||
except Exception as e:
|
||||
logging.error(f"Fatal error in main: {e}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user