From d43df5380e1f13505da559bc0f196714473ea2ae Mon Sep 17 00:00:00 2001 From: Jonas Eberle Date: Mon, 16 Oct 2023 20:59:13 +0200 Subject: [PATCH] Add current thread to logs This adds the current `thread_name` to log entries which allows to more easily follow code paths. The resulting log lines (console and file) look like this: ``` DEBUG [Thread-43 (clean)] getortho: NUM OPEN TILES: 24. TOTAL MEM: 2282 MB INFO [Thread-71 (_udp_listen)] flighttrack: UDP listen thread exiting... INFO [MainThread] flighttrack: FlightTracker exiting. ``` --- autoortho/__main__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/autoortho/__main__.py b/autoortho/__main__.py index 028fdb45..d295e219 100644 --- a/autoortho/__main__.py +++ b/autoortho/__main__.py @@ -12,17 +12,24 @@ def setuplogs(): os.makedirs(log_dir) log_level=logging.DEBUG if os.environ.get('AO_DEBUG') or CFG.general.debug else logging.INFO + log_formatter = logging.Formatter("%(levelname)s [%(threadName)s] %(name)s: %(message)s") + log_streamHandler = logging.StreamHandler() + log_streamHandler.setFormatter(log_formatter) + + log_fileHandler = logging.handlers.RotatingFileHandler( + filename=os.path.join(log_dir, "autoortho.log"), + maxBytes=10485760, + backupCount=5 + ) + log_fileHandler.setFormatter(log_formatter) + logging.basicConfig( #filename=os.path.join(log_dir, "autoortho.log"), level=log_level, handlers=[ #logging.FileHandler(filename=os.path.join(log_dir, "autoortho.log")), - logging.handlers.RotatingFileHandler( - filename=os.path.join(log_dir, "autoortho.log"), - maxBytes=10485760, - backupCount=5 - ), - logging.StreamHandler() if sys.stdout is not None else logging.NullHandler() + log_fileHandler, + log_streamHandler if sys.stdout is not None else logging.NullHandler() ] ) log = logging.getLogger(__name__)