Skip to content

Commit

Permalink
logging_revamp: setup_logging configures uaclient logger only
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvoM committed Jul 24, 2023
1 parent 8819325 commit d91c289
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/reboot_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def main(cfg: config.UAConfig) -> int:
return 0

if not _is_attached(cfg).is_attached:
logging.debug("Skipping reboot_cmds. Machine is unattached")
LOG.debug("Skipping reboot_cmds. Machine is unattached")
state_files.reboot_cmd_marker_file.delete()
notices.remove(notices.Notice.REBOOT_SCRIPT_FAILED)
return 0
Expand Down
2 changes: 0 additions & 2 deletions lib/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ 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 @@ -193,7 +192,6 @@ def run_jobs(cfg: UAConfig, current_time: datetime):
logging.CRITICAL,
logging.DEBUG,
log_file=cfg.timer_log_file,
logger=LOG,
)
# 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
20 changes: 9 additions & 11 deletions uaclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,7 @@ def _warn_about_output_redirection(cmd_args) -> None:
def setup_logging(console_level, log_level, log_file=None, logger=None):
"""Setup console logging and debug logging to log_file
It configures the pro client logger.
If run as non_root and cfg.log_file is provided, it is replaced
with another non-root log file.
"""
Expand All @@ -1895,24 +1896,23 @@ def setup_logging(console_level, log_level, log_file=None, logger=None):
# if we are running as non-root, change log file
if not util.we_are_currently_root():
log_file = pro_log.get_user_log_file()

if isinstance(log_level, str):
log_level = log_level.upper()

console_formatter = util.LogFormatter()
if logger is None:
# Then we configure the root logger
logger = uaclient_logger
logger.setLevel(log_level)
logger.addFilter(pro_log.RedactionFilter())
uaclient_logger.setLevel(log_level)
uaclient_logger.addFilter(pro_log.RedactionFilter())

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

log_file_path = pathlib.Path(log_file)

Expand All @@ -1925,7 +1925,7 @@ def setup_logging(console_level, log_level, log_file=None, logger=None):
file_handler.setFormatter(JsonArrayFormatter())
file_handler.setLevel(log_level)
file_handler.set_name("ua-file")
logger.addHandler(file_handler)
uaclient_logger.addHandler(file_handler)


def set_event_mode(cmd_args):
Expand Down Expand Up @@ -2052,9 +2052,7 @@ def main(sys_argv=None):
for k, v in sorted(util.get_pro_environment().items())
]
if pro_environment:
logging.debug(
"Executed with environment variables: %r" % pro_environment
)
LOG.debug("Executed with environment variables: %r" % pro_environment)

_warn_about_output_redirection(args)

Expand Down
4 changes: 2 additions & 2 deletions uaclient/livepatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def on_supported_kernel() -> LivepatchSupport:
lp_api_kernel_ver, kernel_info.flavor, arch, codename
)
if is_cache_valid:
logging.debug("using livepatch support cache")
LOG.debug("using livepatch support cache")
if cache_says is None:
return LivepatchSupport.UNKNOWN
if cache_says:
Expand All @@ -360,7 +360,7 @@ def on_supported_kernel() -> LivepatchSupport:
return LivepatchSupport.UNSUPPORTED

# finally check api
logging.debug("using livepatch support api")
LOG.debug("using livepatch support api")
api_says = _on_supported_kernel_api(
lp_api_kernel_ver,
kernel_info.flavor,
Expand Down
4 changes: 2 additions & 2 deletions uaclient/upgrade_lts_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@


def process_contract_delta_after_apt_lock(cfg: UAConfig) -> None:
logging.debug("Check whether to upgrade-lts-contract")
LOG.debug("Check whether to upgrade-lts-contract")
if not _is_attached(cfg).is_attached:
logging.debug("Skipping upgrade-lts-contract. Machine is unattached")
LOG.debug("Skipping upgrade-lts-contract. Machine is unattached")
return
out, _err = system.subp(["lsof", "/var/lib/apt/lists/lock"], rcs=[0, 1])
msg = "Starting upgrade-lts-contract."
Expand Down
8 changes: 5 additions & 3 deletions uaclient/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ def disable_log_to_console():
(Note that the @contextmanager decorator also allows this function to be
used as a decorator.)
"""
uaclient_logger = logging.getLogger("uaclient")
log_handlers = uaclient_logger.handlers
if not log_handlers and uaclient_logger.parent:
log_handlers = uaclient_logger.parent.handlers
potential_handlers = [
handler
for handler in logging.getLogger("uaclient").handlers
if handler.name == "ua-console"
handler for handler in log_handlers if handler.name == "ua-console"
]
if not potential_handlers:
# We didn't find a handler, so execute the body as normal then end
Expand Down

0 comments on commit d91c289

Please sign in to comment.