Skip to content

Commit

Permalink
Enable Log name and level overrides from envvars (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel authored May 24, 2023
1 parent 5e43850 commit f5e6f65
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
17 changes: 12 additions & 5 deletions ovos_utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
class LOG:
"""
Custom logger class that acts like logging.Logger
The logger name is automatically generated by the module of the caller
The logger name is gnerally set by the calling module, but the default name
is read from the envvar `OVOS_DEFAULT_LOG_NAME`.
The log level defaults to `INFO` and can be overridden by
`OVOS_DEFAULT_LOG_LEVEL`. Note that log level may be overridden by
configuration when calling `LOG.init`.
Usage:
>>> LOG.debug('My message: %s', debug_str)
Expand All @@ -36,8 +42,8 @@ class LOG:
formatter = logging.Formatter(fmt, datefmt)
max_bytes = 50000000
backup_count = 3
name = 'OVOS'
level = "DEBUG"
name = os.getenv("OVOS_DEFAULT_LOG_NAME") or 'OVOS'
level = os.getenv("OVOS_DEFAULT_LOG_LEVEL") or "INFO"
diagnostic_mode = False
_loggers = {}

Expand All @@ -52,15 +58,16 @@ def init(cls, config=None):
from ovos_config.meta import get_xdg_base
default_base = get_xdg_base()
except ImportError:
default_base = "mycroft"
default_base = os.environ.get("OVOS_CONFIG_BASE_FOLDER") or \
"mycroft"
from ovos_utils.xdg_utils import xdg_state_home

config = config or {}
cls.base_path = config.get("path") or \
f"{xdg_state_home()}/{default_base}"
cls.max_bytes = config.get("max_bytes", 50000000)
cls.backup_count = config.get("backup_count", 3)
cls.level = config.get("level", "INFO")
cls.level = config.get("level") or LOG.level
cls.diagnostic_mode = config.get("diagnostic", False)

@classmethod
Expand Down
55 changes: 54 additions & 1 deletion test/unittests/test_log.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
import os
import shutil
import unittest
import importlib

from os.path import join, dirname, isdir, isfile


class TestLog(unittest.TestCase):
test_dir = join(dirname(__file__), "log_test")

@classmethod
def tearDownClass(cls) -> None:
if isdir(cls.test_dir):
shutil.rmtree(cls.test_dir)

def test_log(self):
import ovos_utils.log
from ovos_utils.log import LOG
# TODO
# Default log config
self.assertEqual(LOG.base_path, "stdout")
self.assertIsInstance(LOG.fmt, str)
self.assertIsInstance(LOG.datefmt, str)
self.assertIsNotNone(LOG.formatter)
self.assertIsInstance(LOG.max_bytes, int)
self.assertIsInstance(LOG.backup_count, int)
self.assertEqual(LOG.name, "OVOS")
self.assertEqual(LOG.level, "INFO")
self.assertFalse(LOG.diagnostic_mode)

# Override from envvars
os.environ["OVOS_DEFAULT_LOG_NAME"] = "test"
os.environ["OVOS_DEFAULT_LOG_LEVEL"] = "DEBUG"
importlib.reload(ovos_utils.log)
from ovos_utils.log import LOG
self.assertEqual(LOG.name, "test")
self.assertEqual(LOG.level, "DEBUG")

# init log
test_config = {"path": self.test_dir,
"max_bytes": 100000,
"backup_count": 0,
"level": "WARNING",
"diagnostic": True}
LOG.init(test_config)
self.assertEqual(LOG.base_path, self.test_dir)
self.assertEqual(LOG.max_bytes, 100000)
self.assertEqual(LOG.backup_count, 0)
self.assertEqual(LOG.level, "WARNING")
self.assertTrue(LOG.diagnostic_mode)

log_file = join(LOG.base_path, f"{LOG.name}.log")
self.assertFalse(isfile(log_file))
LOG.info("This won't print")
self.assertTrue(isfile(log_file))
LOG.warning("This will print")
with open(log_file) as f:
lines = f.readlines()
self.assertEqual(len(lines), 1)
self.assertTrue(lines[0].endswith("This will print\n"))

def test_init_service_logger(self):
from ovos_utils.log import init_service_logger
Expand Down

0 comments on commit f5e6f65

Please sign in to comment.