Skip to content

Commit

Permalink
Log System Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Oct 1, 2024
1 parent 18c9561 commit 9351814
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions utils/errorhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,30 @@ def setup_logging():
os.makedirs('logs')

log_filename = f"palbot_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log"
log_path = os.path.join('logs', log_filename)

log_handler = RotatingFileHandler(
filename=os.path.join('logs', log_filename),
maxBytes=0,
backupCount=5,
filename=log_path,
maxBytes=10**7,
backupCount=6,
encoding='utf-8'
)

log_formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')
log_handler.setFormatter(log_formatter)

logging.basicConfig(handlers=[log_handler], level=logging.INFO)
logging.basicConfig(level=logging.INFO, handlers=[log_handler])

clean_old_logs('logs', 10)

def clean_old_logs(directory, max_logs):
log_files = sorted(
[os.path.join(directory, f) for f in os.listdir(directory) if f.startswith("palbot_") and f.endswith(".log")],
key=os.path.getctime,
reverse=True
)

while len(log_files) > max_logs:
os.remove(log_files.pop())

async def handle_errors(interaction, error):
try:
Expand Down

0 comments on commit 9351814

Please sign in to comment.