Skip to content

Commit

Permalink
cloudinit.log: Standardize use of cloudinit's logging module (#4435)
Browse files Browse the repository at this point in the history
cloud-init's log module implements a wrapper for logging.getLogger,
however this module is not consistently used across the codebase. Furthermore
most of these invocations choose to alias cloudinit.log module to logging.
Shadowing a standard library module, and especially doing so inconsistently
across the codebase, is not best practice. Since this wrapper doesn't add
value, replace all invocations with the standard library call.

Also standardize logger names to use logging.getLogger(__name__).
  • Loading branch information
holmanb committed Sep 21, 2023
1 parent 461f261 commit 0044c83
Show file tree
Hide file tree
Showing 118 changed files with 218 additions and 428 deletions.
5 changes: 1 addition & 4 deletions cloudinit/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# This file is part of cloud-init. See LICENSE file for license information.

import copy
import logging
import os
from typing import Optional

from cloudinit import log as logging
from cloudinit.distros import Distro
from cloudinit.helpers import Paths, Runners
from cloudinit.reporting import events
Expand Down Expand Up @@ -103,6 +103,3 @@ def get_cpath(self, name=None):

def get_ipath(self, name=None):
return self.paths.get_ipath(name)


# vi: ts=4 expandtab
3 changes: 2 additions & 1 deletion cloudinit/cmd/devel/hotplug_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Handle reconfiguration on hotplug events."""
import abc
import argparse
import logging
import os
import sys
import time
Expand All @@ -16,7 +17,7 @@
from cloudinit.sources import DataSource, DataSourceNotFoundException
from cloudinit.stages import Init

LOG = log.getLogger(__name__)
LOG = logging.getLogger(__name__)
NAME = "hotplug-hook"


Expand Down
6 changes: 3 additions & 3 deletions cloudinit/cmd/devel/make_mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"""Generate multi-part mime messages for user-data."""

import argparse
import logging
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from cloudinit import log
from cloudinit.cmd.devel import addLogHandlerCLI
from cloudinit.handlers import INCLUSION_TYPES_MAP

NAME = "make-mime"
LOG = log.getLogger(NAME)
LOG = logging.getLogger(__name__)
EPILOG = (
"Example: make-mime -a config.yaml:cloud-config "
"-a script.sh:x-shellscript > user-data"
Expand Down Expand Up @@ -116,7 +116,7 @@ def handle_args(name, args):
@return 0 on success, 1 on failure.
"""
addLogHandlerCLI(LOG, log.DEBUG if args.debug else log.WARNING)
addLogHandlerCLI(LOG, logging.DEBUG if args.debug else logging.WARNING)
if args.list_types:
print("\n".join(get_content_types(strip_prefix=True)))
return 0
Expand Down
5 changes: 3 additions & 2 deletions cloudinit/cmd/devel/net_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Debug network config format conversions."""
import argparse
import json
import logging
import os
import sys

Expand Down Expand Up @@ -99,9 +100,9 @@ def handle_args(name, args):
os.makedirs(args.directory)

if args.debug:
log.setupBasicLogging(level=log.DEBUG)
log.setupBasicLogging(level=logging.DEBUG)
else:
log.setupBasicLogging(level=log.WARN)
log.setupBasicLogging(level=logging.WARN)
if args.mac:
known_macs = {}
for item in args.mac:
Expand Down
6 changes: 3 additions & 3 deletions cloudinit/cmd/devel/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"""Debug jinja template rendering of user-data."""

import argparse
import logging
import os
import sys

from cloudinit import log
from cloudinit.cmd.devel import addLogHandlerCLI, read_cfg_paths
from cloudinit.handlers.jinja_template import (
JinjaLoadError,
Expand All @@ -18,7 +18,7 @@

NAME = "render"

LOG = log.getLogger(NAME)
LOG = logging.getLogger(__name__)


def get_parser(parser=None):
Expand Down Expand Up @@ -61,7 +61,7 @@ def render_template(user_data_path, instance_data_path=None, debug=False):
@return 0 on success, 1 on failure.
"""
addLogHandlerCLI(LOG, log.DEBUG if debug else log.WARNING)
addLogHandlerCLI(LOG, logging.DEBUG if debug else logging.WARNING)
if instance_data_path:
instance_data_fn = instance_data_path
else:
Expand Down
38 changes: 17 additions & 21 deletions cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,30 @@
import sys
import time
import traceback
import logging
from typing import Tuple

