def add_gray_handler(loggername): logger = logging.getLogger(loggername) logger.setLevel(logging.DEBUG) handler = graypy.GELFHTTPHandler('graylog-dau.esrf.fr', 12206) logger.addHandler(handler) logger.debug('Message:{}'.format('Gray Logger is running for esrf')) return logger
def init_app(self, app, config=None): """ Configure Graylog logger from a Flask application Available configuration options: GRAYLOG_HOST - the host to send messages to [default: 'localhost'] GRAYLOG_PORT - the port to send messages to [default: 12201] GRAYLOG_FACILITY - the facility to report with [default: 'flask'] GRAYLOG_EXTRA_FIELDS - whether or not to include `extra` fields from the message [default: True] GRAYLOG_ADD_DEBUG_FIELDS - whether extra python debug fields should be added to each message [default: True] GRAYLOG_CONFIGURE_MIDDLEWARE - whether to setup middleware to log each response [default: True] :param app: Flask application to configure this logger for :type app: flask.Flask :param config: An override config to use instead of `app.config` :type config: `dict` or `None` """ # Use the config they provided if config is not None: self.config = config # Use the apps config if `config` was not provided elif app is not None: self.config = app.config self.app = app # Setup default config settings self.config.setdefault('GRAYLOG_HOST', 'localhost') self.config.setdefault('GRAYLOG_PORT', 12201) self.config.setdefault('GRAYLOG_FACILITY', 'flask') self.config.setdefault('GRAYLOG_EXTRA_FIELDS', True) self.config.setdefault('GRAYLOG_ADD_DEBUG_FIELDS', True) self.config.setdefault('GRAYLOG_CONFIGURE_MIDDLEWARE', True) # Configure the logging handler and attach to this logger self.handler = graypy.GELFHTTPHandler( host=self.config['GRAYLOG_HOST'], port=self.config['GRAYLOG_PORT'], facility=self.config['GRAYLOG_FACILITY'], extra_fields=self.config['GRAYLOG_EXTRA_FIELDS'], debugging_fields=self.config['GRAYLOG_ADD_DEBUG_FIELDS'], ) self.addHandler(self.handler) # Setup middleware if they asked for it if self.config['GRAYLOG_CONFIGURE_MIDDLEWARE']: self.setup_middleware()
def setup_logger(name: str = None, logger: logging.Logger = None, disable_urllib_warnings=True): if disable_urllib_warnings: urllib3.disable_warnings() if name is None and logger is None: logger = logging.getLogger("antiintuit") elif logger is None: logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setLevel(logging.INFO) logger.addHandler(stdout_handler) if isinstance(Config.GRAYLOG_HOST, str): host, port = get_host_and_port(Config.GRAYLOG_HOST, 12201) if is_open_connection(host, port): graylog_handler = graypy.GELFHTTPHandler(host, port) graylog_handler.setLevel(logging.DEBUG) logger.addHandler(graylog_handler) else: logger.warning("No connection to GrayLog (%s:%i)", host, port) stdout_handler.setLevel(logging.DEBUG) return logger
import logging import graypy logger = logging.getLogger('voos_logger') logger.setLevel(logging.DEBUG) handler = graypy.GELFHTTPHandler(host='127.0.0.1', port=12201) logger.addHandler(handler)