Skip to content

Commit

Permalink
Allow logging to fallback to native logger if dependency not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Oct 4, 2024
1 parent 24d7ee6 commit 493e384
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
13 changes: 7 additions & 6 deletions fmtr/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import fmtr.tools.random_tools as random
import fmtr.tools.string_tools as string
import fmtr.tools.name_tools as name
import fmtr.tools.logging_tools as logging
from fmtr.tools.logging_tools import logger

from fmtr.tools.import_tools import MissingExtraMockModule
from fmtr.tools.path_tools import Path
Expand All @@ -31,12 +33,6 @@
except ImportError as exception:
yaml = MissingExtraMockModule('yaml', exception)

try:
from fmtr.tools import logging_tools as logging
from fmtr.tools.logging_tools import logger
except ImportError as exception:
logging = logger = MissingExtraMockModule('logging', exception)

try:
from fmtr.tools import docker_tools as docker
from fmtr.tools.docker_tools import Container
Expand Down Expand Up @@ -101,6 +97,11 @@
except ImportError as exception:
ai = MissingExtraMockModule('ai', exception)

try:
from fmtr.tools import data_modelling_tools as dm
except ImportError as exception:
dm = MissingExtraMockModule('dm', exception)

try:
from fmtr.tools import json_fix_tools as json_fix
except ImportError as exception:
Expand Down
20 changes: 16 additions & 4 deletions fmtr/tools/logging_tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys

from loguru import logger as logger_loguru

from fmtr.tools.config import ToolsConfig
from fmtr.tools.config_tools import ConfigClass
from fmtr.tools.environment_tools import get
Expand Down Expand Up @@ -30,16 +28,30 @@ def default_patch(record):
return record


def get_logger(logger=logger_loguru, terminal=True, level=LoggingConfig.DEFAULT_LEVEL, time_format=LoggingConfig.TIME,
def get_logger(terminal=True, level=LoggingConfig.DEFAULT_LEVEL, time_format=LoggingConfig.TIME,
icon_format=LoggingConfig.ICON,
level_format=LoggingConfig.LEVEL, file_format=LoggingConfig.FILE, function_format=LoggingConfig.FUNCTION,
message_format=LoggingConfig.MESSAGE,
logfile=False, logfile_dir=None):
"""
Get a pre-configured loguru logger, if dependency is present, otherwise default to native logger.
"""

try:
from loguru import logger as logger_loguru
logger = logger_loguru
except ImportError:
import logging

logger = logging.getLogger(None)
logger.setLevel(level)

return logger



components = [time_format, icon_format, level_format, file_format, function_format, message_format]
format = LoggingConfig.SEP.join([component for component in components if component])
logger.remove()
Expand Down
2 changes: 1 addition & 1 deletion fmtr/tools/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.2
0.9.3

0 comments on commit 493e384

Please sign in to comment.