build_colorized_logger

    Build a colorized logger using function arguments and environment
    variables.

    :param name: name that shows in the logger
    :param config: name of the config file
    :param log_level: level to log
    :param log_config_path: path to log config file
    :param handler_name: handler name in the config
    :param handlers_dict: handlers dict
    :param enable_splunk: Turn off splunk even if the env keys are set
                          True by default - all processes that have the
                          ``SPLUNK_*`` env keys will publish logs to splunk
    :param splunk_user: splunk username - defaults to environment variable:
                        SPLUNK_USER
    :param splunk_password: splunk password - defaults to
                            environment variable:
                            SPLUNK_PASSWORD
    :param splunk_address: splunk address - defaults to environment variable:
                           SPLUNK_ADDRESS which is localhost:8088
    :param splunk_api_address: splunk api address - defaults to
                               environment variable:
                               SPLUNK_API_ADDRESS which is localhost:8089
    :param splunk_index: splunk index - defaults to environment variable:
                         SPLUNK_INDEX
    :param splunk_token: splunk token - defaults to environment variable:
                         SPLUNK_TOKEN
    :param splunk_handler_name: splunk log config handler name - defaults to :
                                SPLUNK_HANDLER_NAME
    :param splunk_handler_name: splunk log config handler name - defaults to :
                                SPLUNK_HANDLER_NAME
    :param splunk_sleep_interval: optional splunk sleep interval
    :param splunk_verify: splunk verify - defaults to environment variable:
                          SPLUNK_VERIFY=<1|0>
    :param splunk_debug: print out the connection attempt for debugging
                         Please Avoid on production...

