Skip to content

Commit

Permalink
chore: standardize our logging in python package
Browse files Browse the repository at this point in the history
  • Loading branch information
cardoe committed Jul 30, 2024
1 parent 081a57e commit dbce971
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
23 changes: 14 additions & 9 deletions python/understack-workflows/understack_workflows/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit dbce971

Please sign in to comment.