Skip to content

Commit

Permalink
logging_revamp: replace modules' logger name
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvoM committed Jun 30, 2023
1 parent c8eccb8 commit 6c04f8d
Show file tree
Hide file tree
Showing 45 changed files with 154 additions and 162 deletions.
6 changes: 3 additions & 3 deletions lib/apt_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from uaclient.config import UAConfig
from uaclient.daemon import setup_logging

uaclient_logger = logging.getLogger("uaclient")
upro_logger = logging.getLogger("ubuntu_pro")


def main(cfg: UAConfig):
Expand All @@ -28,13 +28,13 @@ def main(cfg: UAConfig):
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
logger=uaclient_logger,
logger=upro_logger,
)
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
log_file=cfg.log_file,
logger=uaclient_logger,
logger=upro_logger,
)
main(cfg)
8 changes: 4 additions & 4 deletions lib/auto_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
)
from uaclient.files import state_files

uaclient_logger = logging.getLogger("uaclient")
LOG = logging.getLogger("uaclient.lib.auto_attach")
upro_logger = logging.getLogger("ubuntu_pro")
LOG = logging.getLogger("ubuntu_pro.lib.auto_attach")

try:
import cloudinit.stages as ci_stages # type: ignore
Expand Down Expand Up @@ -108,13 +108,13 @@ def main(cfg: UAConfig):
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
logger=uaclient_logger,
logger=upro_logger,
)
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
log_file=cfg.log_file,
logger=uaclient_logger,
logger=upro_logger,
)
sys.exit(main(cfg))
6 changes: 3 additions & 3 deletions lib/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
setup_logging,
)

LOG = logging.getLogger("uaclient.lib.daemon")
uaclient_logger = logging.getLogger("uaclient")
LOG = logging.getLogger("ubuntu_pro.lib.daemon")
upro_logger = logging.getLogger("ubuntu_pro")


def main() -> int:
Expand All @@ -36,7 +36,7 @@ def main() -> int:
logging.CRITICAL,
logging.ERROR,
log_file=cfg.daemon_log_file,
logger=uaclient_logger,
logger=upro_logger,
)

LOG.debug("daemon starting")
Expand Down
8 changes: 4 additions & 4 deletions lib/esm_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from uaclient.config import UAConfig
from uaclient.daemon import setup_logging

LOG = logging.getLogger("uaclient.lib.esm_cache")
uaclient_logger = logging.getLogger("uaclient")
LOG = logging.getLogger("ubuntu_pro.lib.esm_cache")
upro_logger = logging.getLogger("ubuntu_pro")


def main(cfg: UAConfig) -> None:
Expand All @@ -24,13 +24,13 @@ def main(cfg: UAConfig) -> None:
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
logger=uaclient_logger,
logger=upro_logger,
)
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
log_file=cfg.log_file,
logger=uaclient_logger,
logger=upro_logger,
)
main(cfg)
2 changes: 1 addition & 1 deletion lib/patch_status_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from uaclient import system, util
from uaclient.cli import setup_logging

LOG = logging.getLogger("uaclient.lib.patch_status_json")
LOG = logging.getLogger("ubuntu_pro.lib.patch_status_json")


def patch_status_json_schema_0_1(status_file: str):
Expand Down
2 changes: 1 addition & 1 deletion lib/reboot_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from uaclient.entitlements.fips import FIPSEntitlement
from uaclient.files import notices, state_files

LOG = logging.getLogger("uaclient.lib.reboot_cmds")
LOG = logging.getLogger("ubuntu_pro.lib.reboot_cmds")


def fix_pro_pkg_holds(cfg: config.UAConfig):
Expand Down
2 changes: 1 addition & 1 deletion lib/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from uaclient.timer.update_contract_info import update_contract_info
from uaclient.timer.update_messaging import update_motd_messages

LOG = logging.getLogger("uaclient.lib.timer")
LOG = logging.getLogger("ubuntu_pro.lib.timer")
UPDATE_MESSAGING_INTERVAL = 21600 # 6 hours
METERING_INTERVAL = 14400 # 4 hours
UPDATE_CONTRACT_INFO_INTERVAL = 86400 # 24 hours
Expand Down
4 changes: 2 additions & 2 deletions uaclient/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from uaclient.files.state_files import timer_jobs_state_file

LOG = logging.getLogger(__name__)
LOG = logging.getLogger(pro_log.replace_top_level_logger_name(__name__))


