Пример #1
0
    def _setup_custom_logger(self):
        LOG_PATH = './log'
        try:
            os.mkdir(LOG_PATH)
        except Exception as e:
            pass

        create_folder_if_not_exists(LOG_PATH)

        LOG_FILE = "{0}/{1}".format(LOG_PATH, "migu")

        formatter = logging.Formatter(
            "[%(asctime)s] %(threadName)s - %(filename)s %(funcName)s():%(lineno)d  %(levelname)s \t%(message)s")  # same as default
        # formatter = logging.Formatter('%(asctime)s [%(pathname)s: %(lineno)d] %(levelname)s %(message)s')

        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

        file_handler = ParallelTimedRotatingFileHandler(LOG_FILE, when='H')
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

        # setup `RainbowLoggingHandler`
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)

        # Add file handler
        return logger
Пример #2
0
def setup_console_logging(log_level=None):
    """Setup console logging.

    Aimed to give easy sane defaults for loggig in command line applications.

    Don't use logging settings from INI, but use hardcoded defaults.
    """

    formatter = logging.Formatter("[%(asctime)s] [%(name)s %(funcName)s] %(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.handlers = [handler]

    env_level = os.environ.get("LOG_LEVEL", "info")
    log_level = log_level or getattr(logging, env_level.upper())
    logger.setLevel(log_level)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Пример #3
0
def setup():
    formatter = logging.Formatter(
        "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s"
    )  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(handler)

    if "VERBOSE_TEST" in os.environ:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.ERROR)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    logger = logging.getLogger("cryptoassets.core.backend.blockio")
    logger.setLevel(logging.DEBUG)

    logger = logging.getLogger("cryptoassets.core.backend.bitcoind")
    logger.setLevel(logging.WARN)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Пример #4
0
def setup():
    formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(handler)

    if "VERBOSE_TEST" in os.environ:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.ERROR)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    logger = logging.getLogger("cryptoassets.core.backend.blockio")
    logger.setLevel(logging.DEBUG)

    logger = logging.getLogger("cryptoassets.core.backend.bitcoind")
    logger.setLevel(logging.WARN)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Пример #5
0
def setup_custom_logger():
    LOG_FILE = 'log/cpa'

    formatter = logging.Formatter("[%(asctime)s] %(threadName)s - %(pathname)s %(funcName)s():%(lineno)d  %(levelname)s \t%(message)s")  # same as default
    # formatter = logging.Formatter('%(asctime)s [%(pathname)s: %(lineno)d] %(levelname)s %(message)s')

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    file_handler = ParallelTimedRotatingFileHandler(LOG_FILE, when='H')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    # console_handler = logging.StreamHandler(sys.stdout)
    # console_handler.formatter = formatter
    # logger.addHandler(console_handler)

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    #Add file handler
    handler = TimedRotatingFileHandler(LOG_FILE,
                                       when="h",
                                       interval=1,
                                       backupCount=48)

    # try:
    #     raise RuntimeError("Opa!")
    # except Exception as e:
    #     logger.exception(e)

    return logger
Пример #6
0
def setup_logging() -> None:
    """
    Sets up the logging module to have a verbose option and formats the Console handler and File handler
    """
    logging.addLevelName(VERBOSE, "VERBOSE")
    logging.Logger.verbose = logger_verbose
    logging.verbose = logging_verbose
    logging.VERBOSE = VERBOSE

    # define console handler
    console_handler = ConsoleHandler(sys.stderr)
    if hasattr(console_handler, "_column_color"):
        # noinspection PyProtectedMember
        # pylint: disable=protected-access
        console_handler._column_color['%(message)s'][logging.VERBOSE] = ('cyan', None, False)

    console_formatter = logging.Formatter("[%(asctime)s] %(levelname)s : %(message)s", "%H:%M:%S")
    console_handler.setFormatter(console_formatter)
    console_handler.setLevel(logging.VERBOSE)

    # define file handler
    if not os.path.exists(os.path.dirname(get_global_conf().getdir("install", "log_file"))):
        os.makedirs(os.path.dirname(get_global_conf().getdir("install", "log_file")))
    file_handler = logging.FileHandler(get_global_conf().getdir("install", "log_file"))

    # add handlers
    logging.getLogger().addHandler(console_handler)
    logging.getLogger().addHandler(file_handler)

    logging.getLogger().setLevel(get_global_conf().getint("install", "log_level"))
Пример #7
0
def test_format_all_column():
    logger = logging.getLogger('test_logging_format_all_column')
    logger.setLevel(logging.DEBUG)

    formatter = logging.Formatter('''
        name            = '%(name)s'
        levelno         = '%(levelno)s'
        levelname       = '%(levelname)s'
        pathname        = '%(pathname)s'
        filename        = '%(filename)s'
        module          = '%(module)s'
        lineno          = '%(lineno)d'
        funcName        = '%(funcName)s'
        created         = '%(created)f'
        asctime         = '%(asctime)s'
        msecs           = '%(msecs)d'
        relativeCreated = '%(relativeCreated)d'
        thread          = '%(thread)d'
        threadName      = '%(threadName)s'
        process         = '%(process)d'
        message         = '%(message)s'
    ''')

    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.debug("debug msg")
