From 28ee4f7197bb1f9714ee762715fad00e2292579f Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 21 Jan 2022 00:13:07 +0000 Subject: [PATCH] Add log disable and env var loglevel --- CHANGELOG | 12 ++++++++---- chainlib/cli/arg.py | 3 ++- chainlib/cli/config.py | 41 +++++++++++++++++++++++++++++++++++++++-- setup.cfg | 2 +- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 57ce577..20d2dc3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,16 +1,20 @@ +- 0.0.17 + * Add loglevel environment variable +- 0.0.16 + * Allow query string in query url - 0.0.14 * Add option to skip ssl verification on rpc - 0.0.5 * Move eth code to separate package -- 0.0.4-unreleased +- 0.0.4 * Add pack tx from already signed tx struct * Add http auth handling for jsonrpc connections * Add customizable jsonrpc id generator (to allow for buggy server id handling) -- 0.0.3-unreleased +- 0.0.3 * Remove erc20 module (to new external package) -- 0.0.2-unreleased +- 0.0.2 * -- 0.0.1-unreleased +- 0.0.1 * Add eth tx decode * Add eth balance query with erc20 option * Add eth checksum address diff --git a/chainlib/cli/arg.py b/chainlib/cli/arg.py index e3337be..b61c9d6 100644 --- a/chainlib/cli/arg.py +++ b/chainlib/cli/arg.py @@ -139,8 +139,9 @@ class ArgumentParser(argparse.ArgumentParser): :type env: dict """ if arg_flags & Flag.VERBOSE: + self.add_argument('--no-logs', dest='no_logs',action='store_true', help='Turn off all logging') self.add_argument('-v', action='store_true', help='Be verbose') - self.add_argument('-vv', action='store_true', help='Be more verbose') + self.add_argument('-vv', action='store_true', help='Be very verbose') if arg_flags & Flag.CONFIG: self.add_argument('-c', '--config', type=str, default=env.get('CONFINI_DIR'), help='Configuration directory') self.add_argument('-n', '--namespace', type=str, help='Configuration namespace') diff --git a/chainlib/cli/config.py b/chainlib/cli/config.py index 94871bd..2fd2356 100644 --- a/chainlib/cli/config.py +++ b/chainlib/cli/config.py @@ -2,6 +2,7 @@ import logging import os import sys +import re # external imports import confini @@ -102,14 +103,51 @@ class Config(confini.Config): :rtype: confini.Config :return: Processed configuation """ + env_prefix = getattr(args, 'env_prefix', None) + env_prefix_str = env_prefix + if env_prefix_str == None: + env_prefix_str = '' + else: + env_prefix_str += '_' + + env_loglevel_key_str = env_prefix_str + 'LOGLEVEL' + env_loglevel = os.environ.get(env_loglevel_key_str) + if logger == None: logger = logging.getLogger() - if arg_flags & Flag.CONFIG: + if env_loglevel != None: + env_loglevel = env_loglevel.lower() + if env_loglevel == '0' or env_loglevel == 'no' or env_loglevel == 'none' or env_loglevel == 'disable' or env_loglevel == 'disabled' or env_loglevel == 'off': + logging.disable() + elif env_loglevel == '1' or env_loglevel == 'err' or env_loglevel == 'error': + logger.setLevel(logging.ERROR) + elif env_loglevel == '2' or env_loglevel == 'warning' or env_loglevel == 'warn': + logger.setLevel(logging.WARNING) + elif env_loglevel == '3' or env_loglevel == 'info': + logger.setLevel(logging.INFO) + else: + valid_level = False + try: + num_loglevel = int(env_loglevel) + valid_level = True + except: + if env_loglevel == 'debug': + valid_level = True + + if not valid_level: + raise ValueError('unknown loglevel {} set in environment variable {}'.format(env_loglevel, env_loglevel_key_str)) + + logger.setLevel(logging.DEBUG) + + + if arg_flags & Flag.VERBOSE: if args.vv: logger.setLevel(logging.DEBUG) elif args.v: logger.setLevel(logging.INFO) + if args.no_logs: + logging.disable() override_config_dirs = [] config_dir = [cls.default_base_config_dir] @@ -160,7 +198,6 @@ class Config(confini.Config): # default_config_dir = default_parent_config_dir # config_dir = default_config_dir # override_config_dirs = [] - env_prefix = getattr(args, 'env_prefix', None) config = confini.Config(config_dir, env_prefix=env_prefix, override_dirs=override_config_dirs) config.process() diff --git a/setup.cfg b/setup.cfg index 8cfe823..9cf40ec 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ name=chainlib license=WTFPL2 author_email=dev@holbrook.no description=Generic blockchain access library and tooling -version=0.0.16 +version=0.0.17 url=https://gitlab.com/chaintools/chainlib author=Louis Holbrook