UA_SERVICES = (
Expand Down Expand Up @@ -231,7 +231,7 @@ def collect_logs(cfg: config.UAConfig, output_dir: str):
content,
)
except Exception as e:
logging.warning(
LOG.warning(
"Failed to collect user log file: %s\n%s", log_file, str(e)
)

Expand Down
2 changes: 1 addition & 1 deletion uaclient/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging

# setup null handler for all API endpoints
logging.getLogger("uaclient").addHandler(logging.NullHandler())
logging.getLogger("ubuntu_pro").addHandler(logging.NullHandler())
4 changes: 2 additions & 2 deletions uaclient/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import apt # type: ignore
import apt_pkg # type: ignore

from uaclient import event_logger, exceptions, gpg, messages, system
from uaclient import event_logger, exceptions, gpg, log, messages, system
from uaclient.defaults import ESM_APT_ROOTDIR

APT_HELPER_TIMEOUT = 60.0 # 60 second timeout used for apt-helper call
Expand Down Expand Up @@ -56,7 +56,7 @@
APT_RETRIES = [1.0, 5.0, 10.0]

event = event_logger.get_event_logger()
LOG = logging.getLogger(__name__)
LOG = logging.getLogger(log.replace_top_level_logger_name(__name__))


@enum.unique
Expand Down
4 changes: 2 additions & 2 deletions uaclient/apt_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import apt_pkg

from uaclient import defaults, messages, system, util
from uaclient import defaults, log, messages, system, util
from uaclient.api.u.pro.status.is_attached.v1 import _is_attached
from uaclient.clouds.identity import get_cloud_type
from uaclient.config import UAConfig
Expand All @@ -25,7 +25,7 @@
get_contract_expiry_status,
)

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


class AptNewsMessageSelectors(DataObject):
Expand Down
20 changes: 10 additions & 10 deletions uaclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
)

event = event_logger.get_event_logger()
uaclient_logger = logging.getLogger("uaclient")
LOG = logging.getLogger(__name__)
upro_logger = logging.getLogger("ubuntu_pro")
LOG = logging.getLogger(pro_log.replace_top_level_logger_name(__name__))


