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 Jul 25, 2023
1 parent d91c289 commit 27bde4f
Show file tree
Hide file tree
Showing 54 changed files with 159 additions and 191 deletions.
6 changes: 1 addition & 5 deletions lib/apt_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from uaclient.config import UAConfig
from uaclient.daemon import setup_logging

uaclient_logger = logging.getLogger("uaclient")


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

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

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

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


def main() -> int:
Expand All @@ -31,12 +30,10 @@ def main() -> int:
# Make sure the ua-daemon logger does not generate double logging
# by propagating to the root logger
# The root logger should only log errors to the daemon log file
# TODO: is this okay? root_logger("uaclient")
setup_logging(
logging.CRITICAL,
logging.ERROR,
log_file=cfg.daemon_log_file,
logger=uaclient_logger,
cfg.daemon_log_file,
)

LOG.debug("daemon starting")
Expand Down
7 changes: 2 additions & 5 deletions lib/esm_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
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("ubuntupro.lib.esm_cache")


def main(cfg: UAConfig) -> None:
Expand All @@ -24,13 +23,11 @@ def main(cfg: UAConfig) -> None:
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
logger=uaclient_logger,
)
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
log_file=cfg.log_file,
logger=uaclient_logger,
cfg.log_file,
)
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("ubuntupro.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("ubuntupro.lib.reboot_cmds")


def fix_pro_pkg_holds(cfg: config.UAConfig):
Expand Down
6 changes: 5 additions & 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("ubuntupro.lib.timer")
UPDATE_MESSAGING_INTERVAL = 21600 # 6 hours
METERING_INTERVAL = 14400 # 4 hours
UPDATE_CONTRACT_INFO_INTERVAL = 86400 # 24 hours
Expand Down Expand Up @@ -183,6 +183,7 @@ def run_jobs(cfg: UAConfig, current_time: datetime):
logging.CRITICAL,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["timer_log_file"],
logger=LOG,
)
cfg = UAConfig()
current_time = datetime.now(timezone.utc)
Expand All @@ -192,7 +193,10 @@ def run_jobs(cfg: UAConfig, current_time: datetime):
logging.CRITICAL,
logging.DEBUG,
log_file=cfg.timer_log_file,
logger=LOG,
)
# Make sure the ua-timer logger does not generate double logging
LOG.propagate = False
# The root logger should log any error to the timer log file
setup_logging(logging.CRITICAL, logging.ERROR, log_file=cfg.timer_log_file)

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(util.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("ubuntupro").addHandler(logging.NullHandler())
1 change: 0 additions & 1 deletion uaclient/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
def call_api(
endpoint_path: str, options: List[str], cfg: UAConfig
) -> APIResponse:
# setup null handler for all API endpoints

if endpoint_path not in VALID_ENDPOINTS:
return error_out(
Expand Down
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, messages, system, util
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(util.replace_top_level_logger_name(__name__))


@enum.unique
Expand Down
6 changes: 3 additions & 3 deletions uaclient/apt_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
get_contract_expiry_status,
)

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


class AptNewsMessageSelectors(DataObject):
Expand Down Expand Up @@ -169,7 +169,7 @@ def fetch_aptnews_json(cfg: UAConfig):
def fetch_and_process_apt_news(cfg: UAConfig) -> Optional[str]:
news_dict = fetch_aptnews_json(cfg)
msg = select_message(cfg, news_dict.get("messages", []))
logging.debug("using msg: %r", msg)
LOG.debug("using msg: %r", msg)
if msg is not None:
return "\n".join(msg.lines)
return None
Expand Down Expand Up @@ -227,6 +227,6 @@ def update_apt_news(cfg: UAConfig):
state_files.apt_news_contents_file.delete()
state_files.apt_news_raw_file.delete()
except Exception as e:
logging.debug("something went wrong while processing apt_news: %r", e)
LOG.debug("something went wrong while processing apt_news: %r", e)
state_files.apt_news_contents_file.delete()
state_files.apt_news_raw_file.delete()
21 changes: 11 additions & 10 deletions uaclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
)

event = event_logger.get_event_logger()
uaclient_logger = logging.getLogger("uaclient")
LOG = logging.getLogger(__name__)
LOG = logging.getLogger(util.replace_top_level_logger_name(__name__))


class UAArgumentParser(argparse.ArgumentParser):
Expand Down Expand Up @@ -1876,7 +1875,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 +1900,20 @@ 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())
if not logger:
logger = logging.getLogger("ubuntupro")
logger.setLevel(log_level)
logger.addFilter(pro_log.RedactionFilter())

# Clear all handlers, so they are replaced for this logger
uaclient_logger.handlers = []
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
logger.addHandler(console_handler)

log_file_path = pathlib.Path(log_file)

Expand All @@ -1924,8 +1925,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")
logger.addHandler(file_handler)


def set_event_mode(cmd_args):
Expand Down
2 changes: 1 addition & 1 deletion uaclient/clouds/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(util.replace_top_level_logger_name(__name__))


class UAAutoAttachAWSInstance(AutoAttachCloudInstance):
Expand Down
2 changes: 1 addition & 1 deletion uaclient/clouds/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uaclient import exceptions, http, system, util
from uaclient.clouds import AutoAttachCloudInstance

LOG = logging.getLogger("pro.clouds.azure")
LOG = logging.getLogger("ubuntupro.clouds.azure")

IMDS_BASE_URL = "http://169.254.169.254/metadata/"

Expand Down
2 changes: 1 addition & 1 deletion uaclient/clouds/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from uaclient import exceptions, http, messages, system, util
from uaclient.clouds import AutoAttachCloudInstance

LOG = logging.getLogger("pro.clouds.gcp")
LOG = logging.getLogger("ubuntupro.clouds.gcp")

TOKEN_URL = (
"http://metadata/computeMetadata/v1/instance/service-accounts/"
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, system, util
from uaclient.config import apply_config_settings_override

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

CLOUD_TYPE_TO_TITLE = {
"aws": "AWS",
Expand Down
2 changes: 1 addition & 1 deletion uaclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from uaclient.files.notices import Notice
from uaclient.yaml import safe_load

LOG = logging.getLogger(__name__)
LOG = logging.getLogger(util.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("ubuntupro")
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("ubuntupro")
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: 1 addition & 2 deletions uaclient/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
}

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


class UAContractClient(serviceclient.UAServiceClient):
Expand Down Expand Up @@ -230,7 +230,6 @@ def update_activity_token(self):
# a full `activityInfo` object which belongs at the root of
# `machine-token.json`
if response.json_dict:

machine_token = self.cfg.machine_token
# The activity information received as a response here
# will not provide the information inside an activityInfo
Expand Down
Loading

0 comments on commit 27bde4f

Please sign in to comment.