Build_Status

Installing

Using easy_install:

easy_install graypy

Install with requirements for GELFRabbitHandler:

easy_install graypy[amqp]

Usage

Messages are sent to Graylog2 using a custom handler for the builtin logging library in GELF format:

import logging
import graypy

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201)
my_logger.addHandler(handler)

my_logger.debug('Hello Graylog2.')

Alternately, use GELFRabbitHandler to send messages to RabbitMQ and configure your Graylog2 server to consume messages via AMQP. This prevents log messages from being lost due to dropped UDP packets (GELFHandler sends messages to Graylog2 using UDP). You will need to configure RabbitMQ with a 'gelf_log' queue and bind it to the 'logging.gelf' exchange so messages are properly routed to a queue that can be consumed by Graylog2 (the queue and exchange names may be customized to your liking):

import logging
import graypy

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/', exchange='logging.gelf')
my_logger.addHandler(handler)

my_logger.debug('Hello Graylog2.')

Tracebacks are added as full messages:

import logging
import graypy

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201)
my_logger.addHandler(handler)

try:
    puff_the_magic_dragon()
except NameError:
    my_logger.debug('No dragons here.', exc_info=1)

Configuration parameters

GELFHandler:

  • host - the host of the graylog server.
  • port - the port of the graylog server (default 12201).
  • chunk_size - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default 1420).
  • debugging_fields - send debug fields if true (the default).
  • extra_fields - send extra fields on the log record to graylog if true (the default).
  • fqdn - use fully qualified domain name of localhost as source host (socket.getfqdn()).
  • localname - use specified hostname as source host.
  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.
  • level_names - allows the use of string error level names instead in addition to their numerical representation.

GELFRabbitHandler:

  • url - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F).
  • exchange - RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped.
  • debugging_fields - send debug fields if true (the default).
  • extra_fields - send extra fields on the log record to graylog if true (the default).
  • fqdn - use fully qualified domain name of localhost as source host - socket.getfqdn().
  • exchange_type - RabbitMQ exchange type (default fanout).
  • localname - use specified hostname as source host.
  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.

Using with Django

It's easy to integrate graypy with Django's logging settings. Just add a new handler in your settings.py like this:

LOGGING = {
    ...

    'handlers': {
        'graypy': {
            'level': 'WARNING',
            'class': 'graypy.GELFHandler',
            'host': 'localhost',
            'port': 12201,
        },
    },

    'loggers': {
        'django.request': {
            'handlers': ['graypy'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

Custom fields

A number of custom fields are automatically added if available:
  • function
  • pid
  • process_name
  • thread_name

You can disable these additional fields if you don't want them by adding an argument to the handler:

handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False)

graypy also supports additional fields to be included in the messages sent to Graylog2. This can be done by using Python's LoggerAdapter and Filter. In general, LoggerAdapter makes it easy to add static information to your log messages and Filters give you more flexibility, for example to add additional information based on the message that is being logged.

Example using LoggerAdapter:

import logging
import graypy

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201)
my_logger.addHandler(handler)

my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'),
                                    { 'username': 'John' })

my_adapter.debug('Hello Graylog2 from John.')

Example using Filter:

import logging
import graypy

class UsernameFilter(logging.Filter):
    def __init__(self):
        # In an actual use case would dynamically get this (e.g. from memcache)
        self.username = "John"

    def filter(self, record):
        record.username = self.username
        return True

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201)
my_logger.addHandler(handler)

my_logger.addFilter(UsernameFilter())

my_logger.debug('Hello Graylog2 from John.')

Contributors:

  • Sever Banesiu
  • Daniel Miller
  • Tushar Makkar


Build_Status

安装

使用easy_install:

easy_install graypy

安装与 GELFRabbitHandler 的要求:

easy_install graypy[amqp]

用法

使用GELF格式的内置日志记录库的自定义处理程序将消息发送到Graylog2:

import logging
import graypy

my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler)

my_logger.debug('Hello Graylog2.')

另外,使用 GELFRabbitHandler 将消息发送到RabbitMQ,并将您的Graylog2服务器配置为通过AMQP消费消息。这样可以防止由于丢弃的UDP数据包( GELFHandler 使用UDP向Graylog2发送消息)而丢失日志消息。您将需要使用’gelf_log’队列配置RabbitMQ,并将其绑定到’logging.gelf’交换机,以便消息正确路由到Graylog2可以使用的队列(队列和交换名称可能根据您的喜好自定义) :

import logging
import graypy

my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/', exchange='logging.gelf') my_logger.addHandler(handler)

my_logger.debug('Hello Graylog2.')

追踪回溯作为完整信息添加:

import logging
import graypy

my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler)

try: puff_the_magic_dragon() except NameError: my_logger.debug('No dragons here.', exc_info=1)

配置参数

GELFHandler:

  • host - the host of the graylog server.
  • port - the port of the graylog server (default 12201).
  • chunk_size - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default 1420).
  • debugging_fields - send debug fields if true (the default).
  • extra_fields - send extra fields on the log record to graylog if true (the default).
  • fqdn - use fully qualified domain name of localhost as source host (socket.getfqdn()).
  • localname - use specified hostname as source host.
  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.
  • level_names - allows the use of string error level names instead in addition to their numerical representation.

GELFRabbitHandler:

  • url - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F).
  • exchange - RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped.
  • debugging_fields - send debug fields if true (the default).
  • extra_fields - send extra fields on the log record to graylog if true (the default).
  • fqdn - use fully qualified domain name of localhost as source host - socket.getfqdn().
  • exchange_type - RabbitMQ exchange type (default fanout).
  • localname - use specified hostname as source host.
  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.

使用Django

graypy 与Django的日志设置进行集成是很容易的。只需添加一个 您的 settings.py 中的新处理程序如下所示:

LOGGING = {
    …

'handlers': {
    'graypy': {
        'level': 'WARNING',
        'class': 'graypy.GELFHandler',
        'host': 'localhost',
        'port': 12201,
    },
},

'loggers': {
    'django.request': {
        'handlers': ['graypy'],
        'level': 'ERROR',
        'propagate': True,
    },
},

}

自定义字段

A number of custom fields are automatically added if available:
  • function
  • pid
  • process_name
  • thread_name

如果您不想通过向处理程序添加参数,则可以禁用这些其他字段:

handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False)

graypy还支持将其他字段包含在发送到Graylog2的消息中。这可以通过使用Python的 LoggerAdapter 和<一个href =http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information>过滤器。通常,LoggerAdapter可以方便地将静态信息添加到日志消息中,而过滤器可以提供更多的灵活性,例如根据正在记录的消息添加其他信息。

使用 LoggerAdapter 的示例:

import logging
import graypy

my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler)

my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'), { 'username': 'John' })

my_adapter.debug('Hello Graylog2 from John.')

使用过滤器的示例:

import logging
import graypy

class UsernameFilter(logging.Filter): def init(self): # In an actual use case would dynamically get this (e.g. from memcache) self.username = "John"

def filter(self, record):
    record.username = self.username
    return True

my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFHandler('localhost', 12201) my_logger.addHandler(handler)

my_logger.addFilter(UsernameFilter())

my_logger.debug('Hello Graylog2 from John.')

贡献者:

  • Sever Banesiu
  • Daniel Miller
  • Tushar Makkar




相关问题推荐