def _setup_logger(fs):
    # log DEBUG+, INFO+ and ERROR+ to separate files

    # pychecker makes us do this, it won't recognise that logging.handlers is a
    # thing
    lg = logging

    error_handler = lg.handlers.RotatingFileHandler(
        fs.layout.log_error,
        maxBytes=10 * 1024 * 1024,
        backupCount=10)
    error_handler.setLevel(logging.ERROR)

    info_handler = lg.handlers.RotatingFileHandler(
        fs.layout.log_info,
        maxBytes=10 * 1024 * 1024,
        backupCount=10)
    info_handler.setLevel(logging.INFO)
    info_handler.addFilter(phlsys_verboseerrorfilter.make_filter())

    debug_handler = phlsys_compressedlogging.CompressedRotatingFileHandler(
        fs.layout.log_debug,
        maxBytes=50 * 1024 * 1024,
        backupCount=10)
    debug_handler.setLevel(logging.DEBUG)

    logfmt = '%(asctime)s UTC: %(levelname)s: (%(processName)-11s) %(message)s'
    formatter = logging.Formatter(logfmt)
    logging.Formatter.converter = time.gmtime
    error_handler.setFormatter(formatter)
    info_handler.setFormatter(formatter)
    debug_handler.setFormatter(formatter)
    logging.getLogger().addHandler(error_handler)
    logging.getLogger().addHandler(info_handler)
    logging.getLogger().addHandler(debug_handler)
def _setup_logger(fs):
    # log DEBUG+, INFO+ and ERROR+ to separate files

    # pychecker makes us do this, it won't recognise that logging.handlers is a
    # thing
    lg = logging

    error_handler = lg.handlers.RotatingFileHandler(fs.layout.log_error,
                                                    maxBytes=10 * 1024 * 1024,
                                                    backupCount=10)
    error_handler.setLevel(logging.ERROR)

    info_handler = lg.handlers.RotatingFileHandler(fs.layout.log_info,
                                                   maxBytes=10 * 1024 * 1024,
                                                   backupCount=10)
    info_handler.setLevel(logging.INFO)
    info_handler.addFilter(phlsys_verboseerrorfilter.make_filter())

    debug_handler = phlsys_compressedlogging.CompressedRotatingFileHandler(
        fs.layout.log_debug, maxBytes=50 * 1024 * 1024, backupCount=10)
    debug_handler.setLevel(logging.DEBUG)

    logfmt = '%(asctime)s UTC: %(levelname)s: (%(processName)-11s) %(message)s'
    formatter = logging.Formatter(logfmt)
    logging.Formatter.converter = time.gmtime
    error_handler.setFormatter(formatter)
    info_handler.setFormatter(formatter)
    debug_handler.setFormatter(formatter)
    logging.getLogger().addHandler(error_handler)
    logging.getLogger().addHandler(info_handler)
    logging.getLogger().addHandler(debug_handler)
    def test_B_verbose_filter(self):
        # pychecker makes us do this, it won't recognise that logging.handlers
        # is a thing.
        lg = logging
        with phlsys_fs.chtmpdir_context():
            error_handler = lg.handlers.RotatingFileHandler(
                'errorlog',
                maxBytes=10 * 1024,
                backupCount=10)
            error_handler.setLevel(logging.ERROR)
            logging.getLogger().addHandler(error_handler)

            info_handler = lg.handlers.RotatingFileHandler(
                'infolog',
                maxBytes=10 * 1024,
                backupCount=10)
            info_handler.setLevel(logging.INFO)
            # [ B] VerboseErrorFilter can be added to log handler
            info_handler.addFilter(phlsys_verboseerrorfilter.make_filter())
            logging.getLogger().addHandler(info_handler)

            concise_message = "This is a concise error message."
            verbose_message = "VERBOSE MESSAGE: This is a verbose error "
            "message. This should not appear in error log but not in info log."
            _LOGGER.error(concise_message)
            _LOGGER.error(verbose_message)

            with open('errorlog') as f:
                # [ B] Error log contains both - concise and verbose messages
                error_log = f.read()
                self.assertTrue(concise_message in error_log)
                self.assertTrue(verbose_message in error_log)

            with open('infolog') as f:
                info_log = f.read()
                # [ B] info log contains concise message
                self.assertTrue(concise_message in info_log)
                # [ B] info log does not contains verbose message
                self.assertFalse(verbose_message in info_log)
예제 #4
0
    def test_B_verbose_filter(self):
        # pychecker makes us do this, it won't recognise that logging.handlers
        # is a thing.
        lg = logging
        with phlsys_fs.chtmpdir_context():
            error_handler = lg.handlers.RotatingFileHandler('errorlog',
                                                            maxBytes=10 * 1024,
                                                            backupCount=10)
            error_handler.setLevel(logging.ERROR)
            logging.getLogger().addHandler(error_handler)

            info_handler = lg.handlers.RotatingFileHandler('infolog',
                                                           maxBytes=10 * 1024,
                                                           backupCount=10)
            info_handler.setLevel(logging.INFO)
            # [ B] VerboseErrorFilter can be added to log handler
            info_handler.addFilter(phlsys_verboseerrorfilter.make_filter())
            logging.getLogger().addHandler(info_handler)

            concise_message = "This is a concise error message."
            verbose_message = "VERBOSE MESSAGE: This is a verbose error "
            "message. This should not appear in error log but not in info log."
            _LOGGER.error(concise_message)
            _LOGGER.error(verbose_message)

            with open('errorlog') as f:
                # [ B] Error log contains both - concise and verbose messages
                error_log = f.read()
                self.assertTrue(concise_message in error_log)
                self.assertTrue(verbose_message in error_log)

            with open('infolog') as f:
                info_log = f.read()
                # [ B] info log contains concise message
                self.assertTrue(concise_message in info_log)
                # [ B] info log does not contains verbose message
                self.assertFalse(verbose_message in info_log)