Skip to content

Commit

Permalink
style: Make cloudinit.log functions use snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Sep 21, 2023
1 parent 107aa74 commit 80783c7
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cloudinit/cmd/devel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def addLogHandlerCLI(logger, log_level):
"""Add a commandline logging handler to emit messages to stderr."""
formatter = logging.Formatter("%(levelname)s: %(message)s")
log.setupBasicLogging(log_level, formatter=formatter)
log.setup_basic_logging(log_level, formatter=formatter)
return logger


Expand Down
2 changes: 1 addition & 1 deletion cloudinit/cmd/devel/hotplug_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def handle_args(name, args):
hotplug_init = Init(ds_deps=[], reporter=hotplug_reporter)
hotplug_init.read_cfg()

log.setupLogging(hotplug_init.cfg)
log.setup_logging(hotplug_init.cfg)
if "reporting" in hotplug_init.cfg:
reporting.update_configuration(hotplug_init.cfg.get("reporting"))
# Logging isn't going to be setup until now
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/cmd/devel/net_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def handle_args(name, args):
os.makedirs(args.directory)

if args.debug:
log.setupBasicLogging(level=logging.DEBUG)
log.setup_basic_logging(level=logging.DEBUG)
else:
log.setupBasicLogging(level=logging.WARN)
log.setup_basic_logging(level=logging.WARN)
if args.mac:
known_macs = {}
for item in args.mac:
Expand Down
24 changes: 12 additions & 12 deletions cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
from cloudinit.config.modules import Modules
from cloudinit.config.schema import validate_cloudconfig_schema
from cloudinit.log import (
setupBasicLogging,
setupLogging,
resetLogging,
setup_basic_logging,
setup_logging,
reset_logging,
configure_root_logger,
)
from cloudinit.reporting import events
Expand Down Expand Up @@ -343,11 +343,11 @@ def main_init(name, args):
LOG.debug(
"Logging being reset, this logger may no longer be active shortly"
)
resetLogging()
setupLogging(init.cfg)
reset_logging()
setup_logging(init.cfg)
apply_reporting_cfg(init.cfg)

# Any log usage prior to setupLogging above did not have local user log
# Any log usage prior to setup_logging above did not have local user log
# config applied. We send the welcome message now, as stderr/out have
# been redirected and log now configured.
welcome(name, msg=w_msg)
Expand Down Expand Up @@ -504,7 +504,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!")
setupLogging(mods.cfg)
setup_logging(mods.cfg)

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

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

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

# Setup signal handlers before running
signal_handler.attach_handlers()
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_package_update_upgrade_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema, get_meta_doc
from cloudinit.distros import ALL_DISTROS
from cloudinit.log import flushLoggers
from cloudinit.log import flush_loggers
from cloudinit.settings import PER_INSTANCE

REBOOT_FILE = "/var/run/reboot-required"
Expand Down Expand Up @@ -128,7 +128,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
"Rebooting after upgrade or install per %s", REBOOT_FILE
)
# Flush the above warning + anything else out...
flushLoggers(LOG)
flush_loggers(LOG)
_fire_reboot()
except Exception as e:
util.logexc(LOG, "Requested reboot did not happen!")
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_rsyslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
if restarted:
# This only needs to run if we *actually* restarted
# syslog above.
log.resetLogging()
log.setupLogging(cloud.cfg)
log.reset_logging()
log.setup_logging(cloud.cfg)
# This should now use rsyslog if
# the logging was setup to use it...
LOG.debug("%s configured %s files", name, changes)
18 changes: 9 additions & 9 deletions cloudinit/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
DEFAULT_LOG_FORMAT = "%(asctime)s - %(filename)s[%(levelname)s]: %(message)s"


def setupBasicLogging(level=logging.DEBUG, formatter=None):
def setup_basic_logging(level=logging.DEBUG, formatter=None):
formatter = formatter or logging.Formatter(DEFAULT_LOG_FORMAT)
root = logging.getLogger()
for handler in root.handlers:
Expand All @@ -37,17 +37,17 @@ def setupBasicLogging(level=logging.DEBUG, formatter=None):
root.setLevel(level)


def flushLoggers(root):
def flush_loggers(root):
if not root:
return
for h in root.handlers:
if isinstance(h, (logging.StreamHandler)):
with suppress(IOError):
h.flush()
flushLoggers(root.parent)
flush_loggers(root.parent)