Пример #8
0
 def _setStreamLoggingHandler(self, formatter):
     handler = RainbowLoggingHandler(
         sys.stderr,
         datefmt='%Y-%m-%d %H:%M:%S',
         color_name=('white', None, False),
         color_levelno=('white', None, False),
         color_levelname=('white', None, False),
         color_pathname=('blue', None, True),
         color_filename=('blue', None, True),
         color_module=('blue', None, True),
         color_lineno=('cyan', None, True),
         color_funcName=('blue', None, True),
         color_created=('white', None, False),
         color_asctime=('black', None, True),
         color_msecs=('white', None, False),
         color_relativeCreated=('white', None, False),
         color_thread=('white', None, False),
         color_threadName=('white', None, False),
         color_process=('black', None, True),
         color_message_debug=('cyan', None, False),
         color_message_info=('white', None, False),
         color_message_warning=('yellow', None, True),
         color_message_error=('red', None, True),
         color_message_critical=('white', 'red', True))
     handler.setFormatter(formatter)
     return handler
Пример #9
0
def setup_console_logging(log_level: t.Optional[str] = None):
    """Setup console logging.

    Aimed to give easy sane defaults for logging in command line applications.

    Don't use logging settings from INI, but use hardcoded defaults.
    """

    formatter = logging.Formatter(
        "[%(asctime)s] [%(name)s %(funcName)s] %(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.handlers = [handler]

    env_level = os.environ.get("LOG_LEVEL", "info")
    log_level = log_level or getattr(logging, env_level.upper())
    logger.setLevel(log_level)

    logger = logging.getLogger("requests.packages.urllib3.connectionpool")
    logger.setLevel(logging.ERROR)

    # SQL Alchemy transactions
    logger = logging.getLogger("txn")
    logger.setLevel(logging.ERROR)
Пример #10
0
def test_format_all_column():
    logger = logging.getLogger('test_logging_format_all_column')
    logger.setLevel(logging.DEBUG)

    formatter = logging.Formatter('''
        name            = '%(name)s'
        levelno         = '%(levelno)s'
        levelname       = '%(levelname)s'
        pathname        = '%(pathname)s'
        filename        = '%(filename)s'
        module          = '%(module)s'
        lineno          = '%(lineno)d'
        funcName        = '%(funcName)s'
        created         = '%(created)f'
        asctime         = '%(asctime)s'
        msecs           = '%(msecs)d'
        relativeCreated = '%(relativeCreated)d'
        thread          = '%(thread)d'
        threadName      = '%(threadName)s'
        process         = '%(process)d'
        message         = '%(message)s'
    ''')

    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.debug("debug msg")
Пример #11
0
def init_logging(level='DEBUG', frmt=None, color=True, **kw):
    try:
        level = int(level)
    except:
        pass
    if not frmt:
        frmt = "%(asctime)-15s:%(levelname)5.5s:%(name)s:%(message)s"
    logging.basicConfig(level=level, format=frmt, **kw)

    if color and sys.stderr.isatty():
        from rainbow_logging_handler import RainbowLoggingHandler

        color_handler = RainbowLoggingHandler(
            sys.stderr,
            color_message_debug=('grey', None, False),
            color_message_info=('blue', None, False),
            color_message_warning=('yellow', None, True),
            color_message_error=('red', None, True),
            color_message_critical=('white', 'red', True),
        )
        formatter = formatter = logging.Formatter(frmt)
        color_handler.setFormatter(formatter)

        ## Be conservative and apply color only when
        #  log-config looks like the "basic".
        #
        rlog = logging.getLogger()
        if rlog.handlers and isinstance(rlog.handlers[0],
                                        logging.StreamHandler):
            rlog.removeHandler(rlog.handlers[0])
            rlog.addHandler(color_handler)
Пример #12
0
def factory(logger_name):
    """Logger factory
    """
    logger = logging.getLogger(logger_name)
    handler = RainbowLoggingHandler(sys.stderr, datefmt=None)
    handler.setFormatter(logging.Formatter("[%(asctime)s] %(filename)s %(funcName)s():%(lineno)d\t%(message)s"))
    logger.addHandler(handler)
    return logger
Пример #13
0
def setup_logging():
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel(logging.INFO)
Пример #14
0
def logger(config_file):
    CONFIG = configparser.RawConfigParser()

    CONFIG.read(config_file)

    logger_config = CONFIG['logger']

    logger = logging.getLogger(logger_config['name'])

    logger.setLevel(LOGGING_LEVELS.get(logger_config['level'], logging.NOTSET))

    formatter = logging.Formatter(
        '[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s'
    )

    handler = RainbowLoggingHandler(
        sys.stderr,
        color_funcName=('black', 'yellow', True)
    )

    handler.setFormatter(formatter)

    logger.addHandler(handler)

    if CONFIG.has_section('file_logger'):
        file_logger_config = CONFIG['file_logger']

        fh = logging.handlers.RotatingFileHandler(
            file_logger_config['file_path'],
            maxBytes=int(file_logger_config['max_log_file_size']),
            backupCount=int(file_logger_config['backup_count'])
        )

        fh.setLevel(LOGGING_LEVELS.get(file_logger_config['level'],
                                       logging.NOTSET))
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    if CONFIG.has_section('email_logger'):
        email_logger_config = CONFIG['email_logger']

        eh = logging.handlers.SMTPHandler(
            (email_logger_config['host'], email_logger_config['port']),
            email_logger_config['from'],
            email_logger_config['to'],
            email_logger_config['subject'],
            (email_logger_config['username'], email_logger_config['password']),
            ()
        )

        eh.setLevel(LOGGING_LEVELS.get(email_logger_config['level'],
                                       logging.NOTSET))
        eh.setFormatter(formatter)
        logger.addHandler(eh)

    return logger
Пример #15
0
    def __init__(self, device=None, username=None, password=None, verify_cert=False, domain='Global', loglevel=20):
        self.logger = logging.getLogger('FIREPOWER')
        self.logger.setLevel(loglevel)
        formatter = logging.Formatter('%(asctime)s [%(name)s] [%(levelname)s] %(message)s')
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)

        self.api = FireREST(device=device, username=username, password=password, verify_cert=verify_cert,
                            loglevel=loglevel)
        self.domain = domain
