diff --git a/python/understack-workflows/understack_workflows/helpers.py b/python/understack-workflows/understack_workflows/helpers.py index c62f28805..b63d42e21 100644 --- a/python/understack-workflows/understack_workflows/helpers.py +++ b/python/understack-workflows/understack_workflows/helpers.py @@ -8,16 +8,21 @@ logger = logging.getLogger(__name__) -def setup_logger(name): - logger = logging.getLogger(name) - handler = logging.StreamHandler() - formatter = logging.Formatter( - "%(asctime)s - %(name)s - %(levelname)s - %(message)s" +def setup_logger(name: str | None = None, level: int = logging.DEBUG): + """Standardize our logging. + + Configures the root logger to prefix messages with a timestamp + and to output the log level we want to see by default. + + params: + name: logger hierarchy or root logger + level: default log level (DEBUG) + """ + logging.basicConfig( + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + level=logging.DEBUG, ) - handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging.DEBUG) - return logger + return logging.getLogger(name) def arg_parser(name): diff --git a/python/understack-workflows/understack_workflows/main/synchronize_interfaces.py b/python/understack-workflows/understack_workflows/main/synchronize_interfaces.py index 84147546a..58f2eb586 100644 --- a/python/understack-workflows/understack_workflows/main/synchronize_interfaces.py +++ b/python/understack-workflows/understack_workflows/main/synchronize_interfaces.py @@ -6,14 +6,11 @@ import pynautobot from ironicclient.v1.port import Port +from understack_workflows.helpers import setup_logger from understack_workflows.ironic.client import IronicClient from understack_workflows.port_configuration import PortConfiguration -logger = logging.getLogger(__name__) -handler = logging.StreamHandler() -formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") -handler.setFormatter(formatter) -logger.addHandler(handler) +logger = setup_logger(__name__) def get_nautobot_interfaces(device_id: UUID) -> list[PortConfiguration]: diff --git a/python/understack-workflows/understack_workflows/main/synchronize_obm_creds.py b/python/understack-workflows/understack_workflows/main/synchronize_obm_creds.py index cf9bc1ade..0fb46ed3a 100644 --- a/python/understack-workflows/understack_workflows/main/synchronize_obm_creds.py +++ b/python/understack-workflows/understack_workflows/main/synchronize_obm_creds.py @@ -1,17 +1,14 @@ import json -import logging import sys import ironicclient.common.apiclient.exceptions +from understack_workflows.helpers import setup_logger from understack_workflows.ironic.client import IronicClient from understack_workflows.ironic.secrets import read_secret from understack_workflows.node_configuration import IronicNodeConfiguration -logging.basicConfig( - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG -) -logger = logging.getLogger(__name__) +logger = setup_logger(__name__) def event_to_node_configuration(event: dict) -> IronicNodeConfiguration: diff --git a/python/understack-workflows/understack_workflows/main/synchronize_server.py b/python/understack-workflows/understack_workflows/main/synchronize_server.py index 0ef1657ec..65969200a 100644 --- a/python/understack-workflows/understack_workflows/main/synchronize_server.py +++ b/python/understack-workflows/understack_workflows/main/synchronize_server.py @@ -1,19 +1,14 @@ import json -import logging import sys import ironicclient.common.apiclient.exceptions +from understack_workflows.helpers import setup_logger from understack_workflows.ironic.client import IronicClient from understack_workflows.ironic.secrets import read_secret from understack_workflows.node_configuration import IronicNodeConfiguration -logger = logging.getLogger(__name__) -handler = logging.StreamHandler() -formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") -handler.setFormatter(formatter) -logger.addHandler(handler) -logger.setLevel(logging.DEBUG) +logger = setup_logger(__name__) def replace_or_add_field(path, current_val, expected_val):