Skip to content

Commit

Permalink
Merge pull request #1832 from JarbasAl/bugfix/log_level_conf
Browse files Browse the repository at this point in the history
Bugfix/honor log level in config #1728
  • Loading branch information
forslund committed Oct 9, 2018
2 parents 83e1ed1 + 260f54a commit add3a69
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
4 changes: 2 additions & 2 deletions mycroft/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from .config import Configuration, LocalConf, RemoteConf, \
SYSTEM_CONFIG, USER_CONFIG
from .config import Configuration, LocalConf, RemoteConf
from .locations import SYSTEM_CONFIG, USER_CONFIG


# Compatibility
Expand Down
27 changes: 3 additions & 24 deletions mycroft/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,13 @@
import re
import json
import inflection
from os.path import exists, isfile, join, dirname, expanduser
from os.path import exists, isfile
from requests import RequestException

from mycroft.util.json_helper import load_commented_json
from mycroft.util.json_helper import load_commented_json, merge_dict
from mycroft.util.log import LOG


def merge_dict(base, delta):
"""
Recursively merging configuration dictionaries.
Args:
base: Target for merge
delta: Dictionary to merge into base
"""

for k, dv in delta.items():
bv = base.get(k)
if isinstance(dv, dict) and isinstance(bv, dict):
merge_dict(bv, dv)
else:
base[k] = dv
from .locations import DEFAULT_CONFIG, SYSTEM_CONFIG, USER_CONFIG


def is_remote_list(values):
Expand Down Expand Up @@ -189,12 +174,6 @@ def __init__(self, cache=None):
self.load_local(cache)


DEFAULT_CONFIG = join(dirname(__file__), 'mycroft.conf')
SYSTEM_CONFIG = '/etc/mycroft/mycroft.conf'
USER_CONFIG = join(expanduser('~'), '.mycroft/mycroft.conf')
REMOTE_CONFIG = "mycroft.ai"


class Configuration(object):
__config = {} # Cached config
__patch = {} # Patch config that skills can update to override config
Expand Down
19 changes: 19 additions & 0 deletions mycroft/configuration/locations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from os.path import join, dirname, expanduser

DEFAULT_CONFIG = join(dirname(__file__), 'mycroft.conf')
SYSTEM_CONFIG = '/etc/mycroft/mycroft.conf'
USER_CONFIG = join(expanduser('~'), '.mycroft/mycroft.conf')
REMOTE_CONFIG = "mycroft.ai"
17 changes: 17 additions & 0 deletions mycroft/util/json_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
import json


def merge_dict(base, delta):
"""
Recursively merging configuration dictionaries.
Args:
base: Target for merge
delta: Dictionary to merge into base
"""

for k, dv in delta.items():
bv = base.get(k)
if isinstance(dv, dict) and isinstance(bv, dict):
merge_dict(bv, dv)
else:
base[k] = dv


def load_commented_json(filename):
""" Loads an JSON file, ignoring comments
Expand Down
22 changes: 14 additions & 8 deletions mycroft/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from os.path import isfile

from mycroft.util.json_helper import load_commented_json
from mycroft.util.json_helper import load_commented_json, merge_dict
from mycroft.configuration.locations import SYSTEM_CONFIG, USER_CONFIG


def getLogger(name="MYCROFT"):
Expand Down Expand Up @@ -61,8 +62,12 @@ class LOG:

@classmethod
def init(cls):
sys_config = '/etc/mycroft/mycroft.conf'
config = load_commented_json(sys_config) if isfile(sys_config) else {}
confs = [SYSTEM_CONFIG, USER_CONFIG]
config = {}
for conf in confs:
merge_dict(config,
load_commented_json(conf) if isfile(conf) else {})

cls.level = logging.getLevelName(config.get('log_level', 'DEBUG'))
fmt = '%(asctime)s.%(msecs)03d - ' \
'%(name)s - %(levelname)s - %(message)s'
Expand All @@ -74,11 +79,11 @@ def init(cls):

@classmethod
def create_logger(cls, name):
l = logging.getLogger(name)
l.propagate = False
l.addHandler(cls.handler)
l.setLevel(cls.level)
return l
logger = logging.getLogger(name)
logger.propagate = False
logger.addHandler(cls.handler)
logger.setLevel(cls.level)
return logger

def __init__(self, name):
LOG._custom_name = name
Expand Down Expand Up @@ -107,4 +112,5 @@ def _log(cls, func, *args, **kwargs):
name = module_name + ':' + record[3] + ':' + str(record[2])
func(cls.create_logger(name), *args, **kwargs)


LOG.init()

0 comments on commit add3a69

Please sign in to comment.