Skip to content

Commit

Permalink
Merge pull request #44 from ogajduse/fix/use-isolated-logger
Browse files Browse the repository at this point in the history
Isolate manifester's logger
  • Loading branch information
synkd authored Jul 18, 2024
2 parents 2472b22 + 652b432 commit 4401c8a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
13 changes: 13 additions & 0 deletions manifester/_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Module with settings variables/constants."""
import os
from pathlib import Path

settings_file = "manifester_settings.yaml"
MANIFESTER_DIRECTORY = Path()

if "MANIFESTER_DIRECTORY" in os.environ:
envar_location = Path(os.environ["MANIFESTER_DIRECTORY"])
if envar_location.is_dir():
MANIFESTER_DIRECTORY = envar_location

settings_path = MANIFESTER_DIRECTORY.joinpath(settings_file)
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
44 changes: 40 additions & 4 deletions manifester/logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
"""Defines manifester's internal logging."""

import logging
from pathlib import Path

from dynaconf import Dynaconf
import logzero

from manifester._settings import settings_path

# Initialize temporary settings without running the Vault loader
temp_settings = Dynaconf(
settings_file=str(settings_path.absolute()),
ENVVAR_PREFIX_FOR_DYNACONF="MANIFESTER",
load_dotenv=False,
)


def setup_logzero(level="info", path="logs/manifester.log", silent=True):
def _setup_logzero(
level=temp_settings.get("log_level", "info"),
path="logs/manifester.log",
name=None,
formatter=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 +32,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()
# delete temporary settings after initializing the logger
del temp_settings


def setup_logzero(level, path, 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
11 changes: 1 addition & 10 deletions manifester/settings.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
"""Retrieves settings from configuration file and runs Dynaconf validators."""
import os
from pathlib import Path

from dynaconf import Dynaconf, Validator

settings_file = "manifester_settings.yaml"
MANIFESTER_DIRECTORY = Path()
from manifester._settings import settings_path

if "MANIFESTER_DIRECTORY" in os.environ:
envar_location = Path(os.environ["MANIFESTER_DIRECTORY"])
if envar_location.is_dir():
MANIFESTER_DIRECTORY = envar_location

settings_path = MANIFESTER_DIRECTORY.joinpath("manifester_settings.yaml")
validators = [
Validator("offline_token", must_exist=True),
Validator("simple_content_access", default="enabled"),
Expand Down

0 comments on commit 4401c8a

Please sign in to comment.