def build_colorized_logger(
        name='lg',
        config='shared-logging.json',
        log_level=logging.INFO,
        log_config_path=None,
        handler_name='console',
        handlers_dict=None,
        enable_splunk=True,
        splunk_user=None,
        splunk_password=None,
        splunk_address=None,
        splunk_api_address=None,
        splunk_index=None,
        splunk_token=None,
        splunk_handler_name='splunk',
        splunk_sleep_interval=-1,
        splunk_verify=None,
        splunk_debug=False):
    """build_colorized_logger

    Build a colorized logger using function arguments and environment
    variables.

    :param name: name that shows in the logger
    :param config: name of the config file
    :param log_level: level to log
    :param log_config_path: path to log config file
    :param handler_name: handler name in the config
    :param handlers_dict: handlers dict
    :param enable_splunk: Turn off splunk even if the env keys are set
                          True by default - all processes that have the
                          ``SPLUNK_*`` env keys will publish logs to splunk
    :param splunk_user: splunk username - defaults to environment variable:
                        SPLUNK_USER
    :param splunk_password: splunk password - defaults to
                            environment variable:
                            SPLUNK_PASSWORD
    :param splunk_address: splunk address - defaults to environment variable:
                           SPLUNK_ADDRESS which is localhost:8088
    :param splunk_api_address: splunk api address - defaults to
                               environment variable:
                               SPLUNK_API_ADDRESS which is localhost:8089
    :param splunk_index: splunk index - defaults to environment variable:
                         SPLUNK_INDEX
    :param splunk_token: splunk token - defaults to environment variable:
                         SPLUNK_TOKEN
    :param splunk_handler_name: splunk log config handler name - defaults to :
                                SPLUNK_HANDLER_NAME
    :param splunk_handler_name: splunk log config handler name - defaults to :
                                SPLUNK_HANDLER_NAME
    :param splunk_sleep_interval: optional splunk sleep interval
    :param splunk_verify: splunk verify - defaults to environment variable:
                          SPLUNK_VERIFY=<1|0>
    :param splunk_debug: print out the connection attempt for debugging
                         Please Avoid on production...
    """

    use_log_config_path = '{}/{}'.format(
        os.getenv(
            'LOG_CFG',
            os.path.dirname(os.path.realpath(__file__))),
        config)
    use_splunk_user = SPLUNK_USER
    use_splunk_password = SPLUNK_PASSWORD
    use_splunk_address = SPLUNK_ADDRESS
    use_splunk_api_address = SPLUNK_API_ADDRESS
    use_splunk_token = SPLUNK_TOKEN
    use_splunk_index = SPLUNK_INDEX
    use_splunk_verify = SPLUNK_VERIFY
    use_splunk_debug = SPLUNK_DEBUG
    use_splunk_handler_name = SPLUNK_HANDLER_NAME
    use_handler_name = LOG_HANDLER_NAME
    use_handlers_dict = None
    use_splunk_host = None
    use_splunk_port = None
    if log_config_path:
        use_log_config_path = log_config_path
    if handler_name:
        use_handler_name = handler_name
    if handlers_dict:
        use_handlers_dict = handlers_dict
    if splunk_user:
        use_splunk_user = splunk_user
    if splunk_password:
        use_splunk_password = splunk_password
    if splunk_address:
        use_splunk_address = splunk_address
    if splunk_api_address:
        if 'https:' in splunk_api_address:
            use_splunk_api_address = splunk_api_address
        else:
            use_splunk_api_address = 'https://{}'.format(
                splunk_api_address)
    else:
        use_splunk_api_address = 'https://{}'.format(
            use_splunk_api_address)
    if splunk_index:
        use_splunk_index = splunk_index
    if splunk_token:
        use_splunk_token = splunk_token
    if splunk_handler_name:
        use_splunk_handler_name = splunk_handler_name
    if splunk_verify:
        use_splunk_verify = splunk_verify
    if splunk_debug:
        use_splunk_debug = splunk_debug

    override_config = os.getenv(
        'SHARED_LOG_CFG',
        None)
    debug_log_config = bool(os.getenv(
        'DEBUG_SHARED_LOG_CFG',
        '0') == '1')
    use_config = (
        './log/{}').format(
            '{}'.format(
                config))
    if override_config:
        if debug_log_config:
            print((
                'creating logger config env var: '
                'SHARED_LOG_CFG={}'.format(
                    override_config)))
        use_config = override_config
    else:
        if debug_log_config:
            print((
                'Not using shared logging env var: '
                'SHARED_LOG_CFG={}'.format(
                    override_config)))
    # allow a shared log config across all components

    if not os.path.exists(use_config):
        use_config = use_log_config_path
        if not os.path.exists(use_config):
            use_config = ('./spylunking/log/{}').format(
                    config)
            if not os.path.exists(use_config):
                use_config = ('./spylunking/log/{}').format(
                    'shared-logging.json')
    # find the log processing

    try:
        use_splunk_host = use_splunk_address.split(':')[0]
        use_splunk_port = int(use_splunk_address.split(':')[1])
    except Exception as e:
        print((
            'Failed parsing Splunk Address={} '
            'with ex={}').format(
                use_splunk_address,
                e))
        use_splunk_host = None
        use_splunk_port = None
    # end of try/ex to parse splunk address

    if enable_splunk and (
            use_splunk_user
            and use_splunk_password
            and use_splunk_address
            and use_splunk_api_address):
        try:
            if SPLUNK_TOKEN:
                use_splunk_token = SPLUNK_TOKEN
            else:
                use_splunk_token = get_token.get_token(
                    user=use_splunk_user,
                    password=use_splunk_password,
                    url=use_splunk_api_address,
                    verify=use_splunk_verify)
            if use_splunk_debug:
                print((
                    'connected to splunk address={} user={} '
                    'password={} token={}').format(
                        use_splunk_address,
                        use_splunk_user,
                        use_splunk_password,
                        use_splunk_token))
        except Exception as e:
            print((
                'Failed connecting to splunk address={} user={} '
                'ex={}').format(
                    use_splunk_api_address,
                    splunk_user,
                    e))
            use_splunk_token = None
        # end of try/ex on splunk login
    # end of try to set the token to use

    if enable_splunk:
        if use_splunk_token and use_splunk_debug:
            print('Using splunk address={} token={}'.format(
                use_splunk_address,
                use_splunk_token))
    else:
        # turn off external keyword arguments (kwargs):
        use_splunk_host = None
        use_splunk_port = None
        use_splunk_index = None
        use_splunk_token = None

    setup_logging(
        default_level=log_level,
        default_path=use_config,
        handler_name=use_handler_name,
        handlers_dict=use_handlers_dict,
        splunk_host=use_splunk_host,
        splunk_port=use_splunk_port,
        splunk_index=use_splunk_index,
        splunk_token=use_splunk_token,
        splunk_handler_name=use_splunk_handler_name,
        splunk_sleep_interval=splunk_sleep_interval,
        splunk_verify=use_splunk_verify,
        splunk_debug=use_splunk_debug)

    if enable_splunk:
        default_fields = {
            'name': SPLUNK_LOG_NAME,
            'dc': SPLUNK_DEPLOY_CONFIG,
            'env': SPLUNK_ENV_NAME
        }

        last_step = ''
        try:
            last_step = (
                'checking if LOG_FIELDS_DICT '
                'has valid JSON using json.loads({})').format(
                    os.getenv(
                        'LOG_FIELDS_DICT',
                        None))
            if use_splunk_debug:
                print(last_step)
            if os.getenv(
                    'LOG_FIELDS_DICT',
                    None):
                default_fields = json.loads(
                    os.getenv(
                        'LOG_FIELDS_DICT',
                        default_fields))
            last_step = (
                'looking for splunk_handler={}').format(
                    splunk_handler_name)
            if use_splunk_debug:
                print(last_step)
            for i in logging.root.handlers:
                handler_class_name = i.__class__.__name__.lower()
                last_step = (
                    'checking splunk_handler_name={} '
                    'in handler_name={} or '
                    'hasattr({}.formatter, set_fields)').format(
                        handler_class_name,
                        splunk_handler_name,
                        handler_class_name)
                if use_splunk_debug:
                    print(last_step)
                if (splunk_handler_name in handler_class_name
                        or hasattr(i.formatter, 'set_fields')):
                    last_step = (
                        'assigning fields={} to formatter={}').format(
                            default_fields,
                            i.formatter.__class__.__name__)
                    if use_splunk_debug:
                        print(last_step)
                    i.formatter.set_fields(
                        new_fields=default_fields)
        except Exception as e:
            print((
                'Failed assigning splunk_handler_name={} '
                'during last_step={} with ex={}').format(
                    splunk_handler_name,
                    last_step,
                    e))
        # end of try/ex for setting the formatter
    return logging.getLogger(name)