-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from intercreate/feature/logging
Feature/logging
- Loading branch information
Showing
6 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Logging configuration for smpmgr.""" | ||
|
||
import asyncio | ||
import logging | ||
from enum import Enum, unique | ||
from pathlib import Path | ||
|
||
import click | ||
import serial | ||
import typer | ||
from rich.logging import RichHandler | ||
|
||
|
||
@unique | ||
class LogLevel(Enum): | ||
CRITICAL = 'CRITICAL' | ||
ERROR = 'ERROR' | ||
WARNING = 'WARNING' | ||
INFO = 'INFO' | ||
DEBUG = 'DEBUG' | ||
NOTSET = 'NOTSET' | ||
|
||
|
||
def setup_logging(loglevel: LogLevel, logfile: Path | None) -> None: | ||
"""Setup logging for smpmgr.""" | ||
|
||
DEBUG_FORMAT = "%(message)s - %(pathname)s:%(lineno)s" | ||
DEFAULT_FORMAT = "%(message)s - %(module)s:%(lineno)s" | ||
LOGFILE_FORMAT = "%(asctime)s - %(levelname)s - %(pathname)s:%(lineno)s - %(message)s" | ||
|
||
console_handler = RichHandler( | ||
rich_tracebacks=True, tracebacks_suppress=[click, typer, asyncio, serial] | ||
) | ||
file_handler = logging.FileHandler(logfile) if logfile is not None else None | ||
|
||
logging.basicConfig( | ||
level=logging.NOTSET, # root logger logs everything | ||
format=(DEBUG_FORMAT if loglevel == LogLevel.DEBUG else DEFAULT_FORMAT), | ||
datefmt="[%X]", | ||
handlers=((console_handler,) + ((file_handler,) if file_handler is not None else ())), | ||
) | ||
|
||
console_handler.setLevel(loglevel.value) # UI console log level set from --loglevel | ||
logging.info(f"Console log level: {logging.getLevelName(console_handler.level)}") | ||
|
||
logging.info(f"Log file: {logfile}") | ||
if file_handler is not None: | ||
file_handler.setLevel(logging.DEBUG) # file logs are always DEBUG | ||
file_handler.setFormatter(logging.Formatter(LOGFILE_FORMAT)) | ||
logging.info(f"Log file {logfile} log level: {logging.getLevelName(file_handler.level)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters