Configuring Logging Output¶
You can register “destinations” to handle logging output; a destination is a callable that takes a message dictionary. For example, if we want to just print each new message:
import json, sys from eliot import add_destinations def stdout(message): print(message) add_destinations(stdout)
Before destinations are added¶
Up to a 1000 messages will be buffered in memory until the first set of destinations are added, at which point those messages will be delivered to newly added set of destinations. This ensures that no messages will be lost if logging happens during configuration but before a destination is added.
Outputting JSON to a file¶
Since JSON is a common output format, Eliot provides a utility class that logs to a file,
Each Eliot message will be encoded in JSON and written on a new line.
As a short hand you can call
eliot.to_file, which will create the destination and then add it automatically.
from eliot import to_file to_file(open("eliot.log", "ab"))
This destination is blocking: if writing to a file takes a long time your code will not be able to proceed until writing is done.
If you’re using Twisted you can wrap a
eliot.FileDestination with a non-blocking eliot.logwriter.ThreadedWriter.
This allows you to log to a file without blocking the Twisted
Customizing JSON Encoding¶
If you’re using Eliot’s JSON output you may wish to customize encoding.
For example, if you’re using
numpy the integer and float types won’t serialize to JSON by default.
You can do so by passing a custom
json.JSONEncoder subclass to either
import json import numpy from eliot import to_file class NumpyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, (numpy.integer, numpy.floating)): return float(obj) return json.JSONEncoder.default(self, obj) to_file(open("eliot.log", "ab"), encoder=NumpyEncoder)
For more details on JSON encoding see the Python JSON documentation.
Adding Fields to All Messages¶
Sometimes you want to add a field to all messages output by your process, regardless of destination.
For example if you’re aggregating logs from multiple processes into a central location you might want to include a field
process_id that records the name and process id of your process in every log message.
eliot.add_global_fields API to do so, e.g.:
import os, sys from eliot import add_global_fields add_global_fields(process_id="%s:%d" % (sys.argv, os.getpid()))
You should call
add_destinations to ensure all messages get the global fields.