Пример #16
0
def setup_logging(verbose=False):
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel([logging.INFO, logging.DEBUG][verbose])

    logging.getLogger("boto").level = logging.CRITICAL
    def __init__(self,
                 checker: AbstractChecker,
                 coverage_threshold,
                 time_interval_count,
                 battery_threshold,
                 log=None,
                 improvement_score_limit=1):
        """
        Constructs a solution for the volatile resources problem based on the fractional optimal solution
        for the inherent bin packing problem as defined by Cambazard, et. al. -- Bin Packing with Linear Usage
        Costs - An Application to Energy Management in Data Centres, https://hal.archives-ouvertes.fr/hal-00858159

        :param checker:
        """
        super(ConstructiveMapperFromFractional, self).__init__(checker)
        # NOTE: names starting with __ are sort of private methods in python
        self.__checker = checker
        self.bins = []
        self.items = []
        self.pruning_steps_collection = [PruneLocalityConstraints()]
        self.objective_value_of_fractional_opt = None
        self.objective_value_of_integer_solution = None
        if log is None:
            self.log = logging.Logger(self.__class__.__name__)
            handler = RainbowLoggingHandler(sys.stderr,
                                            color_funcName=('black', 'yellow',
                                                            True))
            formatter = logging.Formatter(
                '%(asctime)s.%(name)s.%(levelname).3s: %(message)s')
            handler.setFormatter(formatter)
            self.log.addHandler(handler)
            self.log.setLevel(logging.INFO)
        else:
            self.log = log.getChild(self.__class__.__name__)
            for handler in log.handlers:
                self.log.addHandler(handler)
            self.log.setLevel(log.getEffectiveLevel())

        # these might not be needed if we override the functions with other heuristics.
        self.EPSILON = 1e-5
        self.min_bin_preference = None
        self.improvement_score_limit = improvement_score_limit
        # compulsory parameters
        self.time_interval_count = time_interval_count
        self.battery_threshold = battery_threshold
        self.coverage_threshold = coverage_threshold
        # variable to communicate between the new best bins and the item move improvement step functions
        self.possible_bins_needed = []
        self.infra = None
        self.ns = None
        # list of BinCapacityViolationChecker objects instantiated in the save_global_mapping_task_information fucntion
        self.violation_checkers = None
        self.hashes_of_visited_mappings = set()
        self.chosen_ap_ids = InvalidableAPSelectionStruct()
Пример #18
0
def add_stdout_handler(logger, formatter):
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=(
            'black', 'black', True))
    except ImportError:
        handler = logging.StreamHandler()
        pass

    handler.setFormatter(formatter)
    logger.addHandler(handler)
Пример #19
0
def set_up_logger():
    logger: logging.Logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter(
        "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
    handler = RainbowLoggingHandler(stderr,
                                    color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.raiseExceptions = False
    return logger
Пример #20
0
    def __init__(self,
                 device=None,
                 username=None,
                 password=None,
                 verify_cert=False,
                 timeout=120,
                 loglevel=20,
                 rate_limit=9):

        self.logger = logging.getLogger('FireREST')
        self.logger.setLevel(loglevel)
        formatter = logging.Formatter(
            '%(asctime)s [%(name)s] [%(levelname)s] %(message)s')
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'yellow',
                                                        True))
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)

        self.device = device
        self.username = username
        self.password = password
        self.verify_cert = verify_cert
        self.timeout = timeout
        self.cred = HTTPBasicAuth(self.username, self.password)
        self.api_platform_request_url = API_PLATFORM_REQ_URL
        self.api_config_request_url = API_CONFIG_REQ_URL
        self.headers = HEADERS
        self.url_policy = {
            'accesspolicy': 'accesspolicies',
            'filepolicy': 'filepolicies',
            'intrusionpolicy': 'intrusionpolicies',
            'snmpalert': 'snmpalerts',
            'syslogalert': 'syslogalerts'
        }
        self.rate_limit = rate_limit
        self.rate_limit_count = 0

        try:
            request = requests.post('https://' + self.device + API_AUTH_URL,
                                    headers=HEADERS,
                                    auth=self.cred,
                                    verify=self.verify_cert)
            self.token = request.headers.get('X-auth-access-token',
                                             default=None)
            self.domains = json.loads(
                request.headers.get('DOMAINS', default=None))
            self.headers['X-auth-access-token'] = self.token
        except Exception as error:
            self.logger.error(
                'Could not generate Authentication Token, check connection parameters'
            )
            self.logger.error('Exception: %s ' % error.message)
            sys.exit()
Пример #21
0
def add_stdout_handler(logger, formatter):
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=(
            'black', 'black', True))
    except ImportError:
        handler = logging.StreamHandler()
        pass

    handler.setFormatter(formatter)
    logger.addHandler(handler)
Пример #22
0
def setup_logging():

    # Setup Python root logger to DEBUG level
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s"
    )  # same as default

    # Add colored log handlign to sys.stderr
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
Пример #23
0
    def __init__(
            self,
            name: str,
            level: int = logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'):
        super().__init__(name, level)

        formatter = logging.Formatter(format)

        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'yellow',
                                                        True))
        handler.setFormatter(formatter)
        self.addHandler(handler)
