def test_init_default_name(self):
        logger = umsg.init()
        s = inspect.stack()[0]
        m = inspect.getmodule(s[0])

        assert_that(logger.name).is_equal_to(m.__name__)
        assert_that(umsg.get_attr('logger').name).is_equal_to(m.__name__)
示例#2
0
    def __init__(self, *args, prefix=None, logger=None, **kwargs):
        # try to play nice with potentially uncooperative classes
        try:
            super().__init__(*args, **kwargs)
        except TypeError as e:
            try:
                super().__init__()
            except TypeError as e:
                pass

        if logger:
            umsg.set_attr('logger', logger)
        else:
            umsg.init()

        self._umsg_logger = umsg.get_attr('logger')
        self._umsg_log_prefix = prefix or umsg.get_attr('msg_prefix')
def test_cooperative_mixin_first(caplog):
    prefix = 'test-04'
    msg = 'test-04'

    class tclass(LoggingMixin, Cooperative):
        def __init__(self):
            super().__init__(a=5, prefix=prefix, b=4)
            self.log(msg)

    test = tclass()
    expected = '[{}] {}'.format(prefix, msg)

    assert_that(test._umsg_logger).is_identical_to(umsg.get_attr('logger'))
    assert_that(caplog.record_tuples[0][2]).is_equal_to(expected)
def test_noncooperative_manual_init(caplog):
    prefix = 'test-03'
    msg = 'test-03'

    class tclass(NonCooperative, LoggingMixin):
        def __init__(self):
            super().__init__(a=5, b=4)
            LoggingMixin.__init__(self, prefix=prefix)
            self.log(msg)

    test = tclass()
    expected = '[{}] {}'.format(prefix, msg)

    assert_that(test._umsg_logger).is_identical_to(umsg.get_attr('logger'))
    assert_that(caplog.record_tuples[0][2]).is_equal_to(expected)
示例#5
0
    def test_init_handlers(self):
        # logger is already setup for this module
        logger = umsg.get_attr('logger')

        # add instance
        umsg.add_handler(logging.StreamHandler())

        # add class
        umsg.add_handler(logging.handlers.SocketHandler, **{
            'host': '127.0.0.1',
            'port': 99
        })

        assert_that(logger.handlers[0]).is_instance_of(logging.NullHandler)
        assert_that(logger.handlers[1]).is_instance_of(logging.StreamHandler)
        assert_that(logger.handlers[2]).is_instance_of(
            logging.handlers.SocketHandler)
示例#6
0
    def run(self):
        """Execute the configured process."""
        self.results = []
        self.files = []
        self.log('Process initiated')

        # creates a logging thread for queued messages
        log_queue = multiprocessing.Manager().Queue()
        logthread = threading.Thread(target=self.logging_thread,
                                     args=(umsg.get_attr('logger'), log_queue))
        logthread.start()

        self.log('Gathering source data')
        workflow = WorkflowJobPool(config=self.config,
                                   handler=self.worker,
                                   logqueue=log_queue)
        workflow.run()
        results, errors = workflow.get_results()

        # closeout our logging thread
        log_queue.put_nowait(None)
        logthread.join()

        if errors:
            self.log('Errors encountered')
            files = None
        else:
            self.log('Generating process output')
            errors = self.generate(results)

        if 'notifications' in self.config:
            self.notify(self.config['notifications'],
                        files=self.files,
                        errors=errors)

        self.log('Process complete')
示例#7
0
##  python __main__ execution
## ----------------------------------------------------
if __name__ == '__main__':
    try:
        required('TR_ACTION_TYPES', ACTION_TYPES)
        required('TR_AUTH', AUTH)
        required('TR_GROUPS', GROUPS)
        required('TR_TAGS', EMAIL_TAGS)

        os.chdir(WORKING_DIR)
        socket.setdefaulttimeout(30)

        vmt_handler = init_log_handler(LOG_FORMATTER, LOG_MODE)
        umsg.init(mode=LOG_MODE, msg_prefix=MSG_PREFIX)
        umsg.add_handler(vmt_handler)
        LOGGER = umsg.get_attr('logger')
        LOGGER.setLevel(LOG_MODE)

        if DISABLE_SSL_WARNINGS:
            _msg('Disabling SSL warnings', level='debug')
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        if HTTP_DEBUG:
            _msg('HTTP Debuging output enabled', level='debug')
            from http.client import HTTPConnection
            HTTPConnection.debuglevel = 1
            requests_log = logging.getLogger('requests.packages.urllib3')
            requests_log.setLevel(logging.DEBUG)
            requests_log.propagate = True

        main()
    def test_init_default_level(self):
        logger = umsg.get_attr('logger')

        assert_that(umsg.get_attr('level')).is_equal_to(logging.INFO)
    def test_init_default_handler(self):
        # logger is already setup for this module
        logger = umsg.get_attr('logger')

        assert_that(logger.handlers[0]).is_instance_of(logging.NullHandler)
    def test_setup(self):
        logger = umsg.get_attr('logger')

        assert_that(logger.name).is_equal_to('TestMessaging')
        assert_that(logger.getEffectiveLevel()).is_equal_to(logging.DEBUG)