All Projects → keeprocking → Pygelf

keeprocking / Pygelf

Licence: mit
Python logging handlers with GELF (Graylog Extended Log Format) support

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects

Projects that are alternatives of or similar to Pygelf

Graylog Ansible Role
Ansible role which installs and configures Graylog
Stars: ✭ 173 (+208.93%)
Mutual labels:  logging, graylog
Documentation
Stars: ✭ 133 (+137.5%)
Mutual labels:  logging, graylog
Quicklogger
Library for logging on files, console, memory, email, rest, eventlog, syslog, slack, telegram, redis, logstash, elasticsearch, influxdb, graylog, Sentry, Twilio, ide debug messages and throw events for Delphi/Firemonkey/freepascal/.NET (Windows/Linux/OSX/IOS/Android).
Stars: ✭ 137 (+144.64%)
Mutual labels:  logging, graylog
Collector Sidecar
Manage log collectors through Graylog
Stars: ✭ 209 (+273.21%)
Mutual labels:  logging, graylog
Graypy
Python logging handler for Graylog that sends messages in GELF (Graylog Extended Log Format).
Stars: ✭ 211 (+276.79%)
Mutual labels:  logging, graylog
Flowgger
A fast data collector in Rust
Stars: ✭ 606 (+982.14%)
Mutual labels:  logging, graylog
logspout-gelf
Logspout with GELF adapter
Stars: ✭ 16 (-71.43%)
Mutual labels:  graylog, logging
Graylog2 Server
Free and open source log management
Stars: ✭ 5,952 (+10528.57%)
Mutual labels:  logging, graylog
Fliplog
fluent logging with verbose insight, colors, tables, emoji, filtering, spinners, progress bars, timestamps, capturing, stack traces, tracking, presets, & more...
Stars: ✭ 41 (-26.79%)
Mutual labels:  logging
Plog
Portable, simple and extensible C++ logging library
Stars: ✭ 1,061 (+1794.64%)
Mutual labels:  logging
Tron
R package for easy logging
Stars: ✭ 38 (-32.14%)
Mutual labels:  logging
Log4j2 Ttl Thread Context Map
🌳 Log4j2 TTL ThreadContextMap, Log4j2 extension integrated TransmittableThreadLocal to MDC
Stars: ✭ 41 (-26.79%)
Mutual labels:  logging
Lager
A logging framework for Erlang/OTP
Stars: ✭ 1,060 (+1792.86%)
Mutual labels:  logging
Office365 Audit Log Collector
Collector script for retrieving audit logs from the Office 365 API with file or network/graylog output.
Stars: ✭ 39 (-30.36%)
Mutual labels:  graylog
Aspnetcorenlog
ASP.NET Core NLog MS SQL Server PostgreSQL MySQL Elasticsearch
Stars: ✭ 54 (-3.57%)
Mutual labels:  logging
Reqray
Log call tree summaries after each request for rust programs instrumented with `tracing`.
Stars: ✭ 37 (-33.93%)
Mutual labels:  logging
Log4jwebtracker
Java web tool to setup at runtime the log level of Log4j appenders in an application, and read the log at runtime.
Stars: ✭ 35 (-37.5%)
Mutual labels:  logging
Autologging
一个非常强大的监控日志输出框架,支持 SpringAOP 和动态字节码注入两种方式输出方法执行的监控日志,而且包含链路追踪功能,只要一个注解即可开启所有功能
Stars: ✭ 56 (+0%)
Mutual labels:  logging
Slog
Structured, contextual, extensible, composable logging for Rust
Stars: ✭ 1,068 (+1807.14%)
Mutual labels:  logging
Log4cplus
log4cplus is a simple to use C++ logging API providing thread-safe, flexible, and arbitrarily granular control over log management and configuration. It is modelled after the Java log4j API.
Stars: ✭ 1,054 (+1782.14%)
Mutual labels:  logging

pygelf

|travis| |coveralls| |pypi| |downloads|

.. |travis| image:: https://travis-ci.org/keeprocking/pygelf.svg?branch=master :target: https://travis-ci.org/keeprocking/pygelf .. |pypi| image:: https://badge.fury.io/py/pygelf.svg :target: https://pypi.python.org/pypi/pygelf .. |coveralls| image:: https://coveralls.io/repos/github/keeprocking/pygelf/badge.svg?branch=master :target: https://coveralls.io/github/keeprocking/pygelf?branch=master .. |downloads| image:: https://img.shields.io/pypi/dm/pygelf.svg :target: https://pypi.python.org/pypi/pygelf

