Exemplo n.º 1
0
def create_file_handler(log_file=LOG_FILE, formatter=FORMATTER):
    """
    Creates *the* (singular) file handler for logging to text file.

    File handler needs to be global and a singular instance,
    to avoid spamming FDs for each create_logger() call.
    :param log_file:
    :param formatter:
    :return:
    """
    global LOG_FILE_HANDLER

    # Get config
    config = load_config()

    # Only create one instance of the file handler
    if config['log_to_file'] is True and LOG_FILE_HANDLER is None:
        logfile_path = os.path.join(config["logging_dir"], log_file)

        # Make sure logs dir exists, if not create it.
        if not os.path.isdir(config["logging_dir"]):
            os.makedirs(config["logging_dir"])

        # Make sure logfile exists, if not create it.
        if not os.path.isfile(logfile_path):
            open(logfile_path, 'a').close()

        LOG_FILE_HANDLER = logging.FileHandler(logfile_path, encoding="UTF-8")
        LOG_FILE_HANDLER.setLevel(logging.DEBUG)
        LOG_FILE_HANDLER.setFormatter(formatter)
Exemplo n.º 2
0
def create_logger_file(facility, formatter=FORMATTER):
    """
    Create and return a a logging instance that logs to file.
    :param facility:
    :param formatter:
    :return:
    """
    global LOG_FILE_HANDLER

    # Get config
    config = load_config()

    log_file_instance = logging.getLogger(facility)

    log_file_instance.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    if not os.path.exists(config["logging_dir"]):
        os.makedirs(config["logging_dir"])

    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)
    # patch the default logging formatter to use unicode format string
    logging._defaultFormatter = logging.Formatter(u"%(message)s")
    ch.setFormatter(formatter)

    # add the handlers to the logger
    log_file_instance.addHandler(LOG_FILE_HANDLER)
    log_file_instance.addHandler(ch)

    return log_file_instance
Exemplo n.º 3
0
def create_logger(facility):
    """
    Creates a logger function based on the logging library
    :param facility:     Name of what's calling logger.
    :return:
    """
    # Get config
    config = load_config()

    create_file_handler()
    # create logger
    if config["log_to_file"] is False:
        logger_instance = create_logger_socket(facility)
    else:
        logger_instance = create_logger_file(facility)

    # Attach a handle to the log levels dict.
    setattr(logger_instance, "my_log_levels", LOG_LEVELS)

    return logger_instance
Exemplo n.º 4
0
            return r


def log_added_route(name: str, include_obj=True):
    rule = get_flask_rule_by_name(name)
    log.info(
        "Added TheHive listener Flask App route '{rule}': "
        "view_func: {endpoint}, methods: {methods}".format(**rule.__dict__))

    if include_obj is True:
        log.debug2(rule.__dict__)


if __name__ == "__main__":
    # Get config.
    config = config_handler.load_config()
    log.info("Loaded configuration: '{}'.".format(
        config_handler.CONFIG_FILE if config_handler.has_custom_config(
        ) else 'default'))
    log.debug("CONFIG (overrides: {overrides}):\n{js}".format(
        overrides=config_handler.CONFIG_OVERRIDES,
        js=json.dumps(config_handler.CONFIG, indent=4)))

    # Initialize database.
    init_db()
    log.info("Initialized database.")

    # Set up TheOracle Git.
    git_handler.clone_if_not_exist(url=config["theoracle_repo"],
                                   path=config["theoracle_local_path"])
Exemplo n.º 5
0
# -*- coding: utf-8 -*-

import logging
import os
from logging.handlers import SocketHandler
from handlers.config_handler import load_config

INITIAL_CONFIG = load_config()

LOG_FILE_HANDLER = None

# create formatter and add it to the handlers
FORMATTER = logging.Formatter(
    u'%(asctime)s - %(name)s - %(levelname)s - %(message)s')

DEFAULT_LOG_LEVELS = [0, 10, 20, 30, 40, 50]

# Logging levels (practically) static dict.
LOG_LEVELS = {
    'UNSET': 0,
    'SPAM': 1,
    'DEBUG9': 2,
    'DEBUG8': 3,
    'DB_DEBUG': 4,
    'DEBUG6': 5,
    'DEBUG5': 6,
    'DEBUG4': 7,
    'DEBUG3': 8,
    'DEBUG2': 9,
    'DEBUG': 10,
    'INFO': 20,