#!/usr/bin/env python
#
# jose.lausuch@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Logging levels:
#  Level     Numeric value
#  CRITICAL  50
#  ERROR     40
#  WARNING   30
#  INFO      20
#  DEBUG     10
#  NOTSET    0
#
# Usage:
#  import functest_logger as fl
#  logger = fl.Logger("script_name").getLogger()
#  logger.info("message to be shown with - INFO - ")
#  logger.debug("message to be shown with - DEBUG -")
import logging
import logging.config
import os

import json

from functest.utils.constants import CONST

logger = logging.getLogger(__name__)


def is_debug():
    if CONST.CI_DEBUG and CONST.CI_DEBUG.lower() == "true":
        return True
    return False


def setup_logging(default_path=CONST.dir_functest_logging_cfg,
                  default_level=logging.INFO,
                  env_key='LOG_CFG'):
    path = default_path
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = json.load(f)
            if (config['handlers'] and
                    config['handlers']['console']):
                stream_level = logging.INFO
                if is_debug():
                    stream_level = logging.DEBUG
                config['handlers']['console']['level'] = stream_level
            logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)


setup_logging()


class Logger:
    def __init__(self, logger_name):
        self.logger = logging.getLogger(logger_name)

    def getLogger(self):
        return self.logger

@@ -738,913 +738,8 @@
 ST%0A%0A
-logger = logging.getLogger(__name__)%0A%0A%0Adef is_debug():%0A    if CONST.CI_DEBUG and CONST.CI_DEBUG.lower() == %22true%22:%0A        return True%0A    return False%0A%0A%0Adef setup_logging(default_path=CONST.dir_functest_logging_cfg,%0A                  default_level=logging.INFO,%0A                  env_key='LOG_CFG'):%0A    path = default_path%0A    value = os.getenv(env_key, None)%0A    if value:%0A        path = value%0A    if os.path.exists(path):%0A        with open(path, 'rt') as f:%0A            config = json.load(f)%0A            if (config%5B'handlers'%5D and%0A                    config%5B'handlers'%5D%5B'console'%5D):%0A                stream_level = logging.INFO%0A                if is_debug():%0A                    stream_level = logging.DEBUG%0A                config%5B'handlers'%5D%5B'console'%5D%5B'level'%5D = stream_level%0A            logging.config.dictConfig(config)%0A    else:%0A        logging.basicConfig(level=default_level)%0A%0A%0Asetup_logging()%0A%0A
 %0Acla
@@ -786,16 +786,45 @@
 _name):%0A
+        self.setup_logging()%0A
         
@@ -894,16 +894,16 @@
 (self):%0A
-
         
@@ -921,8 +921,962 @@
 .logger%0A
+%0A    def is_debug(self):%0A        if CONST.CI_DEBUG and CONST.CI_DEBUG.lower() == %22true%22:%0A            return True%0A        return False%0A%0A    def setup_logging(self, default_path=CONST.dir_functest_logging_cfg,%0A                      default_level=logging.INFO,%0A                      env_key='LOG_CFG'):%0A        path = default_path%0A        value = os.getenv(env_key, None)%0A        if value:%0A            path = value%0A        if os.path.exists(path):%0A            with open(path, 'rt') as f:%0A                config = json.load(f)%0A                if (config%5B'handlers'%5D and%0A                        config%5B'handlers'%5D%5B'console'%5D):%0A                    stream_level = logging.INFO%0A                    if self.is_debug():%0A                        stream_level = logging.DEBUG%0A                    config%5B'handlers'%5D%5B'console'%5D%5B'level'%5D = stream_level%0A                logging.config.dictConfig(config)%0A        else:%0A            logging.basicConfig(level=default_level)%0A

Put the Logger as Object oriented.