class UAArgumentParser(argparse.ArgumentParser):
Expand Down Expand Up @@ -1876,7 +1876,7 @@ def _warn_about_output_redirection(cmd_args) -> None:
):
if hasattr(cmd_args, "format") and cmd_args.format in ("json", "yaml"):
return
logging.warning(
LOG.warning(
messages.WARNING_HUMAN_READABLE_OUTPUT.format(
command=cmd_args.command
)
Expand All @@ -1901,18 +1901,18 @@ def setup_logging(console_level, log_level, log_file=None, logger=None):
log_level = log_level.upper()

console_formatter = util.LogFormatter()
uaclient_logger.setLevel(log_level)
uaclient_logger.addFilter(pro_log.RedactionFilter())
upro_logger.setLevel(log_level)
upro_logger.addFilter(pro_log.RedactionFilter())

# Clear all handlers, so they are replaced for this logger
uaclient_logger.handlers = []
upro_logger.handlers = []

# Setup console logging
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setFormatter(console_formatter)
console_handler.setLevel(console_level)
console_handler.set_name("ua-console") # Used to disable console logging
uaclient_logger.addHandler(console_handler)
console_handler.set_name("upro-console") # Used to disable console logging
upro_logger.addHandler(console_handler)

log_file_path = pathlib.Path(log_file)

Expand All @@ -1924,8 +1924,8 @@ def setup_logging(console_level, log_level, log_file=None, logger=None):
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(JsonArrayFormatter())
file_handler.setLevel(log_level)
file_handler.set_name("ua-file")
uaclient_logger.addHandler(file_handler)
file_handler.set_name("upro-file")
upro_logger.addHandler(file_handler)


def set_event_mode(cmd_args):
Expand Down
4 changes: 2 additions & 2 deletions uaclient/clouds/aws.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from typing import Any, Dict

from uaclient import exceptions, http, system, util
from uaclient import exceptions, http, log, system, util
from uaclient.clouds import AutoAttachCloudInstance

IMDS_IPV4_ADDRESS = "169.254.169.254"
Expand All @@ -19,7 +19,7 @@
AWS_TOKEN_PUT_HEADER = "X-aws-ec2-metadata-token"
AWS_TOKEN_REQ_HEADER = AWS_TOKEN_PUT_HEADER + "-ttl-seconds"

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


class UAAutoAttachAWSInstance(AutoAttachCloudInstance):
Expand Down
4 changes: 2 additions & 2 deletions uaclient/clouds/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from functools import lru_cache
from typing import Dict, Optional, Tuple, Type # noqa: F401

from uaclient import clouds, exceptions, system
from uaclient import clouds, exceptions, log, system
from uaclient.config import apply_config_settings_override

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

CLOUD_TYPE_TO_TITLE = {
"aws": "AWS",
Expand Down
3 changes: 2 additions & 1 deletion uaclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
exceptions,
files,
http,
log,
messages,
snap,
system,
Expand All @@ -31,7 +32,7 @@
from uaclient.files.notices import Notice
from uaclient.yaml import safe_load

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

PRIVATE_SUBDIR = "private"
MERGE_ID_KEY_MAP = {
Expand Down
12 changes: 6 additions & 6 deletions uaclient/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _func():

except LookupError:
# If the caplog fixture isn't available, shim something in ourselves
root = logging.getLogger("uaclient")
root = logging.getLogger("ubuntu_pro")
root.propagate = False
root.setLevel(log_level)
handler = logging.StreamHandler(io.StringIO())
Expand All @@ -135,13 +135,13 @@ def clear_handlers():
def logging_sandbox():
# Monkeypatch a replacement root logger, so that our changes to logging
# configuration don't persist outside of the test
uaclient_logger = logging.getLogger("uaclient")
uaclient_logger.setLevel(logging.WARNING)
upro_logger = logging.getLogger("ubuntu_pro")
upro_logger.setLevel(logging.WARNING)

with mock.patch.object(logging, "root", uaclient_logger):
with mock.patch.object(logging.Logger, "root", uaclient_logger):
with mock.patch.object(logging, "root", upro_logger):
with mock.patch.object(logging.Logger, "root", upro_logger):
with mock.patch.object(
logging.Logger, "manager", logging.Manager(uaclient_logger)
logging.Logger, "manager", logging.Manager(upro_logger)
):
yield

Expand Down
3 changes: 2 additions & 1 deletion uaclient/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
event_logger,
exceptions,
http,
log,
messages,
system,
util,
Expand Down Expand Up @@ -48,7 +49,7 @@
}

event = event_logger.get_event_logger()
LOG = logging.getLogger(__name__)
LOG = logging.getLogger(log.replace_top_level_logger_name(__name__))


class UAContractClient(serviceclient.UAServiceClient):
Expand Down
6 changes: 3 additions & 3 deletions uaclient/daemon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from uaclient.defaults import DEFAULT_DATA_DIR
from uaclient.log import JsonArrayFormatter

LOG = logging.getLogger(__name__)
LOG = logging.getLogger(pro_log.replace_top_level_logger_name(__name__))

AUTO_ATTACH_STATUS_MOTD_FILE = os.path.join(
DEFAULT_DATA_DIR, "messages", "motd-auto-attach-status"
Expand Down Expand Up @@ -50,11 +50,11 @@ def setup_logging(console_level, log_level, log_file, logger):
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setFormatter(logging.Formatter("%(message)s"))
console_handler.setLevel(console_level)
console_handler.set_name("ua-console")
console_handler.set_name("upro-console")
logger.addHandler(console_handler)

file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(JsonArrayFormatter())
file_handler.setLevel(log_level)
file_handler.set_name("ua-file")
file_handler.set_name("upro-file")
logger.addHandler(file_handler)
4 changes: 2 additions & 2 deletions uaclient/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from enum import Enum
from typing import Any, List, Optional, Type, TypeVar, Union

from uaclient import exceptions, messages, util
from uaclient import exceptions, log, messages, util

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


class IncorrectTypeError(exceptions.UserFacingError):
Expand Down
12 changes: 10 additions & 2 deletions uaclient/entitlements/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
from datetime import datetime
from typing import Any, Dict, List, Optional, Set, Tuple, Type, Union

from uaclient import config, contract, event_logger, messages, system, util
from uaclient import (
config,
contract,
event_logger,
log,
messages,
system,
util,
)
from uaclient.api.u.pro.status.is_attached.v1 import _is_attached
from uaclient.defaults import DEFAULT_HELP_FILE
from uaclient.entitlements.entitlement_status import (
Expand All @@ -24,7 +32,7 @@
from uaclient.yaml import safe_load

event = event_logger.get_event_logger()
LOG = logging.getLogger(__name__)
LOG = logging.getLogger(log.replace_top_level_logger_name(__name__))


class IncompatibleService:
Expand Down
Loading

0 comments on commit 6c04f8d

Please sign in to comment.