-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support configure logging via config file
- Loading branch information
Showing
10 changed files
with
127 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,48 @@ | ||
import logging | ||
import logging.config | ||
from .i18n import init_locale | ||
from .config_data import Config | ||
from .utils import merge_dicts | ||
|
||
|
||
__all__ = ['config', 'init_locale', "load_config"] | ||
|
||
__all__ = ['config', 'init_locale', "load_config", "logger", "init_logging"] | ||
|
||
config = None | ||
|
||
_logger_name = "beanbot" | ||
logger = logging.getLogger(_logger_name) | ||
|
||
|
||
def load_config(config_path): | ||
global config | ||
config = Config(config_path) | ||
|
||
|
||
_default_logging_config = { | ||
"version": 1, | ||
"formatters": { | ||
"standard": { | ||
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
} | ||
}, | ||
"handlers": { | ||
"console": { | ||
"class": "logging.StreamHandler", | ||
"level": "WARNING", | ||
"formatter": "standard", | ||
"stream": "ext://sys.stdout" | ||
} | ||
}, | ||
"loggers": { | ||
_logger_name: { | ||
"level": "WARNING", | ||
"handlers": ["console"], | ||
"propagate": False, | ||
} | ||
} | ||
} | ||
|
||
|
||
def init_logging(): | ||
logging_conf = merge_dicts(_default_logging_config, config.get("logging", {})) | ||
logging.config.dictConfig(logging_conf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def merge_dicts(dict1, dict2): | ||
result = dict1.copy() | ||
|
||
for key, value in dict2.items(): | ||
if key in result and isinstance(result[key], dict) and isinstance(value, dict): | ||
result[key] = merge_dicts(result[key], value) | ||
else: | ||
result[key] = value | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from conf.utils import merge_dicts | ||
|
||
|
||
def test_merge_empty_dicts(): | ||
dict1 = {} | ||
dict2 = {} | ||
expected = {} | ||
assert merge_dicts(dict1, dict2) == expected | ||
|
||
def test_merge_dict_with_empty_dict(): | ||
dict1 = {'a': 1, 'b': 2} | ||
dict2 = {} | ||
expected = {'a': 1, 'b': 2} | ||
assert merge_dicts(dict1, dict2) == expected | ||
|
||
def test_merge_non_overlapping_keys(): | ||
dict1 = {'a': 1, 'b': 2} | ||
dict2 = {'c': 3, 'd': 4} | ||
expected = {'a': 1, 'b': 2, 'c': 3, 'd': 4} | ||
assert merge_dicts(dict1, dict2) == expected | ||
|
||
def test_merge_overlapping_keys_non_dict_values(): | ||
dict1 = {'a': 1, 'b': 2} | ||
dict2 = {'b': 3, 'c': 4} | ||
expected = {'a': 1, 'b': 3, 'c': 4} | ||
assert merge_dicts(dict1, dict2) == expected | ||
|
||
def test_merge_overlapping_keys_dict_values(): | ||
dict1 = {'a': 1, 'b': {'x': 1, 'y': 2}} | ||
dict2 = {'b': {'y': 3, 'z': 4}} | ||
expected = {'a': 1, 'b': {'x': 1, 'y': 3, 'z': 4}} | ||
assert merge_dicts(dict1, dict2) == expected | ||
|
||
def test_merge_deeply_nested_dict_values(): | ||
dict1 = {'a': 1, 'b': {'x': 1, 'y': {'m': 1, 'n': 2}}} | ||
dict2 = {'b': {'y': {'n': 3, 'o': 4}}} | ||
expected = {'a': 1, 'b': {'x': 1, 'y': {'m': 1, 'n': 3, 'o': 4}}} | ||
assert merge_dicts(dict1, dict2) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters