try
This commit is contained in:
@@ -34,7 +34,24 @@ CURRENT_YEAR = "2025" # Explicitly set the current year for the prompt
|
|||||||
def setup_logging():
|
def setup_logging():
|
||||||
"""Initialize logging with pruning of old logs."""
|
"""Initialize logging with pruning of old logs."""
|
||||||
try:
|
try:
|
||||||
|
# Ensure the logs directory exists
|
||||||
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
|
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):
|
if os.path.exists(LOG_FILE):
|
||||||
with open(LOG_FILE, 'r') as f:
|
with open(LOG_FILE, 'r') as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
@@ -57,21 +74,21 @@ def setup_logging():
|
|||||||
with open(LOG_FILE, 'w') as f:
|
with open(LOG_FILE, 'w') as f:
|
||||||
f.writelines(pruned_lines)
|
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(
|
logging.basicConfig(
|
||||||
filename=LOG_FILE,
|
|
||||||
level=logging.DEBUG,
|
level=logging.DEBUG,
|
||||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||||
datefmt='%Y-%m-%d %H:%M:%S'
|
datefmt='%Y-%m-%d %H:%M:%S'
|
||||||
)
|
)
|
||||||
console_handler = logging.StreamHandler()
|
console_handler = logging.StreamHandler()
|
||||||
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
||||||
|
console_handler.setLevel(logging.DEBUG)
|
||||||
logging.getLogger().addHandler(console_handler)
|
logging.getLogger().addHandler(console_handler)
|
||||||
logging.getLogger("openai").setLevel(logging.WARNING)
|
logging.getLogger("openai").setLevel(logging.WARNING)
|
||||||
logging.getLogger("tweepy").setLevel(logging.WARNING)
|
logging.getLogger("tweepy").setLevel(logging.WARNING)
|
||||||
logging.info("Logging initialized for foodie_engagement_tweet.py")
|
logging.error(f"Failed to setup file logging to {LOG_FILE}: {e}. Falling back to console-only logging.")
|
||||||
except Exception as e:
|
|
||||||
print(f"Failed to setup logging: {e}")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
def acquire_lock():
|
def acquire_lock():
|
||||||
"""Acquire a lock to prevent concurrent runs."""
|
"""Acquire a lock to prevent concurrent runs."""
|
||||||
|
|||||||
Reference in New Issue
Block a user