Skip to content

Commit

Permalink
refactor: Import log module rather than functions (canonical#5074)
Browse files Browse the repository at this point in the history
Mocking doesn't work when we import functions directly.
  • Loading branch information
TheRealFalcon authored and blackboxsw committed Apr 3, 2024
1 parent 70dd183 commit 08739b9
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@
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 (
LogExporter,
setup_basic_logging,
setup_logging,
reset_logging,
configure_root_logger,
DEPRECATED,
)
from cloudinit import log
from cloudinit.reporting import events
from cloudinit.safeyaml import load
from cloudinit.settings import PER_INSTANCE, PER_ALWAYS, PER_ONCE, CLOUD_CONFIG
Expand Down Expand Up @@ -223,7 +216,7 @@ def attempt_cmdline_url(path, network=True, cmdline=None) -> Tuple[int, str]:
if is_cloud_cfg:
if cmdline_name == "url":
return (
DEPRECATED,
log.DEPRECATED,
str(
util.deprecate(
deprecated="The kernel command line key `url`",
Expand Down Expand Up @@ -348,8 +341,8 @@ def main_init(name, args):
LOG.debug(
"Logging being reset, this logger may no longer be active shortly"
)
reset_logging()
setup_logging(init.cfg)
log.reset_logging()
log.setup_logging(init.cfg)
apply_reporting_cfg(init.cfg)

# Any log usage prior to setup_logging above did not have local user log
Expand Down Expand Up @@ -510,7 +503,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!")
setup_logging(mods.cfg)
log.setup_logging(mods.cfg)

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

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

# now that logging is setup and stdout redirected, send welcome
Expand Down Expand Up @@ -768,7 +761,7 @@ def status_wrapper(name, args, data_d=None, link_d=None):
v1["stage"] = mode
v1[mode]["start"] = time.time()
v1[mode]["recoverable_errors"] = next(
filter(lambda h: isinstance(h, LogExporter), root_logger.handlers)
filter(lambda h: isinstance(h, log.LogExporter), root_logger.handlers)
).export_logs()

# Write status.json prior to running init / module code
Expand Down Expand Up @@ -798,7 +791,7 @@ def status_wrapper(name, args, data_d=None, link_d=None):

# Write status.json after running init / module code
v1[mode]["recoverable_errors"] = next(
filter(lambda h: isinstance(h, LogExporter), root_logger.handlers)
filter(lambda h: isinstance(h, log.LogExporter), root_logger.handlers)
).export_logs()
atomic_helper.write_json(status_path, status)

Expand Down Expand Up @@ -856,7 +849,7 @@ def main_features(name, args):


def main(sysv_args=None):
configure_root_logger()
log.configure_root_logger()
if not sysv_args:
sysv_args = sys.argv
parser = argparse.ArgumentParser(prog=sysv_args.pop(0))
Expand Down Expand Up @@ -1080,9 +1073,11 @@ def main(sysv_args=None):
# - if --debug is passed, logging.DEBUG
# - if --debug is not passed, logging.WARNING
if name not in ("init", "modules"):
setup_basic_logging(logging.DEBUG if args.debug else logging.WARNING)
log.setup_basic_logging(
logging.DEBUG if args.debug else logging.WARNING
)
elif args.debug:
setup_basic_logging()
log.setup_basic_logging()

# Setup signal handlers before running
signal_handler.attach_handlers()
Expand Down

0 comments on commit 08739b9

Please sign in to comment.