Skip to content

Commit

Permalink
Isolate manifester's logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ogajduse committed Jul 17, 2024
1 parent 2472b22 commit 687800e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion manifester/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from pathlib import Path

import click
from logzero import logger

from manifester import Manifester, helpers
from manifester.logger import _logger as logger
from manifester.settings import settings


Expand Down
6 changes: 1 addition & 5 deletions manifester/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
import sys
import time

from logzero import logger
from requests import HTTPError
import yaml

from manifester.logger import setup_logzero
from manifester.logger import _logger as logger
from manifester.settings import settings

setup_logzero(level="info")


RESULTS_LIMIT = 10000


Expand Down
32 changes: 28 additions & 4 deletions manifester/logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"""Defines manifester's internal logging."""

import logging
from pathlib import Path

import logzero

from manifester.settings import settings


def setup_logzero(level="info", path="logs/manifester.log", silent=True):
def _setup_logzero(
level=settings.get("log_level", "info"), path="logs/manifester.log", name=None, silent=True
):
"""Call logzero setup with the given settings."""
Path(path).parent.mkdir(parents=True, exist_ok=True)
log_fmt = "%(color)s[%(levelname)s %(asctime)s]%(end_color)s %(message)s"
Expand All @@ -15,10 +20,29 @@ def setup_logzero(level="info", path="logs/manifester.log", silent=True):
log_level = getattr(logging, level.upper(), logging.INFO)
# formatter for terminal
formatter = logzero.LogFormatter(fmt=debug_fmt if log_level is logging.DEBUG else log_fmt)
logzero.setup_logger(formatter=formatter, disableStderrLogger=silent)
logzero.loglevel(log_level)
if not name:
name = "manifester"
# formatter for file
formatter = logzero.LogFormatter(
fmt=debug_fmt if log_level is logging.DEBUG else log_fmt, color=False
)
logzero.logfile(path, loglevel=log_level, maxBytes=1e9, backupCount=3, formatter=formatter)
logger = logzero.setup_logger(
name=name,
formatter=formatter,
level=log_level,
logfile=path,
maxBytes=1e9,
backupCount=3,
disableStderrLogger=silent,
)
return logger


_logger = _setup_logzero()


def setup_logzero(
level=settings.get("log_level", "info"), path="logs/manifester.log", name=None, silent=True
):
"""Call logzero setup with the given settings."""
_logger = _setup_logzero(level, path, name, silent)
4 changes: 1 addition & 3 deletions manifester/manifester.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import string

from dynaconf.utils.boxing import DynaBox
from logzero import logger
from requests.exceptions import Timeout

from manifester.helpers import (
Expand All @@ -18,7 +17,7 @@
simple_retry,
update_inventory,
)
from manifester.logger import setup_logzero
from manifester.logger import _logger as logger
from manifester.settings import settings


Expand All @@ -33,7 +32,6 @@ def __init__(
proxies=None,
**kwargs,
):
setup_logzero(level=settings.get("log_level", "info"))
if minimal_init:
self.offline_token = settings.get("offline_token")
self.token_request_url = settings.get("url").get("token_request")
Expand Down

0 comments on commit 687800e

Please sign in to comment.