Add optional filepath parameter to set_aqua_logging
@@ -21,6 +21,7 @@ import logging
from logging.config import dictConfig
from collections import OrderedDict
import pkg_resources
+import os
_ALGO_LOGGING_CONFIG = {
'version': 1,
@@ -50,7 +51,7 @@ def _get_logging_names():
return list(names.keys())
-def build_logging_config(level):
+def build_logging_config(level, filepath=None):
"""
Creates a the configuration dict of the named loggers using the default SDK
configuration provided by `_ALGO_LOGGING_CONFIG`:
@@ -58,11 +59,25 @@ def build_logging_config(level):
* console logging using a custom format for levels != level parameter.
* console logging with simple format for level parameter.
* set logger level to level parameter.
+
+ Args:
+ level (number): logging level
+ filepath (str): file to receive logging data
"""
dict = copy.deepcopy(_ALGO_LOGGING_CONFIG)
+ if filepath is not None:
+ filepath = os.path.expanduser(filepath)
+ dict['handlers']['f'] = {
+ 'class': 'logging.FileHandler',
+ 'formatter': 'f',
+ 'filename': filepath,
+ 'mode': 'w'
+ }
+
+ handlers = list(dict['handlers'].keys())
for name in _get_logging_names():
dict['loggers'][name] = {
- 'handlers': ['h'],
+ 'handlers': handlers,
'propagate': False,
'level': level
}
@@ -95,11 +110,12 @@ def get_aqua_logging():
return get_logging_level()
-def set_aqua_logging(level):
+def set_aqua_logging(level, filepath=None):
"""
Updates the Aqua logging with the appropriate logging level
Args:
level (number): logging level
+ filepath (str): file to receive logging data
"""
- set_logging_config(build_logging_config(level))
+ set_logging_config(build_logging_config(level, filepath))
