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 committed Jul 28, 2023
1 parent 2a77c27 commit d3e331d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
8 changes: 2 additions & 6 deletions lib/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from systemd.daemon import notify # type: ignore

from systemd import journal
from uaclient import defaults
from uaclient.config import UAConfig
from uaclient.daemon import (
Expand All @@ -16,12 +17,7 @@


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
Expand Down
18 changes: 4 additions & 14 deletions lib/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime, timedelta, timezone
from typing import Callable, Optional

from systemd import journal
from uaclient import defaults
from uaclient.cli import setup_logging
from uaclient.config import UAConfig
Expand Down Expand Up @@ -179,25 +180,14 @@ def run_jobs(cfg: UAConfig, current_time: datetime):


if __name__ == "__main__":
setup_logging(
logging.CRITICAL,
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.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)
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.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 d3e331d

Please sign in to comment.