Skip to main content

Python GELF

Install from PyPI

pip install gelf

Configure handler from a python script

import logging
import time
from uuid import uuid4

from gelf.handler import GELFHandlerTCP

logger = logging.getLogger("example.module")
logger.setLevel(logging.DEBUG)

handler = GELFHandlerTCP(
    host="127.0.0.1",
    port=12201,
    service="example",
    include_extra_fields=True,
    debug=True,
)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
# not strictly needed, this is to display logs on stdout
logger.addHandler(logging.StreamHandler())

logger.info(
    "This message gives a lot of information",
    extra={
        "request_id": "4edba63411eb4c8f808e32e904d29c1c",
        "user_id": "bf5089c16355401a9bff3efd6b2ada2e"
    },
)

Configure handler using a config file

  • In python script use logging.config.fileConfig to load logging config from file
import logging
import logging.config
import time
from uuid import uuid4

logging.config.fileConfig("logging.conf")

logger = logging.getLogger("example.module")

logger.info(
    "This message gives a lot of information",
    extra={
        "request_id": "4edba63411eb4c8f808e32e904d29c1c",
        "user_id": "bf5089c16355401a9bff3efd6b2ada2e"
    },
)
  • Put your logging config in a file. Here logging.conf
[formatters]
keys = 

[handlers]
keys = stdout,gelf

# not strictly needed, this is to display logs on stdout
[handler_stdout]
class = logging.StreamHandler
level = DEBUG

[handler_gelf]
class = gelf.handler.GELFHandlerTCP
args = ('localhost', '12201', os.getenv('SERVICE', 'unknown'))

[loggers]
keys = root

[logger_root]
handlers = stdout,gelf
level = DEBUG

NOTE: In this example service field of log records is retrieved from SERVICE environment variable but you might as well provide a fixed value.

Release files for gelf 0.0.3

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

Source distribution (sdist)

Source distribution for gelf 0.0.3
File Size Uploaded
gelf-0.0.3.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gelf 0.0.3
File Interpreter ABI Platform
gelf-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 6.3 kB

Release files / gelf-0.0.3.tar.gz

Download URL gelf-0.0.3.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a7732ea0a3380f6c9440daccd2aec4dfd56901de6d956ad6fca2a449a73039d6
BLAKE2b-256 checksum
How to use checksums
1c3454d3a0fa71b6be8f9c3c3847ba7ef51daae48c33ae74fd65a6e7c7506498
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release files / gelf-0.0.3-py3-none-any.whl

Download URL gelf-0.0.3-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a60845bc3df7c47af1f78c3df865aebc6bd630ffec121199fbb9754c0687717
BLAKE2b-256 checksum
How to use checksums
ab48bc59429e76a7c31d051fadaa7b41e4c9a87367df5ea455a722fb9d4cfd3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

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