From 26d3114ad8e59392922fddc9c0e2aeaa7be622e3 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 27 Jan 2024 09:51:33 -0800 Subject: [PATCH] add the ability to pass custom loggers --- milc/__init__.py | 10 ++++++---- milc/milc.py | 11 +++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/milc/__init__.py b/milc/__init__.py index 3b2fa8d..1c5fdcb 100644 --- a/milc/__init__.py +++ b/milc/__init__.py @@ -44,12 +44,14 @@ def argv_name(): cli = MILC(APP_NAME, APP_VERSION, APP_AUTHOR) -def set_metadata(*, name=APP_NAME, author=APP_AUTHOR, version=APP_VERSION): +def set_metadata(*, name=APP_NAME, author=APP_AUTHOR, version=APP_VERSION, logger=None): """Set metadata about your program. This allows you to set the application's name, version, and/or author - before executing your entrypoint. It's best to run this only once, and - it must be run before you call `cli()`. + before executing your entrypoint. You can also pass your own logger here + if you like. + + It's best to run this only once, and it must be run before you call `cli()`. """ global APP_NAME, APP_VERSION, APP_AUTHOR, cli @@ -59,7 +61,7 @@ def set_metadata(*, name=APP_NAME, author=APP_AUTHOR, version=APP_VERSION): APP_NAME = name APP_VERSION = version APP_AUTHOR = author - cli = MILC(name, version, author) + cli = MILC(name, version, author, logger) # Extra stuff people can import diff --git a/milc/milc.py b/milc/milc.py index d3ee12b..84472b5 100644 --- a/milc/milc.py +++ b/milc/milc.py @@ -33,7 +33,7 @@ class MILC(object): """MILC - An Opinionated Batteries Included Framework """ - def __init__(self, name, version, author): + def __init__(self, name, version, author, logger=None): """Initialize the MILC object. """ # Setup a lock for thread safety @@ -64,7 +64,7 @@ def __init__(self, name, version, author): # Initialize all the things self.initialize_config() self.initialize_argparse() - self.initialize_logging() + self.initialize_logging(logger) @property def config_dir(self): @@ -235,9 +235,12 @@ def add_argument(self, *args, **kwargs): self.release_lock() - def initialize_logging(self): + def initialize_logging(self, logger): """Prepare the defaults for the logging infrastructure. """ + if not logger: + logger = logging.getLogger(self.__class__.__name__) + self.acquire_lock() self.log_file = None self.log_file_mode = 'a' @@ -247,7 +250,7 @@ def initialize_logging(self): self.log_print_level = logging.INFO self.log_file_level = logging.INFO self.log_level = logging.INFO - self.log = logging.getLogger(self.__class__.__name__) + self.log = logger self.log.setLevel(logging.DEBUG) logging.root.setLevel(logging.DEBUG) self.release_lock()