Skip to content

Commit

Permalink
logging: use journald logging for all systemd services
Browse files Browse the repository at this point in the history
  • Loading branch information
orndorffgrant committed Oct 6, 2023
1 parent 0b8a6c9 commit ffef1aa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 81 deletions.
15 changes: 2 additions & 13 deletions lib/apt_news.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/usr/bin/python3

import logging
from datetime import datetime, timedelta, timezone

from uaclient import apt, defaults
from uaclient import apt, log
from uaclient.apt_news import update_apt_news
from uaclient.config import UAConfig
from uaclient.daemon import setup_logging


def main(cfg: UAConfig):
Expand All @@ -22,15 +20,6 @@ def main(cfg: UAConfig):


if __name__ == "__main__":
setup_logging(
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
)
log.setup_journald_logging()
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
cfg.log_file,
)
main(cfg)
19 changes: 3 additions & 16 deletions lib/auto_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import logging
import sys

from uaclient import defaults, http, messages, system
from uaclient import http, log, messages, system
from uaclient.api.exceptions import (
AlreadyAttachedError,
AutoAttachDisabledError,
Expand All @@ -24,11 +24,7 @@
full_auto_attach,
)
from uaclient.config import UAConfig
from uaclient.daemon import (
AUTO_ATTACH_STATUS_MOTD_FILE,
retry_auto_attach,
setup_logging,
)
from uaclient.daemon import AUTO_ATTACH_STATUS_MOTD_FILE, retry_auto_attach
from uaclient.files import state_files

LOG = logging.getLogger("ubuntupro.lib.auto_attach")
Expand Down Expand Up @@ -103,16 +99,7 @@ def main(cfg: UAConfig):


if __name__ == "__main__":
setup_logging(
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
)
log.setup_journald_logging()
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
cfg.log_file,
)
http.configure_web_proxy(cfg.http_proxy, cfg.https_proxy)
sys.exit(main(cfg))
14 changes: 2 additions & 12 deletions lib/esm_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import logging

from uaclient import defaults
from uaclient import log
from uaclient.apt import update_esm_caches
from uaclient.config import UAConfig
from uaclient.daemon import setup_logging

LOG = logging.getLogger("ubuntupro.lib.esm_cache")

Expand All @@ -19,15 +18,6 @@ def main(cfg: UAConfig) -> None:


if __name__ == "__main__":
setup_logging(
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
)
log.setup_journald_logging()
cfg = UAConfig()
setup_logging(
logging.INFO,
logging.DEBUG,
cfg.log_file,
)
main(cfg)
21 changes: 8 additions & 13 deletions lib/reboot_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
from uaclient import (
config,
contract,
defaults,
exceptions,
http,
lock,
log,
upgrade_lts_contract,
)
from uaclient.api.u.pro.status.is_attached.v1 import _is_attached
from uaclient.cli import setup_logging
from uaclient.entitlements.fips import FIPSEntitlement
from uaclient.files import notices, state_files

Expand All @@ -47,11 +46,11 @@ def fix_pro_pkg_holds(cfg: config.UAConfig):
# fips was not enabled, don't do anything
return

LOG.debug("Attempting to remove Ubuntu Pro FIPS package holds")
LOG.info("Attempting to remove Ubuntu Pro FIPS package holds")
fips = FIPSEntitlement(cfg)
try:
fips.setup_apt_config() # Removes package holds
LOG.debug("Successfully removed Ubuntu Pro FIPS package holds")
LOG.info("Successfully removed Ubuntu Pro FIPS package holds")
except Exception as e:
LOG.error(e)
LOG.warning("Could not remove Ubuntu Pro FIPS package holds")
Expand All @@ -73,17 +72,17 @@ def refresh_contract(cfg: config.UAConfig):

def main(cfg: config.UAConfig) -> int:
if not state_files.reboot_cmd_marker_file.is_present:
LOG.debug("Skipping reboot_cmds. Marker file not present")
LOG.info("Skipping reboot_cmds. Marker file not present")
notices.remove(notices.Notice.REBOOT_SCRIPT_FAILED)
return 0

if not _is_attached(cfg).is_attached:
LOG.debug("Skipping reboot_cmds. Machine is unattached")
LOG.info("Skipping reboot_cmds. Machine is unattached")
state_files.reboot_cmd_marker_file.delete()
notices.remove(notices.Notice.REBOOT_SCRIPT_FAILED)
return 0

LOG.debug("Running reboot commands...")
LOG.info("Running reboot commands...")
try:
with lock.SpinLock(cfg=cfg, lock_holder="pro-reboot-cmds"):
fix_pro_pkg_holds(cfg)
Expand All @@ -108,16 +107,12 @@ def main(cfg: config.UAConfig) -> int:
notices.add(notices.Notice.REBOOT_SCRIPT_FAILED)
return 1

LOG.debug("Successfully ran all commands on reboot.")
LOG.info("Successfully ran all commands on reboot.")
return 0


if __name__ == "__main__":
setup_logging(
logging.DEBUG,
defaults.CONFIG_DEFAULTS["log_file"],
)
log.setup_journald_logging()
cfg = config.UAConfig()
setup_logging(logging.DEBUG, log_file=cfg.log_file)
http.configure_web_proxy(cfg.http_proxy, cfg.https_proxy)
sys.exit(main(cfg=cfg))
28 changes: 1 addition & 27 deletions uaclient/daemon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import logging
import os
import sys
from subprocess import TimeoutExpired

from uaclient import exceptions
from uaclient import log as pro_log
from uaclient import system, util
from uaclient import exceptions, system, util
from uaclient.config import UAConfig
from uaclient.defaults import DEFAULT_DATA_DIR
from uaclient.log import JsonArrayFormatter

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

Expand Down Expand Up @@ -39,25 +35,3 @@ def cleanup(cfg: UAConfig):
from uaclient.daemon import retry_auto_attach

retry_auto_attach.cleanup(cfg)


def setup_logging(console_level, log_level, log_file, logger=None):
if logger is None:
logger = logging.getLogger("ubuntupro")

logger.setLevel(log_level)

logger.handlers = []
logger.addFilter(pro_log.RedactionFilter())

console_handler = logging.StreamHandler(sys.stderr)
console_handler.setFormatter(logging.Formatter("%(message)s"))
console_handler.setLevel(console_level)
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("upro-file")
logger.addHandler(file_handler)

0 comments on commit ffef1aa

Please sign in to comment.