Skip to content

Commit

Permalink
Add current thread to logs
Browse files Browse the repository at this point in the history
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.
```
  • Loading branch information
jonaseberle committed Oct 30, 2023
1 parent 7f5b3f2 commit d43df53
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions autoortho/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down

0 comments on commit d43df53

Please sign in to comment.