Skip to main content

A Python logging handler for Fluentd event collector

Project description

Build Status Coverage Status

Many web/mobile applications generate huge amount of event logs (c,f. login, logout, purchase, follow, etc). To analyze these event logs could be really valuable for improving the service. However, the challenge is collecting these logs easily and reliably.

Fluentd solves that problem by having: easy installation, small footprint, plugins, reliable buffering, log forwarding, etc.

fluent-logger-python is a Python library, to record the events from Python application.

Requirements

  • Python 2.6 or greater including 3.x

  • msgpack-python

Installation

This library is distributed as ‘fluent-logger’ python package. Please execute the following command to install it.

$ pip install fluent-logger

Configuration

Fluentd daemon must be launched with a tcp source configuration:

<source>
  type forward
  port 24224
</source>

To quickly test your setup, add a matcher that logs to the stdout:

<match app.**>
  type stdout
</match>

Usage

FluentSender Interface

sender.FluentSender is a structured event logger for Fluentd.

By default, the logger assumes fluentd daemon is launched locally. You can also specify remote logger by passing the options.

from fluent import sender

# for local fluent
logger = sender.FluentSender('app')

# for remote fluent
logger = sender.FluentSender('app', host='host', port=24224)

For sending event, call emit method with your event. Following example will send the event to fluentd, with tag ‘app.follow’ and the attributes ‘from’ and ‘to’.

# Use current time
logger.emit('follow', {'from': 'userA', 'to': 'userB'})

# Specify optional time
cur_time = int(time.time())
logger.emit_with_time('follow', cur_time, {'from': 'userA', 'to':'userB'})

To send events with nanosecond-precision timestamps (Fluent 0.14 and up), specify nanosecond_precision on FluentSender.

# Use nanosecond
logger = sender.FluentSender('app', nanosecond_precision=True)
logger.emit('follow', {'from': 'userA', 'to': 'userB'})
logger.emit_with_time('follow', time.time(), {'from': 'userA', 'to': 'userB'})

You can detect an error via return value of emit. If an error happens in emit, emit returns False and get an error object using last_error method.

if not logger.emit('follow', {'from': 'userA', 'to': 'userB'}):
    print(logger.last_error)
    logger.clear_last_error() # clear stored error after handled errors

If you want to shutdown the client, call close() method.

logger.close()

Event-Based Interface

This API is a wrapper for sender.FluentSender.

First, you need to call sender.setup() to create global sender.FluentSender logger instance. This call needs to be called only once, at the beginning of the application for example.

Initialization code of Event-Based API is below:

from fluent import sender

# for local fluent
sender.setup('app')

# for remote fluent
sender.setup('app', host='host', port=24224)

Then, please create the events like this. This will send the event to fluentd, with tag ‘app.follow’ and the attributes ‘from’ and ‘to’.

from fluent import event

# send event to fluentd, with 'app.follow' tag
event.Event('follow', {
  'from': 'userA',
  'to':   'userB'
})

event.Event has one limitation which can’t return success/failure result.

Other methods for Event-Based Interface.

sender.get_global_sender # get instance of global sender
sender.close # Call FluentSender#close

Handler for buffer overflow

You can inject your own custom proc to handle buffer overflow in the event of connection failure. This will mitigate the loss of data instead of simply throwing data away.

import msgpack
from io import BytesIO

def overflow_handler(pendings):
    unpacker = msgpack.Unpacker(BytesIO(pendings))
    for unpacked in unpacker:
        print(unpacked)

logger = sender.FluentSender('app', host='host', port=24224, buffer_overflow_handler=overflow_handler)

You should handle any exception in handler. fluent-logger ignores exceptions from buffer_overflow_handler.

This handler is also called when pending events exist during close().

Python logging.Handler interface

This client-library also has FluentHandler class for Python logging module.

import logging
from fluent import handler

custom_format = {
  'host': '%(hostname)s',
  'where': '%(module)s.%(funcName)s',
  'type': '%(levelname)s',
  'stack_trace': '%(exc_text)s'
}

logging.basicConfig(level=logging.INFO)
l = logging.getLogger('fluent.test')
h = handler.FluentHandler('app.follow', host='host', port=24224, buffer_overflow_handler=overflow_handler)
formatter = handler.FluentRecordFormatter(custom_format)
h.setFormatter(formatter)
l.addHandler(h)
l.info({
  'from': 'userA',
  'to': 'userB'
})
l.info('{"from": "userC", "to": "userD"}')
l.info("This log entry will be logged with the additional key: 'message'.")

You can also customize formatter via logging.config.dictConfig

import logging.config
import yaml

with open('logging.yaml') as fd:
    conf = yaml.load(fd)

logging.config.dictConfig(conf['logging'])

You can inject your own custom proc to handle buffer overflow in the event of connection failure. This will mitigate the loss of data instead of simply throwing data away.

import msgpack
from io import BytesIO

def overflow_handler(pendings):
    unpacker = msgpack.Unpacker(BytesIO(pendings))
    for unpacked in unpacker:
        print(unpacked)

A sample configuration logging.yaml would be:

logging:
    version: 1

    formatters:
      brief:
        format: '%(message)s'
      default:
        format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
        datefmt: '%Y-%m-%d %H:%M:%S'
      fluent_fmt:
        '()': fluent.handler.FluentRecordFormatter
        format:
          level: '%(levelname)s'
          hostname: '%(hostname)s'
          where: '%(module)s.%(funcName)s'

    handlers:
        console:
            class : logging.StreamHandler
            level: DEBUG
            formatter: default
            stream: ext://sys.stdout
        fluent:
            class: fluent.handler.FluentHandler
            host: localhost
            port: 24224
            tag: test.logging
            buffer_overflow_handler: overflow_handler
            formatter: fluent_fmt
            level: DEBUG
        none:
            class: logging.NullHandler

    loggers:
        amqp:
            handlers: [none]
            propagate: False
        conf:
            handlers: [none]
            propagate: False
        '': # root logger
            handlers: [console, fluent]
            level: DEBUG
            propagate: False

Testing

Testing can be done using nose.

Release

Need wheel package.

$ pip install wheel

After that, type following command:

$ python setup.py clean sdist bdist_wheel upload

Contributors

Patches contributed by those people.

License

Apache License, Version 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fluent-logger-0.5.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

fluent_logger-0.5.1-py2.py3-none-any.whl (11.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fluent-logger-0.5.1.tar.gz.

File metadata

File hashes

Hashes for fluent-logger-0.5.1.tar.gz
Algorithm Hash digest
SHA256 85084768abc1fe8bab32486f8ead6b9dccc9288e7c54cdb0fc89df04363db256
MD5 cb114a3f74310f6d0a3789da2095b6c9
BLAKE2b-256 768ef389251af206dde6139afaf435dbbf98399b61525bfe28ee28163332674e

See more details on using hashes here.

File details

Details for the file fluent_logger-0.5.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fluent_logger-0.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d64df714fd44996d90d0a701037049299d6b8677bc4805b6bac12d59247a6f0b
MD5 3c037bba71ffabc7813476e6f5c6f178
BLAKE2b-256 7fa15598d97a62355156caf388b570add51b076c3bb9f25f94612b351e913d5c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page