JSON and human-readable logging with context
Project description
Sift Log - JSON logging adapter for Python (now in color)
Features
- Tag log statements with arbitrary values for easier grouping and analysis
- Add keyword arguments that are converted to JSON values
- Variable substitution
- Specifies where log calls are made from
- Meant to be used with core Python logging (formatters, handlers, etc)
- Colorized logs on a console (POSIX only)
TRACE
log level built-in
Examples
A simple log message
log.info('Hello')
{"msg": "Hello", "time": "12-12-14 10:12:01 EST", "level": "INFO", "loc": "test:log_test:20"}
Logging with tags
log.debug('Creating new user', 'MONGO', 'STORAGE')
{"msg": "Creating new user", "time": "12-12-14 10:12:09 EST", "tags": ["MONGO", "STORAGE"], "level": "DEBUG", "loc": "test:log_test:20"}
Appending more data
log.debug('Some key', is_admin=True, username='papito')
{"msg": "Some key", "is_admin": true, "username": "papito", "time": "12-12-14 10:12:04 EST", "level": "DEBUG", "loc": "test:log_test:20"}
String substitution
log.debug('User "$username" admin? $is_admin', is_admin=False, username='fez')
{"msg": "User \"fez\" admin? False", "username": "fez", "is_admin": false, "time": "12-12-14 10:12:18 EST", "level": "DEBUG", "loc": "test:log_test:20"}
Setup
Logging to console
import sys
import logging
from siftlog import SiftLog
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
log = SiftLog(logger)
In this fashion, you can direct the JSON logs to any logging handler.
Color
For enhanced flamboyancy, attach the ColorStreamHandler
to your logger. The output will not have color if the logs
are being output to a file, or on systems that are not POSIX (will not work on Windows for now).
from siftlog import SiftLog, ColorStreamHandler
logger = logging.getLogger()
handler = ColorStreamHandler(sys.stdout)
logger.addHandler(handler)
log = SiftLog(logger)
For development, you can opt in to use ColorPlainTextStreamHandler
, for logs that are easier to parse visually.
Performance
While the above should play, it's highly recommended that the color handler is only attached conditionally for local development.
Different colors
You can change font background, text color, and boldness:
from siftlog import ColorStreamHandler
handler = ColorStreamHandler(sys.stdout)
handler.set_color(
logging.DEBUG, bg=handler.WHITE, fg=handler.BLUE, bold=True
)
Supported colors
- ColorStreamHandler.BLACK
- ColorStreamHandler.RED
- ColorStreamHandler.GREEN
- ColorStreamHandler.YELLOW
- ColorStreamHandler.BLUE
- ColorStreamHandler.MAGENTA
- ColorStreamHandler.CYAN
- ColorStreamHandler.WHITE
Constants (re-occurring values)
You can define constants that will appear in every single log message. This is useful, for example, if you'd like to log process PID and hostname with every log message. This is done upon log adapter initialization:
import os
from siftlog import SiftLog
log = SiftLog(logger, pid=os.getpid(), env='INTEGRATION')
{"msg": "And here I am", "time": "12-12-14 11:12:24 EST", "pid": 37463, "env": "INTEGRATION", "level": "INFO"}
Dynamic logging context - callbacks
Often you need to add dynamic contextual data to log statements, as opposed to simple constants/literals. You can pass methods to SiftLog on initialization that will be called on every logging call.
Logging request ids or user ids are very common use cases, so to log a thread-local property with Flask, for example, we can do the following:
import flask
def get_user_id():
if flask.has_request_context():
return flask.g.user_id
user_aware_logger = SiftLog(u_id=get_user_id)
Custom time format
log = SiftLog(logger)
SiftLog.TIME_FORMAT = '%Y/%m/%d %H:%M:%S.%f'
Define the format as accepted by strftime()
Custom location format
log = SiftLog(logger)
SiftLog.LOCATION_FORMAT = '$module:$method:$line_no'
The format should be a string containing any of the following variables:
$file
$line_no
$method
$module
Custom core key names
Core keys, such as msg
and level
can be overridden, if they clash with common keys you might be using.
The following can be redefined:
- SiftLog.MESSAGE (default
msg
) - SiftLog.LEVEL (default
level
) - SiftLog.LOCATION (default
loc
) - SiftLog.TAGS (default
tags
) - SiftLog.TIME (default
time
)
As in:
log = SiftLog(logger)
SiftLog.log.MESSAGE = "MESSAGE"
Development flow
Poetry
is used to manage the dependencies.
Most things can be accessed via the Makefile, if you have Make installed. Without Make, just inspect the Makefile for the available commands.
# use the right Python
poetry use path/to/python/3.8-ish
# make sure correct Python is used
make info
# install dependencies
make install
# run tests
make test
# run visual tests (same as tests but with output)
make visual
# formatting, linting, and type checking
make lint
Running a single test
In the standard Nose tests way:
poetry run nosetests siftlog/tests/test_log.py:TestLogger.test_tags
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
Built Distribution
File details
Details for the file siftlog_py-0.9.5.tar.gz
.
File metadata
- Download URL: siftlog_py-0.9.5.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.0 CPython/3.11.1 Linux/5.4.0-65-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e977eb9c9701bcf2cb8c011e3bf98de1598bf19a77ac36b895e633c54fddb3a1 |
|
MD5 | b3180c64558693a3a0609a8b494d7b0d |
|
BLAKE2b-256 | 5ed58dc8528a6df0a0015c248e7ec376b28e5ae1dd17ccc581210cf87ddba9e3 |
File details
Details for the file siftlog_py-0.9.5-py3-none-any.whl
.
File metadata
- Download URL: siftlog_py-0.9.5-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.0 CPython/3.11.1 Linux/5.4.0-65-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8ca4f5bbe1d5742e8beb109c263d5621a9025e431a5b6c930199695696ec93c |
|
MD5 | e0a7eb148d4e742d177c3d6ceea982c2 |
|
BLAKE2b-256 | 555e1b10681508ca9a47791350f97dde0713c2a67f76c5ef33d10cd70867b7d5 |