···9191- Authenticate using the AT Protocol.
9292- Update the Bluesky avatar accordingly.
93939494-Execution logs will be displayed directly in the console, and a log file will be created in the `./logs/` directory. The log file will rotate when it reaches 10 MB, keeping up to 5 backup log files, each named with a timestamp for each execution.
9494+Execution logs will be displayed directly in the console, and a log file will be created in the `./logs/` directory. The log file will rotate every 14 days, keeping up to 5 backup log files. Logs older than 30 days will be automatically deleted.
95959696## Automating with Cron (Linux)
9797···136136137137- **Endpoint not responding?** Verify that the Bluesky API endpoint is correct and accessible.
138138- **Cron job not running?** Verify that the cron job was properly set up using `crontab -l` or set it up manually.
139139+- **Old logs not deleting?** Ensure the script has the necessary permissions to delete files in the `./logs/` directory.
139140140141## License
141142
+23-7
src/main.py
···99from atproto.exceptions import BadRequestError
1010import sys
1111from crontab import CronTab
1212-from logging.handlers import RotatingFileHandler
1212+from logging.handlers import TimedRotatingFileHandler
1313+import glob
1414+import time
13151416# Ensure the script is run inside a virtual environment
1517if not hasattr(sys, 'real_prefix') and not (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
···3032if not os.path.exists(log_dir):
3133 os.makedirs(log_dir)
32343333-# Log file with datetime stamp, but will be rotated
3434-log_file_path = os.path.join(log_dir, f"avatar_update_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
3535+def cleanup_old_logs(log_directory, days=30):
3636+ """Deletes log files older than the specified number of days."""
3737+ cutoff = time.time() - (days * 86400) # Convert days to seconds
3838+ for log_file in glob.glob(os.path.join(log_directory, "avatar_update*.log")):
3939+ if os.path.isfile(log_file) and os.path.getmtime(log_file) < cutoff:
4040+ os.remove(log_file)
4141+ print(f"Deleted old log: {log_file}")
35423636-# Configure logging to both console and file with rotation
4343+# Cleanup logs older than 30 days before setting up new logging
4444+cleanup_old_logs(log_dir, days=30)
4545+4646+# Use a fixed log file name for the current log; TimedRotatingFileHandler will manage rotations.
4747+log_file_path = os.path.join(log_dir, "avatar_update.log")
4848+4949+# Configure logging to both console and file with bi-weekly rotation
3750console_handler = logging.StreamHandler()
3851console_handler.setLevel(logging.INFO) # Show only INFO and higher levels on console
39524040-# Create a rotating file handler (max file size 10 MB, backup 5 old log files)
4141-file_handler = RotatingFileHandler(
4242- log_file_path, maxBytes=10 * 1024 * 1024, backupCount=5
5353+# Create a timed rotating file handler (rotate every 14 days, keep up to 5 backups)
5454+file_handler = TimedRotatingFileHandler(
5555+ log_file_path,
5656+ when="D", # 'D' stands for days
5757+ interval=14, # Rotate every 14 days
5858+ backupCount=5
4359)
4460file_handler.setLevel(logging.INFO) # Save all logs to file
4561