Skip to content

Commit

Permalink
add the ability to pass custom loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
skullydazed committed Jan 27, 2024
1 parent 858a7e5 commit 26d3114
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 6 additions & 4 deletions milc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions milc/milc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'
Expand All @@ -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()
Expand Down

0 comments on commit 26d3114

Please sign in to comment.