Skip to main content

nameko-structlog

Structlog as nameko extension

  • Free software: Apache Software License 2.0

Extension for nameko that replaces python logging module with structlog. The idea behind this module is to use JSONRenderer to be able to use advanced log aggregation and analysis tools like Logstash.

Apart from JSONRenderer structlog processor, it’s also supported KeyValueRenderer processor.

Installation

To install nameko-structlog, simply use pip.

pip install nameko-structlog

Usage

Add Structlog log level to your nameko config file:

 # config.yml when using JSONRenderer
 STRUCTLOG:
    INCLUDE_WORKER_NAME: ${INCLUDE_WORKER_NAME:true}
    INCLUDE_LOG_TRANSACTON_ID: true
    PROCESSOR_NAME: JSONRenderer
    PROCESSOR_OPTIONS:
      sort_keys: true
    EXTRA_PARAMETERS:
       pin: 1234
       env: dev

LOGGING:
  version: 1
  formatters:
    simple:
      format: "%(message)s"

  handlers:
    console:
      class: logging.StreamHandler
      formatter: simple

  root:
    level: DEBUG
    handlers: [console]
 ...

Option INCLUDE_WORKER_NAME will add or not worker_ctx.call_id to every log entry. Option INCLUDE_LOG_TRANSACTON_ID will group all logs that refer to same nameko entrypoint, with a unique log_transaction_id. Doing so may be useful to follow request/data flow on a log aggregation tool. Option EXTRA_PARAMETERS can contain any keys that you want to appear on every log entry.

Include the StructlogDependency dependency in your service class:

# service.py
from nameko.rpc import rpc

from nameko_structlog import StructlogDependency

class MyService(object):
   name = "demo"

   log = StructlogDependency()

   @rpc
   def my_method(self, name):
      self.log.info(message=f"Your name is {name}", type="greeting")

Run your service, providing the config file:

$ nameko run service --config config.yaml

$ nameko shell --config config.yaml
>>> n.rpc.demo.my_method("Alice")
{"level": "info", "log_transaction_id": "b2cd5506-339e-4e59-9a14-a3cd7548bfe5", "logger": "demo", "env": "dev", message": "Your name is Alice", "pin": "1234", "timestamp": "2020-09-27T11:24:30.379918Z", "type": "greeting"}

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.2.1 (2020-12-28)

  • Fix bug #8.

0.2.0 (2020-12-28)

  • Drop indent=2 for log records

  • Drop colorama and development mode.

  • Support two structlog processors, JSONRenderer and KeyValueRenderer.

  • Configuration is more clean now.

  • Decouple Structlog configuration from nameko dependency.

0.1.1 (2018-10-29)

  • First release on PyPI.

Metadata

Release files for nameko-structlog 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for nameko-structlog 0.2.2
File Interpreter ABI Platform
nameko_structlog-0.2.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / nameko_structlog-0.2.2-py2.py3-none-any.whl

Download URL nameko_structlog-0.2.2-py2.py3-none-any.whl
Size 5.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6592207bf423b14560075d91f1b13be401ddabc87e53fc6d96123b91cc76aea3
BLAKE2b-256 checksum
How to use checksums
5cbb5963f08f4545aacd1786f8d5e45eb0ba86cfc4ca27b93553d22329dcf966
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.11

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page