from cloudinit import patcher
from cloudinit.config.modules import Modules

patcher.patch_logging()

from cloudinit.config.schema import validate_cloudconfig_schema
from cloudinit import log as logging
from cloudinit import netinfo
from cloudinit import signal_handler
from cloudinit import patcher
from cloudinit import sources
from cloudinit import stages
from cloudinit import url_helper
from cloudinit import util
from cloudinit import version
from cloudinit import warnings

from cloudinit import reporting
from cloudinit.reporting import events

from cloudinit.settings import PER_INSTANCE, PER_ALWAYS, PER_ONCE, CLOUD_CONFIG

from cloudinit import atomic_helper

from cloudinit.config import cc_set_hostname
from cloudinit.cmd.devel import read_cfg_paths
from cloudinit.config import cc_set_hostname
from cloudinit.config.modules import Modules
from cloudinit.config.schema import validate_cloudconfig_schema
from cloudinit.log import setupBasicLogging, setupLogging, resetLogging
from cloudinit.reporting import events
from cloudinit.settings import PER_INSTANCE, PER_ALWAYS, PER_ONCE, CLOUD_CONFIG

patcher.patch_logging()

# Welcome message template
WELCOME_MSG_TPL = (
Expand Down Expand Up @@ -349,8 +345,8 @@ def main_init(name, args):
LOG.debug(
"Logging being reset, this logger may no longer be active shortly"
)
logging.resetLogging()
logging.setupLogging(init.cfg)
resetLogging()
setupLogging(init.cfg)
apply_reporting_cfg(init.cfg)

# Any log usage prior to setupLogging above did not have local user log
Expand Down Expand Up @@ -510,7 +506,7 @@ def main_init(name, args):
(outfmt, errfmt) = util.fixup_output(mods.cfg, name)
except Exception:
util.logexc(LOG, "Failed to re-adjust output redirection!")
logging.setupLogging(mods.cfg)
setupLogging(mods.cfg)

# give the activated datasource a chance to adjust
init.activate_datasource()
Expand Down Expand Up @@ -615,8 +611,8 @@ def main_modules(action_name, args):
LOG.debug(
"Logging being reset, this logger may no longer be active shortly"
)
logging.resetLogging()
logging.setupLogging(mods.cfg)
resetLogging()
setupLogging(mods.cfg)
apply_reporting_cfg(init.cfg)

# now that logging is setup and stdout redirected, send welcome
Expand Down Expand Up @@ -677,8 +673,8 @@ def main_single(name, args):
LOG.debug(
"Logging being reset, this logger may no longer be active shortly"
)
logging.resetLogging()
logging.setupLogging(mods.cfg)
resetLogging()
setupLogging(mods.cfg)
apply_reporting_cfg(init.cfg)

# now that logging is setup and stdout redirected, send welcome
Expand Down Expand Up @@ -1049,7 +1045,7 @@ def main(sysv_args=None):
# Setup basic logging to start (until reinitialized)
# iff in debug mode.
if args.debug:
logging.setupBasicLogging()
setupBasicLogging()

# Setup signal handlers before running
signal_handler.attach_handlers()
Expand Down
7 changes: 4 additions & 3 deletions cloudinit/cmd/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"""

import argparse
import logging
import os
import sys
from errno import EACCES

from cloudinit import atomic_helper, log, util
from cloudinit import atomic_helper, util
from cloudinit.cmd.devel import addLogHandlerCLI, read_cfg_paths
from cloudinit.handlers.jinja_template import (
convert_jinja_instance_data,
Expand All @@ -30,7 +31,7 @@
from cloudinit.sources import REDACT_SENSITIVE_VALUE

NAME = "query"
LOG = log.getLogger(NAME)
LOG = logging.getLogger(__name__)


def get_parser(parser=None):
Expand Down Expand Up @@ -261,7 +262,7 @@ def _find_instance_data_leaf_by_varname_path(

def handle_args(name, args):
"""Handle calls to 'cloud-init query' as a subcommand."""
addLogHandlerCLI(LOG, log.DEBUG if args.debug else log.WARNING)
addLogHandlerCLI(LOG, logging.DEBUG if args.debug else logging.WARNING)
if not any([args.list_keys, args.varname, args.format, args.dump_all]):
LOG.error(
"Expected one of the options: --all, --format,"
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_ansible.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""ansible enables running on first boot either ansible-pull"""
import abc
import logging
import os
import re
import sys
import sysconfig
from copy import deepcopy
from logging import getLogger
from textwrap import dedent
from typing import Optional

Expand Down Expand Up @@ -61,7 +61,7 @@
}

