Esempio n. 1
0
def init_logging(config):
    """
    Initialize logging based on configuration

    Either the entire config, or the logging subsection must be passed in. If no
    logging `path` parameter is found a :py:class:`~logging.StreamHandler` will
    be initialized instead. If `path` is found, a :py:class:`~logging.handlers.RotatingFileHandler`
    will be initialized, with a default rotation of 10mb, and `backupCount` of 1.

    Args:
        config (dict): Configuration.
    """
    global LOG_MODE, LOG_MAXSIZE, LOG_PATH, LOG_FILENAME, LOG_FILEMODE, LOG_ENCODING

    hdlr = None
    logging = config.get('logging', config)

    LOG_MODE = logging.get('mode', 'ERROR')
    LOG_MAXSIZE = logging.get('maxsize', '10M')
    LOG_PATH = logging.get('path', None)
    LOG_FILEMODE = logging.get('filemode', 'a+').lower()
    LOG_ENCODING = logging.get('encoding', None)

    if LOG_PATH:
        hdlr = RotatingFileHandler(LOG_PATH,
                                   mode=LOG_FILEMODE,
                                   maxBytes=mem_cast(LOG_MAXSIZE, 'B'),
                                   backupCount=1,
                                   encoding=LOG_ENCODING)
    if not hdlr:
        hdlr = StreamHandler()

    umsg.init(mode=LOG_MODE)
    umsg.add_handler(hdlr)
Esempio n. 2
0
 def setup_class(cls):
     umsg.init(logger_name=LNAME,
               level=logging.DEBUG,
               propagate=True
              )
     cls.parent = Parent()
     cls.child = Child()
     cls.grandchild = GrandChild()
    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__)
Esempio n. 4
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 main(config_file):
    args = read_config_file(config_file)

    requests.packages.urllib3.disable_warnings(
        requests.packages.urllib3.exceptions.InsecureRequestWarning)
    umsg.init(level=args.get('LOG_LEVEL', 'info'))
    log_file = os.path.join(args.get('LOG_DIR', ''), args.get('LOG_FILE', ''))

    if log_file:
        handler = logging.handlers.RotatingFileHandler(log_file,
                                                       mode='a',
                                                       maxBytes=10 * 1024 *
                                                       1024,
                                                       backupCount=1,
                                                       encoding=None,
                                                       delay=0)
        umsg.add_handler(handler)

    else:
        umsg.add_handler(logging.StreamHandler())

    umsg.log('Starting script')
    csv_data = get_csv_data(filename=args['INPUT_CSV_NAME'],
                            csv_location=args['CSV_LOCATION'],
                            entity_headers=args.get('ENTITY_FIELD_MAP'),
                            match_ip=args.get('MATCH_IP', False))
    apps = parse_csv_into_apps(csv_data, args.get('APP_PREFIX', ''))

    if args.get('IGNORE_TURBO_VERSION'):
        spec = vc.VersionSpec(versions=[], required=False)
        vmt_conn = vc.Connection(os.environ['TURBO_ADDRESS'],
                                 os.environ['TURBO_USERNAME'],
                                 os.environ['TURBO_PASSWORD'],
                                 req_versions=spec)

    else:
        vmt_conn = vc.Connection(os.environ['TURBO_ADDRESS'],
                                 os.environ['TURBO_USERNAME'],
                                 os.environ['TURBO_PASSWORD'])
    turbo_vms = get_turbo_vms(vmt_conn, start=0, end=500, step=500)
    apps = match_apps_to_turbo_vms(apps, turbo_vms,
                                   args.get('MATCH_IP', False))
    make_apps_thru_atm(vmt_conn, apps)

    umsg.log('Finished script')
def main(config_file):
    args = read_config_file(config_file)
    host_name = '0.0.0.0'
    port_number = 8081
    umsg.init(level=args.get('LOG_LEVEL', 'INFO'))
    log_file = os.path.join(args.get('LOG_DIR'), args.get('LOG_FILE'))

    if log_file:
        handler = logging.handlers.RotatingFileHandler(log_file,
                                                       mode='a',
                                                       maxBytes=10 * 1024 *
                                                       1024,
                                                       backupCount=1,
                                                       encoding=None,
                                                       delay=0)
        umsg.add_handler(handler)

    else:
        umsg.add_handler(logging.StreamHandler())

    topology_handler = MakeHandlerClassFromArgs(args['INPUT_CSV_NAME'],
                                                args['CSV_LOCATION'],
                                                args.get('ENTITY_FIELD_MAP'),
                                                args.get('METRIC_FIELD_MAP'),
                                                args.get('APP_PREFIX'))

    httpd = HTTPServer((host_name, port_number), topology_handler)

    umsg.log(f'Server Starts - {host_name}:{port_number}')
    try:
        httpd.serve_forever()

    except KeyboardInterrupt:
        pass

    httpd.server_close()
    umsg.log(f'Server Stops - {host_name}:{port_number}')
def setup_module():
    umsg.init(logger_name=LNAME, level=logging.DEBUG, propagate=True)
Esempio n. 8
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
 def setup_class(cls):
     umsg.set_attr('logger_name', 'TestMessaging')
     umsg.set_attr('level', logging.DEBUG)
     umsg.set_attr('propagate', True)
     logger = umsg.init()
Esempio n. 10
0
    def test_child_profile(self):
        logger = umsg.init(logger_name=LNAME)

        assert_that(logger).is_identical_to(self.parent._umsg_logger)
        assert_that(logger).is_identical_to(self.child._umsg_logger)
    def test_mixin_profile(self):
        logger = umsg.init(logger_name=LNAME)

        # as long as we're getting back the same instance, everything else is fine
        assert_that(logger).is_identical_to(self.testclass._umsg_logger)
 def setup_class(cls):
     umsg.init(logger_name=LNAME, level=logging.DEBUG, propagate=True)
     cls.testclass = Base()