Package: django-graylog
django-graylogA Django middleware for logging requests toGraylog.Installationpip install django-graylogUsing an HTTP/HTTPS requires therequestsmodule, which can be included withpip install django-graylog[http].Parsing user agents requires theua_parsermodule, which can be included withpip install django-graylog[ua].All optional libraries may be included usingpip install django-graylog[all].ConfigurationSimply adddjango_graylog.GraylogMiddlewareto yourMIDDLEWAREsetting, afterdjango.middleware.common.CommonMiddleware. By default, the middleware does nothing
unlessGRAYLOG_ENDPOINTis set (see below).SettingsGRAYLOG_ENDPOINT- An HTTP, HTTPS, UDP, or TCP endpoint to send GELF logs to. For
example:http://yourserver:12201/gelfudp://yourserver:12201tcp://yourserver:12201GRAYLOG_NODE- The middleware sends a_nodefield that defaults tosocket.gethostname. Set this to override it.GRAYLOG_LEVEL- The defaultlevelto send for log entries. Defaults to 6 (INFO).GRAYLOG_TIMEOUT- Timeout (in seconds) for sending log entries to Graylog. Defaults
to0.25seconds.GRAYLOG_FIELDS- Extra fields to send for each request (not prefixed with
underscores). Defaults to{}.GRAYLOG_HEADERS-Trueto include all HTTP request headers (except sensitive
headers likeAuthorizationandCookie- see below), otherwise a list of headers to
be included. Defaults to[].GRAYLOG_EXCLUDE_HEADERS- A list of headers to exclude whenGRAYLOG_HEADERSisTrue. Defaults to["authorization", "cookie", "proxy-authorization"].GRAYLOG_USER_AGENT-Trueto parse out User-Agent header into separate fields
usingua_parser(default isFalse).GRAYLOG_REFERER-Trueto parse out the referer domain (default isFalse) into a
separate field.GRAYLOG_USERNAME-Trueto includerequest.user.get_username()(default isFalse).GRAYLOG_TIMING-Trueto include request timing information (the default),Falseto disable.GRAYLOG_FILTERS- A dictionary of filters to exclude records from being logged. Each
key is a field name, and each value is a list of regegular expressions to exclude. For
example:{"host": [r"media.example.com"]}- Skips logging of requests to themedia.example.comdomain.{"path": [r"^/_"]}- Skips logging of requests to paths starting with_.{"ip": "192\.168\."}- Skips logging of requests from192.168.*addresses.
Using a string instead of a list works for a single regular expression.GRAYLOG_EXCEPTION_MESSAGES- Whether to include exception messages in data sent to
Graylog. Setting toFalsewill strip the last line from stack traces (in case the
line includes a literal message), and not send the_exception_messagefield.
Defaults toTrue.GRAYLOG_FACILITY- Sent as_facility, and used as the default logger name for
logging messages sent viarequest.graylog.GRAYLOG_TIMESTAMP- Whether to include atimestampfield in data sent to Graylog.
Setting toFalsemeans Graylog will infer the current time when it receives log
entries. Defaults toTrue.GRAYLOG_IP- Whether to include the IP address of requests. Defaults toTrue.Advanced UsageIn addition to the standard logging middleware,django_grayloginstalls an object on
your request objects (request.graylog) that has standard logging methods for recording
per-request logs. You may also set custom keys on this object
(request.graylog["key"] = "value") that will be included in log entries. For example:defhomepage(request):request.graylog["user"]=request.user.user_namerequest.graylog.info("Rendered homepage for{user}",user=request.user.user_name)If you want standard Python logging calls during a request to be logged into Graylog as
well,django_graylogcomes with aGraylogRequestHandlerlogging handler that tracks
the current request and associates logging to it.