Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup logs #355

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,4 @@ contracting
lamden
/xian_venv
cometbft
logs
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ logs:
pm2 logs --lines 1000

down:
pm2 stop all
sleep 5 # Give Loguru time to handle any final cleanup
pm2 delete all

restart:
Expand Down
52 changes: 46 additions & 6 deletions src/xian/xian_abci.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import sys
import gc
import asyncio

from xian.constants import Constants
import signal

from loguru import logger
from datetime import timedelta
from datetime import timedelta, datetime
from abci.server import ABCIServer
from xian.constants import Constants
from xian.services.bds.bds import BDS
from contracting.client import ContractingClient

Expand Down Expand Up @@ -40,6 +40,8 @@
get_logger("urllib3").setLevel(30)
get_logger("asyncio").setLevel(30)

LOG_RETENTION_DAYS = 3


def load_module(module_path, original_module_path):
"""
Expand Down Expand Up @@ -176,25 +178,63 @@ async def query(self, req):
return res


def cleanup_old_logs(logs_dir: str, days: int = 3):
"""Clean up log files older than specified days on startup"""
try:
threshold = datetime.now() - timedelta(days=days)
for f in os.listdir(logs_dir):
if not f.endswith('.log'):
continue

file_path = os.path.join(logs_dir, f)
file_time = datetime.fromtimestamp(os.path.getctime(file_path))

if file_time < threshold:
try:
os.remove(file_path)
logger.debug(f"Removed old log file: {f}")
except OSError as e:
logger.error(f"Error removing old log file {f}: {e}")
except Exception as e:
logger.error(f"Error during log cleanup: {e}")


def main():
logger.remove()

logger.add(sys.stderr, level="DEBUG")

start_path = os.path.dirname(os.path.realpath(__file__))
log_path = os.path.realpath(os.path.join(start_path, '..', '..'))
logs_dir = os.path.join(log_path, 'logs')

os.makedirs(logs_dir, exist_ok=True)

# Clean up old logs on startup
cleanup_old_logs(logs_dir, days=LOG_RETENTION_DAYS)

logger.add(
os.path.join(log_path, 'logs', '{time}.log'),
retention=timedelta(days=3),
retention=timedelta(days=LOG_RETENTION_DAYS),
rotation=timedelta(hours=1),
format="{time} {level} {name} {message}",
level="DEBUG",
rotation="10 MB",
enqueue=True,
compression="zip" # Compress old logs
)

# Register signal handlers
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)

app = asyncio.get_event_loop().run_until_complete(Xian.create())
ABCIServer(app=app).run()


def signal_handler(signum, frame):
logger.info("Shutting down...")
logger.remove()
sys.exit(0)


if __name__ == "__main__":
main()
Loading