Пример #24
0
def setup_custom_logger(szPath="", szFileName="logger"):
    global g_logger

    if g_logger is not None:
        for handler in g_logger.handlers:
            g_logger.removeHandler(handler)

    LOG_PATH = "."
    if szPath is None or 0 == len(szPath):
        LOG_PATH = '.'

    create_folder_if_not_exists(LOG_PATH)

    LOG_FILE = "{0}/{1}".format(LOG_PATH, szFileName)

    formatter = logging.Formatter(
        "[%(asctime)s] %(threadName)s - %(pathname)s %(funcName)s():%(lineno)d  %(levelname)s \t%(message)s"
    )  # same as default
    # formatter = logging.Formatter('%(asctime)s [%(pathname)s: %(lineno)d] %(levelname)s %(message)s')

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    file_handler = ParallelTimedRotatingFileHandler(LOG_FILE, when='H')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    # console_handler = logging.StreamHandler(sys.stdout)
    # console_handler.formatter = formatter
    # logger.addHandler(console_handler)

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr,
                                    color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    #Add file handler
    handler = TimedRotatingFileHandler(LOG_FILE,
                                       when="h",
                                       interval=1,
                                       backupCount=48)

    # try:
    #     raise RuntimeError("Opa!")
    # except Exception as e:
    #     logger.exception(e)

    g_logger = logger
    return logger
Пример #25
0
def setup_logging(logfile=None, verbose=False):

    if logfile:
        if os.path.exists(os.path.dirname(logfile)):

            # Setup default file logging and set the handler to recieve everything
            fh = logging.FileHandler(logfile)
            fh.setFormatter(logging.Formatter(CONST_LOG_FORMAT_FILE))
            fh.setLevel(logging.INFO)
            log.addHandler(fh)
        else:

            raise ("log directory does not exist (" +
                   os.path.dirname(logfile) + ")")

            clean_up(-1)

    # Add a log handler for stdout and set the handler to recieve everything
    csh = RainbowLoggingHandler(sys.stderr,
                                color_funcName=('black', 'yellow', True))
    csh.setFormatter(logging.Formatter(CONST_LOG_FORMAT_CONSOLE))
    csh.setLevel(logging.DEBUG)
    log.addHandler(csh)

    # Now set the root logger to INFO
    log.setLevel(logging.INFO)

    # Check for verbose logging enabled
    if verbose is True:
        log.setLevel(logging.DEBUG)
        if logfile:
            fh.setLevel(logging.DEBUG)
        csh.setLevel(logging.DEBUG)
        log.debug('Debug logging enabled')
    return log
Пример #26
0
    def __init__(self, module_name):
        self.logger_tmp = None
        logger = logging.getLogger(module_name)
        logging.basicConfig(filename='/tmp/r2als.log',level=logging.DEBUG,format='%(asctime)s %(levelname)s %(message)s')
        # logging.basicConfig(level=logging.DEBUG)
        formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default

         # setup `RainbowLoggingHandler`
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)

        logger.addHandler(handler)

        self.logger_tmp = logger
Пример #27
0
def setup_logging(verbose=False, silent=False, debug=False):
    log = logging.getLogger("")
    handler = RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
    handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
    log.addHandler(handler)
    log.setLevel([logging.INFO, logging.DEBUG][verbose or debug])
    if silent:
        log.setLevel(logging.ERROR)

    logging.getLogger("requests").setLevel([logging.CRITICAL, logging.ERROR][verbose or debug])
    return handler
Пример #28
0
def test_custom_format():
    logger = logging.getLogger('test_logging_custom_format')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("MESSAGE ONLY => %(message)s")

    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.debug("debug msg")
    logger.info("info msg")
    logger.warn("warn msg")
    logger.error("error msg")
    logger.critical("critical msg")
Пример #29
0
def test_custom_format():
    logger = logging.getLogger('test_logging_custom_format')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("MESSAGE ONLY => %(message)s")

    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.debug("debug msg")
    logger.info("info msg")
    logger.warn("warn msg")
    logger.error("error msg")
    logger.critical("critical msg")
Пример #30
0
def setup():
    # prepare log file
    global f_log, logpath
    (fd, logpath) = mkstemp(prefix='raibow-', suffix='.txt')
    f_log = os.fdopen(fd, 'w')

    # prepare 'test_format' logger
    logger = logging.getLogger('test_format')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("AAA %(name)s - %(levelname)s - %(message)s ZZZ")  # testing whether this format is used

    handler = RainbowLoggingHandler(f_log)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
Пример #31
0
def get_logger():
    global logger
    
    if logger == None:
        logger = logging.getLogger('freesprints')
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default
        
        # setup colored logging
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    
    return logger
Пример #32
0
def add_stdout_handler(logger):
    formatter = Formatter(
        "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
    try:
        # Si está instalado rainbow_logging_handler (coloriza el output
        # de consola) lo usamos, sino defaulteamos al módulo logging
        from rainbow_logging_handler import RainbowLoggingHandler
        handler = RainbowLoggingHandler(sys.stderr, color_funcName=(
            'black', 'black', True))
    except ImportError:
        handler = StreamHandler()
        pass

    handler.setFormatter(formatter)
    logger.addHandler(handler)
Пример #33
0
    def setup_logging(self, args, verbose=False, silent=False, debug=False, logging_name=""):
        """Setup the RainbowLoggingHandler for the logs and call setup_other_logging"""
        log = logging.getLogger(logging_name)
        handler = RainbowLoggingHandler(self.logging_handler_file)
        handler._column_color['%(asctime)s'] = ('cyan', None, False)
        handler._column_color['%(levelname)-7s'] = ('green', None, False)
        handler._column_color['%(message)s'][logging.INFO] = ('blue', None, False)
        handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)-15s %(message)s"))
        log.addHandler(handler)
        log.setLevel([logging.INFO, logging.DEBUG][verbose or debug])
        if silent:
            log.setLevel(logging.ERROR)

        self.setup_other_logging(args, verbose, silent, debug)
        return handler
