Esempio n. 1
0
 def get_level(level):
     """Get log level from a string."""
     if level.upper() not in LogLevel.levels.keys():
         rwt(
             AttributeError('Log level not found: {0}'.format(
                 level.upper())))
     return LogLevel.levels[level.upper()]
Esempio n. 2
0
def setup_logger(log_directory='.',
                 file_handler_type=HandlerType.TIME_ROTATING_FILE_HANDLER,
                 allow_console_logging=True,
                 allow_file_logging=True,
                 backup_count=100,
                 max_file_size_bytes=100000,
                 when_to_rotate='D',
                 change_log_level=None,
                 log_formatter=Formatters.TextFormatter,
                 gelf_handler=None):
    """Set up the global logging settings.

    Args:
        log_directory (str)            :directory to write log files to. Applicable only when `allow_file_logging` = True
        file_handler_type              :object of logging handler from HandlerType class. Applicable only when `allow_file_logging` = True
        allow_console_logging (bool)   :Turn off/on the console logging.
        allow_file_logging (bool)      :Turn off/on if logs need to go in files as well.
        backup_count (int)             :Number of files to backup before rotating the logs.
        max_file_size_bytes (int)      :Size of file in bytes before rotating the file. Applicable only to ROTATING_FILE_HANDLER.
        when_to_rotate (str)           :Duration after which a file can be rotated. Applicable only to TIME_ROTATING_FILE_HANDLER
                                        Accepts following values:
                                        'S'	Seconds
                                        'M'	Minutes
                                        'H'	Hours
                                        'D'	Days
                                        'W0'-'W6'	Weekday (0=Monday)
                                        'midnight'	Roll over at midnight
        change_log_level (dict)        :A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} )
        gelf_handler                   :An external handler for graylog data publishing.
    """
    if file_handler_type not in [
            HandlerType.ROTATING_FILE_HANDLER,
            HandlerType.TIME_ROTATING_FILE_HANDLER
    ]:
        rwt(ValueError('Please pass an object of HandlerType class'))

    if change_log_level:
        __set_log_levels(change_log_level)

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

    if gelf_handler:
        logger.addHandler(gelf_handler)

    # create console handler and set level to info
    if allow_console_logging:
        handler = logging.StreamHandler()
        handler.setLevel(logging.INFO)
        handler.setFormatter(log_formatter)
        logger.addHandler(handler)

    if allow_file_logging:
        __setup_file_logging(g_logger=logger,
                             log_directory=log_directory,
                             file_handler_type=file_handler_type,
                             backup_count=backup_count,
                             max_file_size_bytes=max_file_size_bytes,
                             when_to_rotate=when_to_rotate)
Esempio n. 3
0
def __set_log_levels(level_dict):
    """Set the log levels for any log-handler for e.g. level_dict = {'requests':'error'}."""
    if not isinstance(level_dict, dict):
        rwt(TypeError("Expecting dict object with format for e.g. {'requests':'warning'} \n"
                      "Available levels are: {0}".format(LogLevel.levels.keys)))

    for key, val in iteritems(level_dict):
        logging.getLogger(key).setLevel(LogLevel.get_level(val))
Esempio n. 4
0
def setup_logger(log_directory='.',
                 file_handler_type=HandlerType.TIME_ROTATING_FILE_HANDLER,
                 allow_console_logging=True,
                 allow_file_logging=True,
                 backup_count=100,
                 max_file_size_bytes=100000,
                 when_to_rotate='D',
                 change_log_level=None,
                 log_formatter=Formatters.TextFormatter,
                 gelf_handler=None,
                 file_log_level=logging.DEBUG,
                 log_tags=None,
                 **kwargs):
    """Set up the global logging settings.

    Args:
        log_directory (str)            :directory to write log files to. Applicable only when
            `allow_file_logging` = True
        file_handler_type              :object of logging handler from HandlerType class. Applicable only when
            `allow_file_logging` = True
        allow_console_logging (bool)   :Turn off/on the console logging.
        allow_file_logging (bool)      :Turn off/on if logs need to go in files as well.
        backup_count (int)             :Number of files to backup before rotating the logs.
        max_file_size_bytes (int)      :Size of file in bytes before rotating the file. Applicable only to
            ROTATING_FILE_HANDLER.
        when_to_rotate (str)           :Duration after which a file can be rotated. Applicable only to
            TIME_ROTATING_FILE_HANDLER
                                        Accepts following values:
                                        'S'	Seconds
                                        'M'	Minutes
                                        'H'	Hours
                                        'D'	Days
                                        'W0'-'W6'	Weekday (0=Monday)
                                        'midnight'	Roll over at midnight
        change_log_level (dict)        :A dictionary of handlers with corresponding log-level
            ( for eg. {'requests':'warning'} )
        console_log_level (logging)    :Change the LogLevel of console log handler, default is logging.INFO
            (e.g. logging.DEBUG, logging.INFO)
        file_log_level (logging)       :Change the LogLevel of file log handler, default is logging.DEBUG
            (e.g. logging.DEBUG, logging.INFO)
        gelf_handler                   :An external handler for graylog data publishing.
        log_tags                       :Adding contextual information to a given log handler for e.g. {'app_name': 'My Perfect App'}
    """
    file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER]
    if allow_file_logging and (file_handler_type not in file_handlers):
        rwt(ValueError('Please pass an object of HandlerType class'))

    if change_log_level:
        __set_log_levels(change_log_level)

    # Check if log_tags is not defined, initialize it to empty dict
    if not log_tags:
        log_tags = {}

    class AppFilter(logging.Filter):
        def filter(self, record):
            for key, value in log_tags.items():
                setattr(record, key, value)
            return True

    log_filter = AppFilter() if log_tags else None
    logger = logging.getLogger()
    logger.propagate = False
    logger.setLevel(logging.DEBUG)

    if gelf_handler:
        if log_filter:
            gelf_handler.addFilter(log_filter)
        logger.addHandler(gelf_handler)

    # create console handler and set level to info
    if allow_console_logging:
        handler = logging.StreamHandler()
        log_level = kwargs.get("console_log_level", logging.INFO)
        handler.setLevel(log_level)
        handler.setFormatter(log_formatter)
        if log_filter:
            handler.addFilter(log_filter)
        logger.addHandler(handler)

    if allow_file_logging:
        __setup_file_logging(g_logger=logger,
                             log_directory=log_directory,
                             file_handler_type=file_handler_type,
                             backup_count=backup_count,
                             max_file_size_bytes=max_file_size_bytes,
                             when_to_rotate=when_to_rotate,
                             file_log_level=file_log_level,
                             log_filter=log_filter)
