def init_logger(log_dir, console_debug=False):
        # get root logger
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

        # create file handler which logs even debug messages
        log_file = ProjectUtils.get_default_log_file(PROJECT_NAME)
        fh = TimedRotatingFileHandler(os.path.join(log_dir, log_file),
                                      when='midnight')
        fh.suffix = '%Y_%m_%d.log'
        fh.setLevel(logging.DEBUG)

        # create console handler with a higher log level
        ch = logging.StreamHandler(stream=sys.stdout)
        ch.setLevel(logging.INFO)
        if console_debug:
            ch.setLevel(logging.DEBUG)

        formatter = logging.Formatter(
            '%(asctime)s - %(levelname)s - %(name)s - %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # add the handlers to the logger
        logger.addHandler(fh)
        logger.addHandler(ch)
Example #2
0
    def init_logger(execution_mode: ExecutionMode,
                    console_debug=False,
                    postfix: str = None,
                    repos=None,
                    verbose=False):
        # get root logger
        logger = logging.getLogger()
        level = logging.DEBUG
        logger.setLevel(level)

        if execution_mode == ExecutionMode.PRODUCTION:
            log_file = ProjectUtils.get_default_log_file(PROJECT_NAME,
                                                         postfix=postfix)
        elif execution_mode == ExecutionMode.TEST:
            log_file = ProjectUtils.get_default_test_log_file(PROJECT_NAME,
                                                              postfix=postfix)
        else:
            raise ValueError(f"Unknown execution mode: {execution_mode}")

        # create file handler which logs even debug messages
        fh = TimedRotatingFileHandler(log_file, when="midnight")
        fh.suffix = "%Y_%m_%d.log"
        fh.setLevel(level)

        # create console handler with a higher log level
        ch = logging.StreamHandler(stream=sys.stdout)
        ch.setLevel(logging.INFO)
        if console_debug:
            ch.setLevel(level)

        formatter = logging.Formatter(
            "%(asctime)s - %(levelname)s - %(name)s - %(message)s")
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
        # add the handlers to the logger
        logger.addHandler(fh)
        logger.addHandler(ch)

        # https://gitpython.readthedocs.io/en/stable/tutorial.html#git-command-debugging-and-customization
        # THIS WON'T WORK BECAUSE GITPYTHON MODULE IS LOADED BEFORE THIS CALL
        # os.environ["GIT_PYTHON_TRACE"] = "1"
        # https://github.com/gitpython-developers/GitPython/issues/222#issuecomment-68597780
        LOG.warning(
            "Cannot enable GIT_PYTHON_TRACE because repos list is empty!")
        if repos:
            for repo in repos:
                val = "full" if verbose else "1"
                type(repo.git).GIT_PYTHON_TRACE = val
        return log_file