Python logging handlers with GELF (Graylog Extended Log Format) support.

Currently TCP, UDP, TLS (encrypted TCP) and HTTP logging handlers are supported.

Get pygelf

.. code:: python

pip install pygelf

Usage

.. code:: python

from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler
import logging


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger.addHandler(GelfTcpHandler(host='127.0.0.1', port=9401))
logger.addHandler(GelfUdpHandler(host='127.0.0.1', port=9402))
logger.addHandler(GelfTlsHandler(host='127.0.0.1', port=9403))
logger.addHandler(GelfHttpHandler(host='127.0.0.1', port=9404))

logger.info('hello gelf')

Message structure

According to the GELF spec, each message has the following mandatory fields:

  • version: '1.1', can be overridden when creating a logger
  • short_message: the log message itself
  • timestamp: current timestamp
  • level: syslog-compliant_ log level number (e.g. WARNING will be sent as 4)
  • host: hostname of the machine that sent the message
  • full_message: this field contains stack trace and is being written ONLY when logging an exception, e.g.

.. code:: python

try:
    1/0
except ZeroDivisionError as e:
    logger.exception(e)

.. _syslog-compliant: https://en.wikipedia.org/wiki/Syslog#Severity_level

In debug mode (when handler was created with debug=True option) each message contains some extra fields (which are pretty self-explanatory):

  • _file
  • _line
  • _module
  • _func
  • _logger_name

Configuration

Each handler has the following parameters:

  • host: IP address of the GELF input
  • port: port of the GELF input
  • debug (False by default): if true, each log message will include debugging info: module name, file name, line number, method name
  • version ('1.1' by default): GELF protocol version, can be overridden
  • include_extra_fields (False by default): if true, each log message will include all the extra fields set to LogRecord
  • json_default (:code:str with exception for several :code:datetime objects): function that is called for objects that cannot be serialized to JSON natively by python. Default implementation is custom function that returns result of :code:isoformat() method for :code:datetime.datetime, :code:datetime.time, :code:datetime.date objects and result of :code:str(obj) call for other objects (which is string representation of an object with fallback to :code:repr)

Also, there are some handler-specific parameters.

UDP:

  • chunk_size (1300 by default) - maximum length of the message. If log length exceeds this value, it splits into multiple chunks (see https://www.graylog.org/resources/gelf/ section "chunked GELF") with the length equals to this value. This parameter must be less than the MTU_. If the logs don't seem to be delivered, try to reduce this value.
  • compress (True by default) - if true, compress log messages before sending them to the server

.. _MTU: https://en.wikipedia.org/wiki/Maximum_transmission_unit

TLS:

  • validate (False by default) - if true, validate server certificate. If server provides a certificate that doesn't exist in ca_certs, you won't be able to send logs over TLS
  • ca_certs (None by default) - path to CA bundle file. This parameter is required if validate is true.
  • certfile (None by default) - path to certificate file that will be used to identify ourselves to the remote endpoint. This is necessary when the remote server has client authentication required. If certfile contains the private key, it should be placed before the certificate.
  • keyfile (None by default) - path to the private key. If the private key is stored in certfile this parameter can be None.

HTTP:

Static fields

If you need to include some static fields into your logs, simply pass them to the handler constructor. Each additional field should start with underscore. You can't add field '_id'.

Example:

.. code:: python

handler = GelfUdpHandler(host='127.0.0.1', port=9402, _app_name='pygelf', _something=11)
logger.addHandler(handler)

Dynamic fields

If you need to include some dynamic fields into your logs, add them to record by using LoggingAdapter or logging.Filter and create handler with include_extra_fields set to True. All the non-trivial fields of the record will be sent to graylog2 with '_' added before the name

Example:

.. code:: python

class ContextFilter(logging.Filter):

    def filter(self, record):
        record.job_id = threading.local().process_id
        return True

logger.addFilter(ContextFilter())
handler = GelfUdpHandler(host='127.0.0.1', port=9402, include_extra_fields=True)
logger.addHandler(handler)

Running tests

To run tests, you'll need tox_. After installing, simply run it:

.. code::

tox

.. _tox: https://pypi.python.org/pypi/tox

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].