Пример #34
0
def setup_TerminalLogger(log_level, logger_name='TerminalLogger'):
    """Setup colorful logger

    *Usage*

    .. code-block:: python
        setup_TerminalLogger(logging.DEBUG)
        logger = logging.getLogger('TerminalLogger')
        logger.debug('hello')
    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    handler = RainbowLoggingHandler(sys.stderr, datefmt=None)
    handler.setFormatter(logging.Formatter("[%(asctime)s] %(filename)s %(funcName)s():%(lineno)d\t%(message)s"))
    logger.addHandler(handler)
Пример #35
0
def execute(args=None, parser=None):
    parser = build_parser(parent=parser)
    parser.add_argument(
        '--max-cycles',
        metavar='N',
        default=None,
        type=int,
        help=
        'limit number of fuzz job cycles to %(metavar)s (default: no limit)')
    arguments = parser.parse_args(args)

    logger = logging.getLogger('fuzzinator')
    logger.addHandler(RainbowLoggingHandler(sys.stdout))
    logger.setLevel(arguments.log_level)

    config = configparser.ConfigParser(
        interpolation=configparser.ExtendedInterpolation())
    config.read(arguments.config)

    controller = Controller(config=config)
    controller.listener = CliListener()

    try:
        controller.run(max_cycles=arguments.max_cycles)
    except KeyboardInterrupt:
        pass
Пример #36
0
def setup():
    # prepare log file
    global f_log, logpath
    (fd, logpath) = mkstemp(prefix='raibow-', suffix='.txt')
    f_log = os.fdopen(fd, 'w')

    # prepare 'test_format' logger
    logger = logging.getLogger('test_format')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        "AAA %(name)s - %(levelname)s - %(message)s ZZZ"
    )  # testing whether this format is used

    handler = RainbowLoggingHandler(f_log)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
Пример #37
0
def colorlogs():
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        formatter = logging.Formatter(
            "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
        # ^^^ same as default
        handler = RainbowLoggingHandler(
            sys.stderr, color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
Пример #38
0
def _setupLogging(stream, level, color=True): # pragma: no cover
    "Setup logging according to the command line parameters"
    if isinstance(stream, str):
        class Stream(file):
            """
            File subclass that allows RainbowLoggingHandler to write
            with colors
            """
            def isatty(self):
                return color

        stream = Stream(stream, 'ab', buffering=1)

    from rainbow_logging_handler import RainbowLoggingHandler
    rainbow_stream_handler = RainbowLoggingHandler(
        stream,
        #  Customizing each column's color
        # pylint: disable=bad-whitespace
        color_asctime          = ('dim white',  'black'),
        color_name             = ('dim white',  'black'),
        color_funcName         = ('green',      'black'),
        color_lineno           = ('dim white',  'black'),
        color_pathname         = ('black',      'red'),
        color_module           = ('yellow',     None),
        color_message_debug    = ('color_59',   None),
        color_message_info     = (None,         None),
        color_message_warning  = ('color_226',  None),
        color_message_error    = ('red',        None),
        color_message_critical = ('bold white', 'red'))
        # pylint: enable=bad-whitespace

    logging.root.addHandler(rainbow_stream_handler)
    logging.root.setLevel(level)
Пример #39
0
def colorlogs():
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        formatter = logging.Formatter(
            "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
        # ^^^ same as default
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
Пример #40
0
    def __init__(self, name, mode, maxsize, rotate):
        logging.Handler.__init__(self)

        #self._handler = RotatingFileHandler(name, mode, maxsize, rotate)
        formatter = logging.Formatter(
            "[%(asctime)s.%(msecs)03d] %(filename)16s Line %(lineno)3d %(funcName)s():\t %(message)s"
        )
        self._handler = RainbowLoggingHandler(sys.stderr,
                                              color_funcName=('green', 'none',
                                                              True))
        self._handler.setFormatter(formatter)

        self.queue = multiprocessing.Queue(-1)

        t = threading.Thread(target=self.receive)
        t.daemon = True
        t.start()
Пример #41
0
    def __init__(self,
                 device=None,
                 username=None,
                 password=None,
                 verify_cert=False,
                 loglevel=20):
        self.logger = logging.getLogger('ASA')
        self.logger.setLevel(loglevel)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s: %(message)s')
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'yellow',
                                                        True))
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)

        self.api = AsaREST(device, username, password, verify_cert)
Пример #42
0
 def __init__(self):
     """
     初始化类
     """
     self.formatter = logging.Formatter(
         '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
     )
     self.local_time = time.strftime("%Y%m%d-%H%M%S")
     self.path = 'result\log'
     if not os.path.exists(self.path):
         os.makedirs(self.path)
     # 日志文件
     self.file_handler = logging.FileHandler('result\log\Testlog_%s.log' %
                                             self.local_time)
     # 控制台日志
     self.color_handler = RainbowLoggingHandler(sys.stderr,
                                                color_funcName=('black',
                                                                'yellow',
                                                                True))
Пример #43
0
def main_func():

    # Check the number of inputs
    numofinputs = len(sys.argv)

    # Conditional expressions
    if numofinputs > 5 :
        print ("Invalid arguments \nGood BYE")
    else :
        # Get the input arguments
        filename = argv.pop(1) # EXE name, File Name, Module Name
        operation = argv.pop(1) # DEBUG, INFO, WARN, ERROR, CRITICAL, EXCEPTION
        message = argv.pop(1) # Message to be printed
        logFile = argv.pop(1) # Log file

        # setup `logging` module
        logger = logging.getLogger(filename)
        fileHandler = logging.FileHandler('Logger.log')
        logger.setLevel(logging.INFO)
        formatter = logging.Formatter("[%(asctime)s] [%(name)s] : %(message)s")

        # setup `RainbowLoggingHandler`
        handler = RainbowLoggingHandler(sys.stderr, color_name=('grey', None , True),color_asctime=('green', None  , True))
        handler.setFormatter(formatter)
        fileHandler.setFormatter(formatter)
        logger.addHandler(fileHandler)
        logger.addHandler(handler)

        if operation == 'DEBUG' :
            logger.debug(message)
        if operation == 'INFO' :
            logger.info(message)
        if operation == 'WARN' :
            logger.warn(message)
        if operation == 'ERROR' :
            logger.error(message)
        if operation == 'CRITICAL' :
            logger.critical(message)
        if operation == 'EXCEPTION' :
            raise RuntimeError("InstallException!")
            logger.exception(message)
            logger.exception(e)
Пример #44
0
def colorlogs(format="short"):
    """Append a rainbow logging handler and a formatter to the root logger"""
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        # same as default
        if format == "short":
            fmt = "%(message)s "
        else:
            fmt = "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s [%(levelname)s]"
        formatter = logging.Formatter(fmt)
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
Пример #45
0
def setup_console_logging(log_level=None):
    """Setup console logging.

    Aimed to give easy sane defaults for loggig in command line applications.

    Don't use logging settings from INI, but use hardcoded defaults.
    """

    formatter = logging.Formatter("[%(asctime)s] [%(name)s %(funcName)s:%(lineno)d] %(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    # and quiet some logs for the test output
    handler = RainbowLoggingHandler(sys.stdout)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.handlers = [handler]

    env_level = os.environ.get("LOG_LEVEL", "info")
    log_level = log_level or getattr(logging, env_level.upper())
    logger.setLevel(log_level)
Пример #46
0
def execute(arguments):
    if not root_logger.hasHandlers():
        root_logger.addHandler(RainbowLoggingHandler(sys.stdout))

    controller = Controller(config=arguments.config)
    controller.listener += CliListener()

    try:
        controller.run(max_cycles=arguments.max_cycles)
    except KeyboardInterrupt:
        Controller.kill_process_tree(os.getpid(), kill_root=False)
Пример #47
0
def colorlogs(format="short"):
    """Append a rainbow logging handler and a formatter to the root logger"""
    try:
        from rainbow_logging_handler import RainbowLoggingHandler
        import sys
        # setup `RainbowLoggingHandler`
        logger = logging.root
        # same as default
        if format == "short":
            fmt = "%(message)s "
        else:
            fmt = "[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s [%(levelname)s]"
        formatter = logging.Formatter(fmt)
        handler = RainbowLoggingHandler(sys.stderr,
                                        color_funcName=('black', 'gray', True))
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    except ImportError:
        # rainbow logger not found, that's ok
        pass
def test_usage():
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    handler = RainbowLoggingHandler(sys.stderr)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler.setFormatter(formatter)
    root_logger.addHandler(handler)
    logger = logging.getLogger('test')

    logger.debug("debug msg")
    logger.info("info msg")
    logger.warn("warn msg")
    logger.error("error msg")
    logger.critical("critical msg")

    try:
        raise RuntimeError("Opa!")
    except Exception as e:
        logger.exception(e)
Пример #49
0
def get_logger(name):
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    handler = logging.FileHandler(os.path.join(config.logs_dir, '{}.log'.format(name)))
    handler.setFormatter(log_formatter)
    handler.setLevel(logging.DEBUG)

    handler2 = RainbowLoggingHandler(
            sys.stdout,
            '%Y-%m-%d %H:%M:%S',
            color_asctime=('white', None, False)
    )
    handler2.setFormatter(log_formatter)
    handler2.setLevel(logging.DEBUG)

    logger.addHandler(handler)
    logger.addHandler(handler2)

    return logger
Пример #50
0
 def __init__(self, application_object, plugin_object):
     # setup logger
     self.logger = logging.getLogger('Needles')
     self.logger.setLevel(logging.DEBUG)
     formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")  # same as default
     
     # setup colored logging
     handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
     handler.setFormatter(formatter)
     self.logger.addHandler(handler)
     
     self.logger.debug("init in Needles plugin")
     self.application = application_object
     self.plugin_object = plugin_object
     self.display_surface = self.application.get_window_surface()
     
     font_path = "./fonts/Cave-Story.ttf"
     distance_font_path = "./fonts/Warenhaus-Standard.ttf"
     self.font_big = pygame.font.Font(distance_font_path, 68)
     self.font_distance = pygame.font.Font(distance_font_path, 50)
Пример #51
0
def setup_custom_logger():
    LOG_PATH = 'log'
    FileUtils.create_folder_if_not_exists(LOG_PATH)

    LOG_FILE = "{0}/{1}".format(LOG_PATH, "thinklog")

    # logging.Formatter.converter = time.gmtime
    formatter = logging.Formatter(
        "[%(asctime)s] %(threadName)s - %(pathname)s %(funcName)s():%(lineno)d  %(levelname)s \t%(message)s"
    )  # same as default
    # formatter = logging.Formatter('%(asctime)s [%(pathname)s: %(lineno)d] %(levelname)s %(message)s')

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    file_handler = ParallelTimedRotatingFileHandler(LOG_FILE, when='H')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    # console_handler = logging.StreamHandler(sys.stdout)
    # console_handler.formatter = formatter
    # logger.addHandler(console_handler)

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr,
                                    color_funcName=('black', 'yellow', True))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    #Add file handler
    handler = TimedRotatingFileHandler(LOG_FILE,
                                       when="h",
                                       interval=1,
                                       backupCount=24)

    # try:
    #     raise RuntimeError("Opa!")
    # except Exception as e:
    #     logger.exception(e)

    return logger
Пример #52
0
def enable_logging(filename: str, level: str):
    class Squelch(logging.Filter):
        def filter(self, record):
            if record.levelno == logging.DEBUG:
                return not record.name.startswith('asyncio') and \
                       not record.name.startswith('websockets') and \
                       not record.name.startswith('aiohttp')
            elif record.levelno == logging.INFO and record.name.startswith('asyncio'):
                return False
            return True

    formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d:%(levelname)s:%(name)s:%(message)s')

    # File logger captures everything.
    file_handler = logging.FileHandler(filename)
    file_handler.setLevel(logging.DEBUG)

    # Console output level is configurable.
    stream_handler = RainbowLoggingHandler(
        sys.stdout,
        color_asctime=('cyan', None, False),
        color_msecs=('cyan', None, False),
        color_levelname=('gray', None, False),
        color_module=('yellow', None, False),
        color_name=('blue', None, False),
        color_lineno=('green', None, False),
        datefmt="%Y-%m-%d %H:%M:%S"
    )
    stream_handler.setLevel(getattr(logging, level))
    stream_handler.addFilter(Squelch())

    # Set an output format.
    stream_handler.setFormatter(formatter)
    file_handler.setFormatter(formatter)
    file_handler.addFilter(Squelch())

    # Add handlers to root.
    root = logging.getLogger('')
    root.setLevel(logging.DEBUG)
    root.addHandler(stream_handler)
    root.addHandler(file_handler)
Пример #53
0
import logging, traceback, sys
from rainbow_logging_handler import RainbowLoggingHandler

# logging.basicConfig(level=logging.DEBUG, format='%(filename)-15s:%(lineno)-4d %(levelname)s: %(message)s')
handler = RainbowLoggingHandler(sys.stderr)
handler.setFormatter(logging.Formatter("%(filename)s:%(lineno)d\t%(levelname)s: %(message)s"))
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

logging.getLogger("OpenGL").setLevel(99)

# allow importing from the repository root
import sys, os, json

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import apc.baxter_scoop as baxter
from integration.visualization import debug_cloud
from integration.camera import uvtexture, invalid_mask
from integration.io import pcd
from perception.segmentation.color import rgb2yuv, make_uv_hist

from klampt.robotsim import WorldModel
from klampt import se3, so3
from time import sleep, time
import numpy, math
from matplotlib import pyplot


def load_histogram(obj, n):
Пример #54
0
import logging
from rainbow_logging_handler import RainbowLoggingHandler
import datetime

if __name__ == "__main__":
    # logger取得
    logger = logging.getLogger("log")

    # レベル設定
    logger.setLevel(logging.DEBUG)

    # 出力フォーマット
    formatter = logging.Formatter(fmt="%(asctime)s %(message)s")

    # 色付け出力ハンドラの登録
    rainbow_handler = RainbowLoggingHandler(sys.stderr, color_message_info=("green", None, True))
    rainbow_handler.setFormatter(formatter)
    logger.addHandler(rainbow_handler)

    # ファイル出力用ハンドラの登録
    date_obj = datetime.datetime.now()
    date_str = date_obj.strftime("%Y-%m-%d-%H:%M:%S")
    logfilename = "./log/%s.log" % (date_str)
    file_handler = logging.FileHandler(logfilename, "w")
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    logger.debug("this is debug")
    logger.info("this is info")
    logger.warning("this is warning")
    logger.error("this is error")
Пример #55
0
def initialize_logger(args, script_name, syslog_server=None):
    """
    Initialize the logging instance with the values provided
    by the user and return the logging object.

    """
    LOGGER = logging.getLogger(script_name)

    FORMATTER = logging.Formatter(
        ""+script_name+" : "+str(getpass.getuser())+" : %(asctime)s : %(levelname)s : %(message)s",
                                  datefmt='%m/%d/%Y %I:%M:%S %p')

    # create console handler and set level to info
    logging.StreamHandler()

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'white', True))
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    # setup log level from arguments
    if args.log:
        if contains('DEBUG', args.log.upper()):
            handler.setLevel(logging.DEBUG)
            LOGGER.setLevel(logging.DEBUG)
        elif contains('WARNING', args.log.upper()):
            handler.setLevel(logging.WARNING)
            LOGGER.setLevel(logging.WARNING)
        elif contains('ERROR', args.log.upper()):
            handler.setLevel(logging.ERROR)
            LOGGER.setLevel(logging.ERROR)
        else:
            handler.setLevel(logging.INFO)
            LOGGER.setLevel(logging.INFO)
        LOGGER.info('Logging level has been set to {}'.format(args.log.upper()))

    # setup logging directory to store log files
    if args.log_dir:

        if not os.path.isdir(args.log_dir):
            os.makedirs(args.log_dir)
        output_dir = args.log_dir

    else:
        output_dir = create_log_dir()

    LOGGER.info('Logging directory has been set to {}'.format(output_dir))


    # create optional syslog handler, if the argument has been supplied to support it
    if args.syslog:

        DESIG_SYSLOG_SERVER = syslog_server

        handler = logging.handlers.SysLogHandler(address=(DESIG_SYSLOG_SERVER,
                                                          logging.handlers.SYSLOG_UDP_PORT))
        syslog_format = logging.Formatter(
            '[appname]: %(name)s: [alias]: '+str(getpass.getuser())+' %(message)s')
        handler.setFormatter(syslog_format)

        if args.syslog_level:
            # Set syslog level to the user specified level
            if args.syslog_level == 'info':
                handler.setLevel(logging.INFO)
            elif args.syslog_level == 'warning':
                handler.setLevel(logging.WARNING)
            elif args.syslog_level == 'error':
                handler.setLevel(logging.ERROR)

        LOGGER.info(
            'Syslog has been enabled for [{}] logging level, sent to syslog server [{}]'.format(
                                                                            args.syslog_level,
                                                                            DESIG_SYSLOG_SERVER))

        # Add syslog handler to logging object
        LOGGER.addHandler(handler)

    # create error file handler and set level to error
    handler = logging.FileHandler(os.path.join(
        output_dir, "error.log"),"a", encoding=None, delay="true")
    handler.setLevel(logging.ERROR)
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    # create debug file handler and set level to debug
    handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"a")
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    if args.logfile_rotate:
        '''
        User has specified they would like to create a daily rotating logfile.
        Create an instance and DO NOT create a single serving logfile.
        '''
        LOGGER.info('Logfiles are now set to auto-rotate at midnight UTC.')
        filepath_and_name_format = ('{}/{}'.format(output_dir, script_name.replace('.py','')))
        LOGGER.info('Logfiles are now being written at {}'.format(filepath_and_name_format))

        log_filename=filepath_and_name_format
        # maxBytes takes the max file size in MB and bit-shift converts to bytes
        handler=SizedTimedRotatingFileHandler(
            log_filename, maxBytes=args.logsize<<20)
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(FORMATTER)
        handler.suffix = "%Y-%m-%d.log"
        LOGGER.addHandler(handler)

    else:
        # create individual execution file handler and set level to debug
        now = datetime.datetime.now()
        datetime_stamp = now.strftime("%Y-%m-%d_%H-%M-%S")
        datetime_stamp = ('{}_{}.log'.format(datetime_stamp, script_name.replace('.py','')))
        filename = os.path.join(output_dir, datetime_stamp)

        handler = logging.FileHandler(filename, "a")

        if contains('DEBUG', args.log.upper()):
            handler.setLevel(logging.DEBUG)
        elif contains('WARNING', args.log.upper()):
            handler.setLevel(logging.WARNING)
        elif contains('ERROR', args.log.upper()):
            handler.setLevel(logging.ERROR)
        else:
            handler.setLevel(logging.INFO)

        handler.setFormatter(FORMATTER)
        LOGGER.addHandler(handler)

        # display the exact file name / path to the user
        LOGGER.info('Logs for this session now being written to {}'.format(filename))

        # Attach filepath / filename string to logger
        LOGGER.filename = filename

    return LOGGER
Пример #56
0
"""
Initialise the application.

