Skip to main content

Log dictionaries to file using the MessagePack serialization format.

Project description

mpacklog

Build Coverage Documentation

Log dictionaries to MessagePack files in C++ and Python.

Installation

C++ with Bazel

C++ version C++ release

Add a git repository rule to your Bazel WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "mpacklog",
    remote = "https://github.com/stephane-caron/mpacklog.git",
    tag = "v2.0.0",
)

You can then use the @mpacklog dependency for C++ targets, or the @mpacklog//:python dependency for Python targets.

Python

PyPI version PyPI downloads

$ pip install mpacklog

Usage

C++

The C++ implementation uses multi-threading. Add messages to the log using the put function, they will be written to file in the background.

#include <mpacklog/Logger.h>
#include <palimpsest/Dictionary.h>

int main() {
    mpacklog::Logger logger("output.mpack");

    for (unsigned bar = 0; bar < 1000u; ++bar) {
        palimpsest::Dictionary dict;
        dict("foo") = bar;
        dict("something") = "else";
        logger.put(dict):
    }
}

Python

The Python implementation provides both a synchronous and an asynchronous API.

Asynchronous API

Add messages to the log using the put function, have them written to file in the separate write coroutine.

import asyncio
import mpacklog

async def main():
    logger = mpacklog.AsyncLogger("output.mpack")
    await asyncio.gather(main_loop(logger), logger.write())

async def main_loop(logger):
    for bar in range(1000):
        await asyncio.sleep(1e-3)
        await logger.put({"foo": bar, "something": "else"})
    await logger.stop()


if __name__ == "__main__":
    asyncio.run(main())

Synchronous API

The Synchronous API is very similar to the Asynchronous API, except it doesn't provide a stop method and the put and write methods are blocking.

import mpacklog

logger = mpacklog.SyncLogger("output.mpack")

for bar in range(1000):
    logger.put({"foo": bar, "something": "else"})

# Flush all messages to the file
logger.write()

Command-line

If you pip-installed mpacklog, you can use the mpacklog command to dump logs to JSON:

mpacklog dump my_log.mpack

Alternatively and more generally, two great tools to manipulate logs from the command line are:

  • rq: transform from/to MessagePack, JSON, YAML, TOML, ...
  • jq: manipulate JSON series to add, remove or extend fields

For instance, mpacklog dump is equivalent to:

rq -mJ < my_log.mpack

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

mpacklog-3.0.0.tar.gz (67.9 kB view details)

Uploaded Source

Built Distribution

mpacklog-3.0.0-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

Details for the file mpacklog-3.0.0.tar.gz.

File metadata

  • Download URL: mpacklog-3.0.0.tar.gz
  • Upload date:
  • Size: 67.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for mpacklog-3.0.0.tar.gz
Algorithm Hash digest
SHA256 8ee2f66615db0c91c0fa6139c78c387077577930a53185a4dea5bc4b6380ec5e
MD5 1d805bbd9eb33b2d02e71dab749b362d
BLAKE2b-256 d2c8a9110f6c345ca8f097c6156eda69b201c30852c81b027816e40e10e2d5ac

See more details on using hashes here.

Provenance

File details

Details for the file mpacklog-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: mpacklog-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 31.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for mpacklog-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecbe1c8f856f99569f499e498e1ae27b45ff80982c3baac2e09b48681574da98
MD5 a5393c28c1ce3187271e01aff7e9c2d5
BLAKE2b-256 c3f06b89099c46882ec1aa1f17aa1b920a4849484808ad7d38fce6389584b9ef

See more details on using hashes here.

Provenance

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