API reference

Driftwood

A collection of python logging extensions

Features

  • Compatible with Python 3 (only)
  • Provides Dictionary, JSON, and MongoDB logging
  • Features for logging custom attributes
  • Can notify of status changes based on the level of messages being logged

Get Driftwood

pip install driftwood

Run the tests

Assumes you have Driftwood already installed

pip install nose2
git clone https://github.com/HurricaneLabs/driftwood.git
cd driftwood/test/unit
nose2

JSON Formatter

This code example:

import logging
from driftwood.formatters import JSONFormatter

log = logging.getLogger("test")
handler = logging.StreamHandler()

json_formatter = JSONFormatter()
handler.setFormatter(json_formatter)

log.addHandler(handler)
log.warning("uh oh")

Produces (as a string, not a dict):

{"created": 1422386241.4394472, "pathname": "<stdin>", "message": "uh oh", "threadName": "MainThread", "levelname": "WARNING", "process": 4384, "module": "<stdin>", "thread": 139785634490176, "levelno": 30, "msecs": 439.44716453552246, "filename": "<stdin>", "lineno": 1, "relativeCreated": 52455.650329589844, "funcName": "<module>", "name": "test"}

MongoHandler

This handler is used to log records to MongoDB. The following code:

import logging
import os

from driftwood.handlers.mongo import MongoHandler, LogRecord
import mongoengine

mongoengine.connect("testdb", host=os.environ["MONGO_PORT_27017_TCP_ADDR"])
MongoClient('172.17.0.50', 27017)

mongo_handler = MongoHandler()
log = logging.getLogger("test")
log.addHandler(mongo_handler)

log.error("something bad happened")
print(LogRecord.objects)
print(LogRecord.objects[0].message)

Produces:

[<LogRecord: LogRecord object>]
something bad happened

Your message has been logged to mongodb and includes all standard logging attributes except asctime. See the full documentation for including extra attributes, as provided by the DictHandler base class.

StatusUpdateAdapter

This logging.LoggerAdapter is used to track the status of an operation based on the level of messages being logged. Every time a message is logged, if the level is higher than any previous message, a callback is triggered to alert of the status change.

import logging

from driftwood.adapters import StatusUpdateAdapter

def status_update(levelno, levelname):
    print("The status has changed to {0}".format(levelname))

log = logging.getLogger("test")
log.setLevel(logging.CRITICAL)
adapter = StatusUpdateAdapter(status_update, log)

adapter.info("info test")
adapter.warning("warning test")
adapter.error("error test")

adapter.info("won't trigger the callback")

Produces:

The status has changed to INFO
The status has changed to WARNING
The status has changed to ERROR

Table of Contents