def defineDeprecationLogger(lvl=35):
def define_deprecation_logger(lvl=35):
logging.addLevelName(lvl, "DEPRECATED")

def deprecated(self, message, *args, **kwargs):
Expand All @@ -57,7 +57,7 @@ def deprecated(self, message, *args, **kwargs):
logging.Logger.deprecated = deprecated


def setupLogging(cfg=None):
def setup_logging(cfg=None):
# See if the config provides any logging conf...
if not cfg:
cfg = {}
Expand Down Expand Up @@ -109,10 +109,10 @@ def setupLogging(cfg=None):
)
if basic_enabled:
sys.stderr.write("Setting up basic logging...\n")
setupBasicLogging()
setup_basic_logging()


def resetLogging():
def reset_logging():
"""Remove all current handlers and unset log level."""
log = logging.getLogger()
handlers = list(log.handlers)
Expand Down Expand Up @@ -150,6 +150,6 @@ def configure_root_logger():

# Always format logging timestamps as UTC time
logging.Formatter.converter = time.gmtime
defineDeprecationLogger()
define_deprecation_logger()
setup_backup_logging()
resetLogging()
reset_logging()
2 changes: 1 addition & 1 deletion cloudinit/sources/DataSourceVMware.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ def main():
Executed when this file is used as a program.
"""
try:
log.setupBasicLogging()
log.setup_basic_logging()
except Exception:
pass
metadata = {
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def _initialize_filesystem(self):
log_file = util.get_cfg_option_str(self.cfg, "def_log_file")
if log_file:
# At this point the log file should have already been created
# in the setupLogging function of log.py
# in the setup_logging function of log.py

try:
fmode = util.get_permissions(log_file)
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/config/test_cc_ca_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def test_schema_validation(self, config, error_msg):
@mock.patch.object(cc_ca_certs, "update_ca_certs")
def test_deprecate_key_warnings(self, update_ca_certs, caplog):
"""Assert warnings are logged for deprecated keys."""
logger.setupLogging()
logger.setup_logging()
cloud = get_cloud("ubuntu")
cc_ca_certs.handle(
"IGNORE", {"ca-certs": {"remove-defaults": False}}, cloud, []
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def test_validateconfig_strict_metaschema_do_not_raise_exception(
def test_validateconfig_logs_deprecations(
self, schema, config, expected_msg, log_deprecations, caplog
):
log.setupLogging()
log.setup_logging()
validate_cloudconfig_schema(
config,
schema,
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/net/test_network_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_v2_warns_deprecated_gateways(
In netplan targets we perform a passthrough and the warning is not
needed.
"""
log.setupLogging()
log.setup_logging()

util.deprecate._log = set() # type: ignore
ncfg = safeyaml.load(
Expand Down
6 changes: 3 additions & 3 deletions tests/unittests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class TestCloudInitLogger(CiTestCase):
def setUp(self):
# set up a logger like cloud-init does in setupLogging, but instead
# set up a logger like cloud-init does in setup_logging, but instead
# of sys.stderr, we'll plug in a StringIO() object so we can see
# what gets logged
logging.Formatter.converter = time.gmtime
Expand Down Expand Up @@ -62,13 +62,13 @@ def test_logger_uses_gmtime(self):
class TestDeprecatedLogs:
def test_deprecated_log_level(self, caplog):
logger = logging.getLogger()
log.setupLogging()
log.setup_logging()
logger.deprecated("deprecated message")
assert "DEPRECATED" == caplog.records[0].levelname
assert "deprecated message" in caplog.text

def test_log_deduplication(self, caplog):
log.defineDeprecationLogger()
log.define_deprecation_logger()
util.deprecate(
deprecated="stuff",
deprecated_version="19.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -5454,7 +5454,7 @@ def test_from_v2_routes(self):
""" # noqa: E501
),
}
log.setupLogging()
log.setup_logging()

found = self._render_and_read(network_config=v2_data)
self._compare_files_to_expected(expected, found)
Expand Down
2 changes: 1 addition & 1 deletion tools/ccfg-merge-debug
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def main():
if args.verbose:
level = (logging.WARN, logging.INFO,
logging.DEBUG)[min(args.verbose, 2)]
logging.setupBasicLogging(level)
logging.setup_basic_logging(level)

outfile = args.output
if args.output == "-":
Expand Down

0 comments on commit 80783c7

Please sign in to comment.