def setup_logger(name='crypto-exporter', level='INFO', gelf_host=None, gelf_port=None, **kwargs): """ sets up the logger """ logging.basicConfig(handlers=[logging.NullHandler()]) formatter = logging.Formatter( fmt= '%(asctime)s.%(msecs)03d %(levelname)s [%(module)s.%(funcName)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', ) logger = logging.getLogger(name) logger.setLevel(level) handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) if gelf_host and gelf_port: handler = pygelf.GelfUdpHandler(host=gelf_host, port=gelf_port, debug=True, include_extra_fields=True, **kwargs) logger.addHandler(handler) return logger
def _setup_logging_gelf(): parts = os.environ['KATSDP_LOG_GELF_ADDRESS'].rsplit(':', 1) host = parts[0] if len(parts) == 2: port = int(parts[1]) else: port = 12201 # Default GELF port localname = os.environ.get('KATSDP_LOG_GELF_LOCALNAME') extras = os.environ.get('KATSDP_LOG_GELF_EXTRA', '{}') extras = json.loads(extras) if not isinstance(extras, dict): raise ValueError('KATSDP_LOG_GELF_EXTRA must be a JSON dict') # pygelf wants the additional fields to have _ already prepended extras = {'_' + key: value for (key, value) in extras.items()} container_id = docker_container_id() if container_id is not None: extras['_docker.id'] = container_id handler = pygelf.GelfUdpHandler(host, port, debug=True, include_extra_fields=True, compress=True, static_fields=extras) handler.addFilter(_TimestampFilter()) if localname: handler.domain = localname logging.root.addHandler(handler)
def configure_logging(): """ Configures the logging """ gelf_enabled = False if os.environ.get('GELF_HOST'): GELF = pygelf.GelfUdpHandler( host=os.environ.get('GELF_HOST'), port=int(os.environ.get('GELF_PORT', 12201)), debug=True, include_extra_fields=True, _ix_id=FILENAME ) LOG.addHandler(GELF) gelf_enabled = True LOG.info('Initialized logging with GELF enabled: {}'.format(gelf_enabled))
def configure_logging(): """ Configures the logging """ logging.basicConfig( stream=sys.stdout, level=settings['loglevel'], format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', ) if settings.get('gelf_host'): gelf = pygelf.GelfUdpHandler( name=__package__, level=settings['loglevel'], host=settings['gelf_host'], port=int(settings['gelf_port']), include_extra_fields=True, debug=True, _ix_id=os.path.splitext(sys.modules['__main__'].__file__)[0], ) log.addHandler(gelf) log.info('Initialized logging with GELF enabled to {}:{}'.format)
import os import pygelf import graypy logging.basicConfig(level=logging.INFO) logger = logging.getLogger() # remove default logging to stdout logger.removeHandler(logger.handlers[0]) # dummy data fields = {"_some": {"structured": "data"}} # pygelf pygelf_handler = pygelf.GelfUdpHandler(host=os.getenv('KBC_LOGGER_ADDR'), port=int(os.getenv('KBC_LOGGER_PORT')), chunkSize=1350, **fields) logger.addHandler(pygelf_handler) logging.info('A sample info message (pygelf)') logging.warning('A sample warn message (pygelf)') logging.critical('A sample emergency message (pygelf)') logger.removeHandler(pygelf_handler) # graypy graypy_handler = graypy.GELFHandler(host=os.getenv('KBC_LOGGER_ADDR'), port=int(os.getenv('KBC_LOGGER_PORT')), extra_fields=fields) logger.addHandler(graypy_handler) logging.info('A sample info message (graypy)') logging.warning('A sample warn message (graypy)')