__doc__ = get_meta_doc(meta)
LOG = getLogger(__name__)
LOG = logging.getLogger(__name__)
CFG_OVERRIDE = "ansible_config"


Expand Down
5 changes: 1 addition & 4 deletions cloudinit/config/cc_apk_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

"""Apk Configure: Configures apk repositories file."""

import logging
from textwrap import dedent

from cloudinit import log as logging
from cloudinit import temp_utils, templater, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
Expand Down Expand Up @@ -190,6 +190,3 @@ def _write_repositories_file(alpine_repo, alpine_version, local_repo):
templater.render_to_file(template_fn, repo_file, params)
# Clean up temporary template
util.del_file(template_fn)


# vi: ts=4 expandtab
7 changes: 2 additions & 5 deletions cloudinit/config/cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@

import functools
import glob
import logging
import os
import pathlib
import re
import shutil
import signal
from textwrap import dedent

from cloudinit import gpg
from cloudinit import log as logging
from cloudinit import subp, templater, util
from cloudinit import gpg, subp, templater, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema, get_meta_doc
Expand Down Expand Up @@ -1015,5 +1014,3 @@ def apt_key_list():
CONFIG_CLEANERS = {
"cloud-init": clean_cloud_init,
}

# vi: ts=4 expandtab
5 changes: 1 addition & 4 deletions cloudinit/config/cc_ca_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

"""CA Certs: Add ca certificates."""

import logging
import os
from textwrap import dedent

from cloudinit import log as logging
from cloudinit import subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
Expand Down Expand Up @@ -285,6 +285,3 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
# Update the system with the new cert configuration.
LOG.debug("Updating certificates")
update_ca_certs(distro_cfg)


# vi: ts=4 expandtab
5 changes: 1 addition & 4 deletions cloudinit/config/cc_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# This file is part of cloud-init. See LICENSE file for license information.
"""Fan: Configure ubuntu fan networking"""

import logging
from textwrap import dedent

from cloudinit import log as logging
from cloudinit import subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
Expand Down Expand Up @@ -107,6 +107,3 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
config_file=mycfg.get("config_path"),
content=mycfg.get("config"),
)


# vi: ts=4 expandtab
2 changes: 1 addition & 1 deletion cloudinit/config/cc_growpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import base64
import copy
import json
import logging
import os
import os.path
import re
Expand All @@ -20,7 +21,6 @@
from textwrap import dedent
from typing import Tuple

from cloudinit import log as logging
from cloudinit import subp, temp_utils, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
Expand Down
5 changes: 1 addition & 4 deletions cloudinit/config/cc_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

"""keyboard: set keyboard layout"""

import logging
from textwrap import dedent

from cloudinit import distros
from cloudinit import log as logging
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema, get_meta_doc
Expand Down Expand Up @@ -85,6 +85,3 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
options = kb_cfg.get("options", "")
LOG.debug("Setting keyboard layout to '%s'", layout)
cloud.distro.set_keymap(layout, model, variant, options)


# vi: ts=4 expandtab
2 changes: 1 addition & 1 deletion cloudinit/config/cc_lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

"""LXD: configure lxd with ``lxd init`` and optionally lxd-bridge"""

import logging
import os
from textwrap import dedent
from typing import List, Tuple

from cloudinit import log as logging
from cloudinit import safeyaml, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
Expand Down
Loading

0 comments on commit 0044c83

Please sign in to comment.