Repository: DakaraProject/dakara-base

Issue:
Allow to set log format and level
Currently, `dakara_base.config.create_logger` setups logger using a fixed log format string and log level. This format and level should be adjustable.

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c55cb8e..89002dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,10 @@
 
 ## Unreleased
 
+### Added
+
+- You can now specify custom log format and log level in `dakara_base.config.create_logger` with arguments `custom_log_format` and `custom_log_level`.
+
 ## 1.1.0 - 2019-09-16
 
 ### Added
diff --git a/src/dakara_base/config.py b/src/dakara_base/config.py
index 49d0e69..8f7aa4d 100644
--- a/src/dakara_base/config.py
+++ b/src/dakara_base/config.py
@@ -85,20 +85,24 @@ def load_config(config_path, debug, mandatory_keys=None):
     return config
 
 
-def create_logger(wrap=False):
+def create_logger(wrap=False, custom_log_format=None, custom_log_level=None):
     """Create logger
 
     Args:
-        wrap (bool): If True, wrap the standard error stream for using logging
+        wrap (bool): if True, wrap the standard error stream for using logging
             and progress bar. You have to enable this flag if you use
             `progress_bar`.
+        custom_log_format (str): custom format string to use for logs.
+        custom_log_level (str): custom level of logging.
     """
     # wrap stderr on demand
     if wrap:
         progressbar.streams.wrap_stderr()
 
     # setup loggers
-    coloredlogs.install(fmt=LOG_FORMAT, level=LOG_LEVEL)
+    log_format = custom_log_format or LOG_FORMAT
+    log_level = custom_log_level or LOG_LEVEL
+    coloredlogs.install(fmt=log_format, level=log_level)
 
 
 def set_loglevel(config):


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
index 61edc9e..a1e56e7 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -6,8 +6,6 @@ from yaml.parser import ParserError
 
 from dakara_base.config import (
     load_config,
-    LOG_FORMAT,
-    LOG_LEVEL,
     ConfigInvalidError,
     ConfigNotFoundError,
     ConfigParseError,
@@ -97,6 +95,8 @@ class LoadConfigTestCase(TestCase):
         )
 
 
+@patch("dakara_base.config.LOG_FORMAT", "my format")
+@patch("dakara_base.config.LOG_LEVEL", "my level")
 class CreateLoggerTestCase(TestCase):
     """Test the `create_logger` function
     """
@@ -110,7 +110,7 @@ class CreateLoggerTestCase(TestCase):
         create_logger()
 
         # assert the call
-        mocked_install.assert_called_with(fmt=LOG_FORMAT, level=LOG_LEVEL)
+        mocked_install.assert_called_with(fmt="my format", level="my level")
         mocked_wrap_stderr.assert_not_called()
 
     @patch("dakara_base.progress_bar.progressbar.streams.wrap_stderr")
@@ -122,9 +122,25 @@ class CreateLoggerTestCase(TestCase):
         create_logger(wrap=True)
 
         # assert the call
-        mocked_install.assert_called_with(fmt=LOG_FORMAT, level=LOG_LEVEL)
+        mocked_install.assert_called_with(fmt="my format", level="my level")
         mocked_wrap_stderr.assert_called_with()
 
+    @patch("dakara_base.progress_bar.progressbar.streams.wrap_stderr")
+    @patch("dakara_base.config.coloredlogs.install", autospec=True)
+    def test_custom(self, mocked_install, mocked_wrap_stderr):
+        """Test to call the method with custom format and level
+        """
+        # call the method
+        create_logger(
+            custom_log_format="my custom format", custom_log_level="my custom level"
+        )
+
+        # assert the call
+        mocked_install.assert_called_with(
+            fmt="my custom format", level="my custom level"
+        )
+        mocked_wrap_stderr.assert_not_called()
+
 
 class SetLoglevelTestCase(TestCase):
     """Test the `set_loglevel` function