Skip to content

Commit

Permalink
Add more function to manipulate logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dachengx committed Sep 14, 2024
1 parent e49fcba commit aadecea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion utilix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
uconfig = config.Config()

if uconfig.is_configured:
logger = config.setup_logger(uconfig.logging_level)
logger = config.setup_logger(logging_level=uconfig.logging_level)
else:
uconfig = None # type: ignore
logger = config.setup_logger()
Expand Down
19 changes: 15 additions & 4 deletions utilix/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
import logging


def setup_logger(logging_level="WARNING"):
logger = logging.getLogger("utilix")
def setup_logger(logger="utilix", logging_level="WARNING"):
set_logging_level(logger=logger, logging_level=logging_level)
logger = setup_handler(logger=logger, logging_level=logging_level)
return logger


def set_logging_level(logger="utilix", logging_level="WARNING"):
logger = logging.getLogger(logger)
logger.setLevel(logging_level)
return logger


def setup_handler(logger="utilix", logging_level="WARNING"):
logger = logging.getLogger(logger)
if logger.hasHandlers():
logger.handlers.clear()
ch = logging.StreamHandler()
ch.setLevel(logging_level)
logger.setLevel(logging_level)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
Expand Down Expand Up @@ -96,7 +107,7 @@ def get_list(self, category, key):
@property
def logging_level(self):
# look for logging level in 'basic' field in config file. Defaults to WARNING
level = self.get("basic", "logging_level", fallback="WARNING").upper()
level = self.get("basic", "logging_level", fallback="WARNING")
possible_levels = ["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
if level not in possible_levels:
raise RuntimeError(
Expand Down
8 changes: 3 additions & 5 deletions utilix/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ def func_wrapper(*args, **kwargs):
st = func(*args, **kwargs)
if st.status_code != 200:
logger.error(
"\n\tAPI Call was {0}\n\tReturn code: {1}\n\tReason: {2} ".format(
args[1],
st.status_code,
st.text,
)
f"\n\tAPI Call was {args[1]}"
f"\n\tReturn code: {st.status_code}"
f"\n\tReason: {st.text} "
)

if st.status_code == 401:
Expand Down

0 comments on commit aadecea

Please sign in to comment.