Skip to main content

Logging formatters for ECS (Elastic Common Schema) in Python

Project description

ecs-logging-python

Build Status PyPI Versions Supported

Please note that this is library is in a beta version and backwards-incompatible changes might be introduced in future releases. While we strive to comply to semver, we can not guarantee to avoid breaking changes in minor releases.

Check out the Elastic Common Schema (ECS) reference for more information.

The library currently implements ECS 1.5, after a 1.x version is released we will be following (ECS.major).(ECS.minor).(package minor) as our versioning scheme.

Installation

python -m pip install ecs-logging

Getting Started

ecs-logging-python has formatters for the standard library logging module and the structlog package.

Logging Example

import logging
import ecs_logging

# Get the Logger
logger = logging.getLogger("app")
logger.setLevel(logging.DEBUG)

# Add an ECS formatter to the Handler
handler = logging.StreamHandler()
handler.setFormatter(ecs_logging.StdlibFormatter())
logger.addHandler(handler)

# Emit a log!
logger.debug("Example message!", extra={"http.request.method": "get"})
{
    "@timestamp": "2020-03-20T18:11:37.895Z",
    "ecs": {
        "version": "1.5.0"
    },
    "http": {
      "request": {
        "method": "get"
      }
    },
    "log": {
        "level": "debug",
        "logger": "app",
        "origin": {
            "file": {
                "line": 14,
                "name": "test.py"
            },
            "function": "func"
        },
        "original": "Example message!"
    },
    "message": "Example message!"
}

Structlog Example

import structlog
import ecs_logging

# Configure Structlog
structlog.configure(
    processors=[ecs_logging.StructlogFormatter()],
    wrapper_class=structlog.BoundLogger,
    context_class=dict,
    logger_factory=structlog.PrintLoggerFactory(),
)

# Get the Logger
logger = structlog.get_logger("app")

# Add additional context
logger = logger.bind(**{
    "http": {
        "version": "2",
        "request": {
            "method": "get",
            "bytes": 1337,
        },
    },
    "url": {
        "domain": "example.com",
        "path": "/",
        "port": 443,
        "scheme": "https",
        "registered_domain": "example.com",
        "top_level_domain": "com",
        "original": "https://example.com",
    }
})

# Emit a log!
logger.debug("Example message!")
{
  "@timestamp": "2020-03-26T13:08:11.728Z",
  "ecs": {
    "version": "1.5.0"
  },
  "http": {
    "request": {
      "bytes": 1337,
      "method": "get"
    },
    "version": "2"
  },
  "log": {
    "level": "debug"
  },
  "message": "Example message!",
  "url": {
    "domain": "example.com",
    "original": "https://example.com",
    "path": "/",
    "port": 443,
    "registered_domain": "example.com",
    "scheme": "https",
    "top_level_domain": "com"
  }
}

License

Apache-2.0

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

ecs-logging-0.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

ecs_logging-0.2-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ecs-logging-0.2.tar.gz.

File metadata

  • Download URL: ecs-logging-0.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for ecs-logging-0.2.tar.gz
Algorithm Hash digest
SHA256 f2ff77d47a9c2a4bcd8ecac826166fc884184514887446246c00ed8c98014bcf
MD5 c79de3785a6c4e81e16aa3c2779f3bf6
BLAKE2b-256 7603f0d5c0f3d12289dd00dafc0a32db3830e2ce235d3b829aee1521eab2b8f4

See more details on using hashes here.

File details

Details for the file ecs_logging-0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: ecs_logging-0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for ecs_logging-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e9126e29c1eac62f2d8eb83f821c2b275216b6b3d9ce94e827785ef61197826b
MD5 20fd45b14981fee166957057d3a3761e
BLAKE2b-256 8ce22078c88854a359f371568bef92c327e766f306a1ca24e706420307727c1c

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