Skip to content

Commit

Permalink
Added trigger log entry for midnight log rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
blink-zero committed Jun 21, 2024
1 parent 0ea3e17 commit 099ba8b
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import re
import time
import json
Expand All @@ -13,6 +13,7 @@
from apps import create_app, db
from werkzeug.security import generate_password_hash
from apps.utils.logging import log_json, JSONFormatter
import threading

# Initialize directories
required_dirs = [
Expand Down Expand Up @@ -135,13 +136,13 @@ def inject_host_status():

# Handler for app.log (standard logs)
file_handler = TimedRotatingFileHandler('logs/app.log', when='midnight', interval=1, backupCount=30)
file_handler.suffix = "%Y-%m-%d"
file_handler.suffix = "%Y-%m-%d_%H-%M-%S"
file_handler.setFormatter(log_formatter)
file_handler.setLevel(logging.INFO)

# Handler for app_json.log (JSON logs)
json_file_handler = TimedRotatingFileHandler('logs/app_json.log', when='midnight', interval=1, backupCount=30)
json_file_handler.suffix = "%Y-%m-%d"
json_file_handler.suffix = "%Y-%m-%d_%H-%M-%S"
json_file_handler.setFormatter(JSONFormatter())
json_file_handler.setLevel(logging.INFO)

Expand All @@ -160,14 +161,30 @@ def inject_host_status():
# Clean up old log files
log_directory = 'logs'
log_retention_days = 30
log_file_pattern = re.compile(r'app\.log\.\d{4}-\d{2}-\d{2}')
log_file_pattern = re.compile(r'app\.log\.\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}')

for log_file in os.listdir(log_directory):
file_path = os.path.join(log_directory, log_file)
if os.path.isfile(file_path) and log_file_pattern.match(log_file):
file_creation_time = os.path.getctime(file_path)
if (time.time() - file_creation_time) // (24 * 3600) >= log_retention_days:
os.remove(file_path)


# Function to wait until the next midnight and log a message
def log_at_midnight():
while True:
now = datetime.now()
# Calculate the time until the next midnight
next_midnight = (now + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
sleep_time = (next_midnight - now).total_seconds()
time.sleep(sleep_time)
app.logger.info("Midnight log entry to trigger log rotation.")
json_logger.info("Midnight JSON log entry to trigger log rotation.")

# Start the midnight logging in a separate thread
midnight_thread = threading.Thread(target=log_at_midnight)
midnight_thread.daemon = True
midnight_thread.start()

if __name__ == "__main__":
app.run(host='0.0.0.0')

0 comments on commit 099ba8b

Please sign in to comment.