Skip to content

Commit

Permalink
clean up old logs
Browse files Browse the repository at this point in the history
I finally got around to adding code to delete old logs. Currently it saves the last 5 logs, all the other logs will be deleted.
  • Loading branch information
ego-lay-atman-bay committed Jul 11, 2024
1 parent 85acf6d commit 96657cc
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@

__min_wmwpy_version__ = "0.6.0-beta"

import traceback
import logging
import os
import sys
import io
import platform
from datetime import datetime
import crossplatform
import re

def createLogger(type = 'file', filename = 'logs/log.log', debug = False):
def createLogger(
type = 'file',
filename = 'logs/log.log',
debug = False,
):
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
format = '[%(levelname)s] %(message)s'
Expand Down Expand Up @@ -63,9 +66,40 @@ def createLogger(type = 'file', filename = 'logs/log.log', debug = False):
logger = logging.getLogger(__name__)
logger.info(filename)

_log_filename_date_format = "%m-%d-%y_%H-%M-%S"
def setup_logger(
name: str,
dir: str = 'logs',
extension: str = 'log',
keep: int = 5,
debug: bool = False,
):
log_filename = os.path.join(dir, f'{datetime.now().strftime(name)}.{extension}')

log_files = os.listdir(dir)
logs = []

_log_filename = f'logs/{datetime.now().strftime(_log_filename_date_format)}.log'
createLogger('file', filename = log_filename, debug = debug)

for file in log_files:
if file == os.path.basename(log_filename):
continue

try:
logs.append((datetime.strptime(os.path.splitext(file)[0], name), file))
except ValueError:
continue

logs.sort(key = lambda i: i[0])

logs = logs[max(0, keep-1)::]

for log in logs:
logging.debug(f'deleting log: {log[1]}')
os.remove(os.path.join(dir, log[1]))

return log_filename

_log_filename_format = "%m-%d-%y_%H-%M-%S"

debug = False

Expand All @@ -76,13 +110,16 @@ def createLogger(type = 'file', filename = 'logs/log.log', debug = False):
if len(args) > 0:
if args[0] in ['-d', '--debug']:
debug = True
createLogger('file', filename = _log_filename, debug = debug)

_log_filename = setup_logger(
_log_filename_format,
debug = debug,
)

import tkinter as tk
from tkinter import ttk, simpledialog, messagebox, filedialog
from tkinter import ttk, messagebox, filedialog
import tkwidgets
from PIL import Image, ImageTk, ImageColor, ImageDraw
import json
from PIL import Image, ImageTk, ImageDraw
from settings import Settings
from lxml import etree
import numpy
Expand Down

0 comments on commit 96657cc

Please sign in to comment.