github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/sawtooth_validator/config/logs.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 import os 16 import sys 17 import toml 18 import yaml 19 20 21 def _get_config_dir(): 22 """Returns the sawtooth configuration directory based on the 23 SAWTOOTH_HOME environment variable (if set) or OS defaults. 24 """ 25 if 'SAWTOOTH_HOME' in os.environ: 26 return os.path.join(os.environ['SAWTOOTH_HOME'], 'etc') 27 28 if os.name == 'nt': 29 base_dir = \ 30 os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) 31 return os.path.join(base_dir, 'conf') 32 33 return '/etc/sawtooth' 34 35 36 def _get_config(): 37 """Determines if there is a log config in the config directory 38 and returns it. If it does not exist, return None. 39 40 Returns: 41 log_config (dict): The dictionary to pass to logging.config.dictConfig 42 """ 43 conf_file = os.path.join(_get_config_dir(), 'log_config.toml') 44 if os.path.exists(conf_file): 45 with open(conf_file) as fd: 46 raw_config = fd.read() 47 log_config = toml.loads(raw_config) 48 return log_config 49 50 conf_file = os.path.join(_get_config_dir(), 'log_config.yaml') 51 if os.path.exists(conf_file): 52 with open(conf_file) as fd: 53 raw_config = fd.read() 54 log_config = yaml.safe_load(raw_config) 55 return log_config 56 57 return None 58 59 60 def get_log_config(): 61 """Returns the log config if it exists.""" 62 return _get_config()