Example #1
0
def _create_graylog_handler(site_config, config_prefix):
    try:
        import pygelf
    except ImportError:
        return None

    address = site_config.get(f'{config_prefix}/address')
    host, *port = address.split(':', maxsplit=1)
    if not port:
        raise ConfigError('graylog handler: no port specified')

    port = port[0]

    # Check if the remote server is up and accepts connections; if not we will
    # skip the handler
    try:
        with socket.create_connection((host, port), timeout=1):
            pass
    except OSError as e:
        getlogger().warning(
            f"could not connect to Graylog server at '{address}': {e}")
        return None

    extras = site_config.get(f'{config_prefix}/extras')
    return pygelf.GelfHttpHandler(host=host,
                                  port=port,
                                  static_fields=extras,
                                  include_extra_fields=True,
                                  json_default=jsonext.encode)
Example #2
0
def _create_graylog_handler(handler_config):
    try:
        import pygelf
    except ImportError:
        return None

    host = handler_config.get('host', None)
    port = handler_config.get('port', None)
    extras = handler_config.get('extras', None)
    if host is None:
        raise ConfigError('graylog handler: no host specified')

    if port is None:
        raise ConfigError('graylog handler: no port specified')

    if extras is not None and not isinstance(extras, collections.abc.Mapping):
        raise ConfigError('graylog handler: extras must be a mapping type')

    return pygelf.GelfHttpHandler(host=host, port=port, debug=True,
                                  static_fields=extras,
                                  include_extra_fields=True)