Skip to content

Commit

Permalink
logging_revamp: add journald logging for daemon and timer,to replace …
Browse files Browse the repository at this point in the history
…added log files.
  • Loading branch information
CalvoM authored and orndorffgrant committed Aug 25, 2023
1 parent 881f976 commit 0bfc766
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
42 changes: 11 additions & 31 deletions lib/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,27 @@
from systemd.daemon import notify # type: ignore

from uaclient import defaults, http
from systemd import journal # type: ignore
from uaclient.config import UAConfig
from uaclient.daemon import (
poll_for_pro_license,
retry_auto_attach,
setup_logging,
)
from uaclient.daemon import poll_for_pro_license, retry_auto_attach

LOG = logging.getLogger("ubuntupro.lib.daemon")
LOG = logging.getLogger("ubuntupro.daemon")
root_logger = logging.getLogger("ubuntupro")
LOG.addHandler(journal.JournalHandler(SYSLOG_IDENTIFIER="ubuntu-pro-client"))
root_logger.addHandler(
journal.JournalHandler(SYSLOG_IDENTIFIER="ubuntu-pro-client")
)


def main() -> int:
setup_logging(
logging.INFO,
logging.DEBUG,
defaults.CONFIG_DEFAULTS["daemon_log_file"],
logger=LOG,
)
LOG.setLevel(logging.DEBUG)
cfg = UAConfig()
setup_logging(
logging.INFO, logging.DEBUG, log_file=cfg.daemon_log_file, logger=LOG
)
# used with loggers in uaclient.daemon
daemon_logger = logging.getLogger("ubuntupro.daemon")
setup_logging(
logging.INFO,
logging.DEBUG,
log_file=cfg.daemon_log_file,
logger=daemon_logger,
)
LOG.setLevel(logging.DEBUG)
# The ua-daemon logger should log everything to its file
# Make sure the ua-daemon logger does not generate double logging
# by propagating to the root logger
LOG.propagate = False
daemon_logger.propagate = False
# The root logger should only log errors to the daemon log file
setup_logging(
logging.CRITICAL,
logging.ERROR,
cfg.daemon_log_file,
)
http.configure_web_proxy(cfg.http_proxy, cfg.https_proxy)
root_logger.setLevel(logging.ERROR)

LOG.debug("daemon starting")

Expand Down
25 changes: 12 additions & 13 deletions lib/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

from uaclient import defaults, http
from uaclient.cli import setup_logging

from systemd import journal # type: ignore

from uaclient.config import UAConfig
from uaclient.exceptions import InvalidFileFormatError
from uaclient.files.state_files import (
Expand All @@ -19,7 +22,12 @@
from uaclient.timer.update_contract_info import update_contract_info
from uaclient.timer.update_messaging import update_motd_messages

LOG = logging.getLogger("ubuntupro.lib.timer")
LOG = logging.getLogger("ubuntupro.timer")
root_logger = logging.getLogger("ubuntupro")
LOG.addHandler(journal.JournalHandler(SYSLOG_IDENTIFIER="ubuntu-pro-client"))
root_logger.addHandler(
journal.JournalHandler(SYSLOG_IDENTIFIER="ubuntu-pro-client")
)
UPDATE_MESSAGING_INTERVAL = 21600 # 6 hours
METERING_INTERVAL = 14400 # 4 hours
UPDATE_CONTRACT_INFO_INTERVAL = 86400 # 24 hours
Expand Down Expand Up @@ -180,24 +188,15 @@ def run_jobs(cfg: UAConfig, current_time: datetime):


if __name__ == "__main__":
setup_logging(
logging.DEBUG,
defaults.CONFIG_DEFAULTS["timer_log_file"],
logger=LOG,
)
LOG.setLevel(logging.DEBUG)
cfg = UAConfig()
current_time = datetime.now(timezone.utc)
LOG.setLevel(logging.DEBUG)

# The ua-timer logger should log everything to its file
setup_logging(
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.ERROR, log_file=cfg.timer_log_file)
http.configure_web_proxy(cfg.http_proxy, cfg.https_proxy)
root_logger.setLevel(logging.ERROR)

run_jobs(cfg=cfg, current_time=current_time)
5 changes: 5 additions & 0 deletions uaclient/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
# Those need to be mocked here, before importing our modules, so the pytest
# virtualenv doesn't cry because it can't find the modules
m_apt_pkg = mock.MagicMock()
m_systemd_pkg = mock.MagicMock()
m_systemd_pkg.journal = mock.MagicMock()
m_systemd_pkg.journal.JournalHandler = mock.MagicMock()
m_systemd_pkg.journal.JournalHandler.return_value.level = logging.DEBUG
sys.modules["apt"] = mock.MagicMock()
sys.modules["apt_pkg"] = m_apt_pkg
sys.modules["systemd"] = m_systemd_pkg

# Useless try/except to make flake8 happy \_("/)_/
try:
Expand Down

0 comments on commit 0bfc766

Please sign in to comment.