Skip to main content

Nameko extension exposing a structlog dependency injector

Project description

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}
    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]
 ...

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.1.1 (2018-10-29)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

nameko_structlog-0.2.0-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file nameko_structlog-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: nameko_structlog-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for nameko_structlog-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 57ffa3f528030bd7d7f8d0140632db4dcb9b3f9f28e4129763adc46472df8ef1
MD5 b211d2588b10b0a046cfcab7ab7d4402
BLAKE2b-256 91870a7a033a68b73cfd94d14bce3fed2512809f5395e1213e3772b9c24955cf

See more details on using hashes here.

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