@author: Chris Scott

"""
import sys
import platform
import logging
try:
    from rainbow_logging_handler import RainbowLoggingHandler
    root = logging.getLogger()
    root.setLevel(logging.NOTSET)
    formatter = logging.Formatter("%(name)s:%(funcName)s():%(lineno)d: %(message)s")
    handler = RainbowLoggingHandler(sys.stderr)
    handler.setFormatter(formatter)
    root.addHandler(handler)
except ImportError:
    # configure logging (we have to set logging.NOTSET here as global for root logger)
    logging.basicConfig(format="%(levelname)s: %(name)s: %(message)s", level=logging.NOTSET)

from PySide import QtGui, QtCore

# set default for stream handler (we don't want it to be NOTSET by default)
_argLevel = None
if len(sys.argv) > 1:
    if "DEBUG" in sys.argv:
        _argLevel = logging.DEBUG
    elif "INFO" in sys.argv:
        _argLevel = logging.INFO
Пример #57
0
import logging, sys
from rainbow_logging_handler import RainbowLoggingHandler
logging.VERBOSE = VERBOSE = 5
logging.SUCCESS = SUCCESS = logging.INFO + 5
logging.addLevelName(VERBOSE, "VERBOSE")
logging.addLevelName(SUCCESS, "SUCCESS")
def verbose(self, message, *args, **kws):
    # Yes, logger takes its '*args' as 'args'.
    if self.isEnabledFor(VERBOSE):
        self._log(VERBOSE, message, args, **kws)
logging.Logger.verbose = verbose
logging.Logger.v = verbose
def success(self, message, *args, **kws):
    # Yes, logger takes its '*args' as 'args'.
    if self.isEnabledFor(SUCCESS):
        self._log(SUCCESS, message, args, **kws)
logging.Logger.success = success
logging.Logger.ok = success
logger = logging.getLogger('dj')
formatter = logging.Formatter("[%(asctime)s] %(name)s:\t%(message)s")  # same as default
# setup `RainbowLoggingHandler`
handler = RainbowLoggingHandler(sys.stderr, color_funcName=('black', 'yellow', True))
handler.setFormatter(formatter)
handler._column_color['%(message)s'][VERBOSE] =  ('magenta', None, False)
handler._column_color['%(message)s'][SUCCESS] =  ('green', None, False)
logger.addHandler(handler)
logger.setLevel(VERBOSE)
Пример #58
0
Файл: run.py Проект: eiriks/nrk
    log_level = logging.DEBUG
elif args.verbose[0] == "NOTSET":
    log_level = logging.NOTSET
else: # aka default - just the crazy stuff
    log_level = logging.CRITICAL    


# set up logging to file 
logging.basicConfig(level=logging.DEBUG,
                    format='[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='spam.log',
                    filemode='w')

# define a Handler which writes INFO messages or higher to the sys.stderr
console = RainbowLoggingHandler(sys.stdout) #logging.StreamHandler() 
# handler = RainbowLoggingHandler(sys.stdout)

# get level from command input
console.setLevel(log_level)

formatter = logging.Formatter("[%(asctime)s] %(name)s %(funcName)s():%(lineno)d\t%(message)s")
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
logger = logging.getLogger('nrk2013')
# tell requests lib to chill out
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)