importloggingimportlogging.configimportlogging.handlersimportnox## Detect container env, or default to Falseif"CONTAINER_ENV"inos.environ:CONTAINER_ENV:bool=os.environ["CONTAINER_ENV"]else:CONTAINER_ENV:bool=Falsedefsetup_nox_logging(level_name:str="DEBUG",disable_loggers:list[str]|None=[])->None:"""Configure a logger for the Nox module. Params: level_name (str): The uppercase string repesenting a logging logLevel. disable_loggers (list[str] | None): A list of logger names to disable, i.e. for 3rd party apps. Note: Disabling means setting the logLevel to `WARNING`, so you can still see errors. """## If container environment detected, default to logging.DEBUGifCONTAINER_ENV:level_name:str="DEBUG"logging_config:dict={"version":1,"disable_existing_loggers":False,"loggers":{"nox":{"level":level_name.upper(),"handlers":["console"],"propagate":False,}},"handlers":{"console":{"class":"logging.StreamHandler","formatter":"nox","level":"DEBUG","stream":"ext://sys.stdout",}},"formatters":{"nox":{"format":"[NOX] [%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s","datefmt":"%Y-%m-%D %H:%M:%S",}},}## Configure logging. Only run this once in an applicationlogging.config.dictConfig(config=logging_config)## Disable loggers by name. Sets logLevel to logging.WARNING to suppress all but warnings & errorsfor_loggerindisable_loggers:logging.getLogger(_logger).setLevel(logging.WARNING)## Configure loggingsetup_nox_logging()## Create logger for this modulelog:logging.Logger=logging.getLogger("nox")
Disable loggers by name
If you use the setup_nox_logging() method with a list of logger names (as str objects, i.e. urllib3, sqlalchemy, etc), those loggers will have their log level set to logging.WARNING. This effectively silences them; warning, error, and critical messages will still show.
defsetup_nox_logging(level_name:str="DEBUG",disable_loggers:list[str]|None=[])->None:...## Disable loggers by name. Sets logLevel to logging.WARNING to suppress all but warnings & errorsfor_loggerindisable_loggers:logging.getLogger(_logger).setLevel(logging.WARNING)## Disable the requests and urllib3 loggerssetup_nox_logging(disable_loggers=["requests","urllib3"])