Browse Source

Support logging to GELF via HTTP

master
Johann Schmitz 1 year ago
parent
commit
3cee69e61e
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
3 changed files with 49 additions and 1 deletions
  1. +34
    -0
      fuglugelf/httphandler.py
  2. +14
    -1
      fuglugelf/logger.py
  3. +1
    -0
      requirements.txt

+ 34
- 0
fuglugelf/httphandler.py View File

@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
from logging import Handler

import requests
from graypy.handler import BaseGELFHandler, WAN_CHUNK


class GELFHttpHandler(BaseGELFHandler, Handler):
def __init__(self, host, port=443,
debugging_fields=True, extra_fields=True, fqdn=False,
localname=None, facility=None, level_names=False, compress=True):

super(GELFHttpHandler, self).__init__(host, port, WAN_CHUNK, debugging_fields, extra_fields,
fqdn, localname, facility, level_names, compress)
Handler.__init__(self)
self.host = host
self.port = port

def emit(self, record):
try:
msg = self.makePickle(record)

url = "https://%s:%s/gelf" % (self.host, self.port)
headers = {}
if self.compress:
headers['content-encoding'] = 'deflate'
req = requests.post(url, data=msg, timeout=5.0, headers=headers)
req.raise_for_status()
except:
self.handleError(record)


+ 14
- 1
fuglugelf/logger.py View File

@@ -7,6 +7,8 @@ from email.utils import getaddresses
import graypy
from fuglu.shared import AppenderPlugin, actioncode_to_string, Suspect, yesno

from fuglugelf.httphandler import GELFHttpHandler


class GELFLogger(AppenderPlugin):
def __init__(self, *args, **kwargs):
@@ -28,6 +30,10 @@ class GELFLogger(AppenderPlugin):
'default': '12201',
'description': 'Port on the target server',
},
'gelf-protocol': {
'default': 'udp',
'description': '''Procotol to use ('udp' for UDP, 'tcp' for TCP or 'http' for GELF via HTTP)'''
}
}
self._log_level = None
@@ -49,7 +55,14 @@ class GELFLogger(AppenderPlugin):
host = self.config.get(self.section, 'gelf-host')
port = self.config.getint(self.section, 'gelf-port')

handler = graypy.GELFHandler(host, port)
protocol = self.config.get(self.section, 'gelf-protocol')
if protocol == "tcp":
handler = graypy.GELFTcpHandler(host, port)
elif protocol == "http":
handler = GELFHttpHandler(host, port)
else:
handler = graypy.GELFHandler(host, port)
self._gelf_logger.addHandler(handler)
self.logger.log(self.log_level, "Sending messages to GELF server at %s:%s on %s", host, port, self.log_level)



+ 1
- 0
requirements.txt View File

@@ -1,3 +1,4 @@
# Project dependencies
fuglu
graypy
requests

Loading…
Cancel
Save