Esempio n. 5
0
def setup_logger(log_directory='.',
                 file_handler_type=HandlerType.TIME_ROTATING_FILE_HANDLER,
                 allow_console_logging=True,
                 allow_file_logging=True,
                 backup_count=100,
                 max_file_size_bytes=100000,
                 when_to_rotate='D',
                 change_log_level=None,
                 log_formatter=Formatters.TextFormatter,
                 gelf_handler=None,
                 logger_name=None,
                 **kwargs):
    """Set up the global logging settings.

    Args:
        log_directory (str)            :directory to write log files to. Applicable only when `allow_file_logging` = True
        file_handler_type              :object of logging handler from HandlerType class. Applicable only when `allow_file_logging` = True
        allow_console_logging (bool)   :Turn off/on the console logging.
        allow_file_logging (bool)      :Turn off/on if logs need to go in files as well.
        backup_count (int)             :Number of files to backup before rotating the logs.
        max_file_size_bytes (int)      :Size of file in bytes before rotating the file. Applicable only to ROTATING_FILE_HANDLER.
        when_to_rotate (str)           :Duration after which a file can be rotated. Applicable only to TIME_ROTATING_FILE_HANDLER
                                        Accepts following values:
                                        'S'	Seconds
                                        'M'	Minutes
                                        'H'	Hours
                                        'D'	Days
                                        'W0'-'W6'	Weekday (0=Monday)
                                        'midnight'	Roll over at midnight
        change_log_level (dict)        :A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} )
        console_log_level (logging)    :Change the LogLevel of console log handler, default is logging.INFO (e.g. logging.DEBUG, logging.INFO)
        gelf_handler                   :An external handler for graylog data publishing.
        logger_name (str)              :If None, then a default logger is created using `logging.getLogger()` and all handlers are registered
                                        with that logger. If a string name is provided then a logger with that name is created and similarly
                                        all handlers are registered with this logger. However if a logger already exists with that name,
                                        then it reuses that logger (does not overwrite or recreate) and adds new handlers to it.
    """
    file_handlers = [HandlerType.ROTATING_FILE_HANDLER, HandlerType.TIME_ROTATING_FILE_HANDLER]
    if file_handler_type not in file_handlers:
        rwt(ValueError('Please pass an object of HandlerType class'))

    if change_log_level:
        __set_log_levels(change_log_level)

    # Get logger object.
    if logger_name:
        logger = logging.getLogger(logger_name)
    else:
        logger = logging.getLogger()

    logger.propagate = False
    logger.setLevel(logging.DEBUG)

    if gelf_handler:
        logger.addHandler(gelf_handler)

    # create console handler and set level to info
    if allow_console_logging:
        handler = logging.StreamHandler()
        log_level = kwargs.get("console_log_level", logging.INFO)
        handler.setLevel(log_level)
        handler.setFormatter(log_formatter)
        logger.addHandler(handler)

    if allow_file_logging:
        __setup_file_logging(g_logger=logger,
                             log_directory=log_directory,
                             file_handler_type=file_handler_type,
                             backup_count=backup_count,
                             max_file_size_bytes=max_file_size_bytes,
                             when_to_rotate=when_to_rotate)