Python logging handler for DataDog events
Project description
DataDog Logger
Note: This library does not send logs to Datadog's Log Management product. See Datadog's documentation for how to configure log collection. See https://docs.datadoghq.com/logs/log_collection/
A Python logging.Handler
for sending log messages to DataDog as Events in
the Events Explorer.
Installation
pip install datadog-logger
Usage
The simplest way to enable logging to DataDog is to use the
log_error_events
helper, which will cause all logging.ERROR
and
higher messages to be sent to DataDog:
import datadog
from datadog_logger import log_error_events
import logging
# Authenticate with DataDog
datadog.initialize(api_key="api-key", app_key="app-key")
# Note, a normal STDOUT handler will not be configured if this is not
# called first
logging.basicConfig()
log_error_events(tags=["tag1:value", "tag2:value"], mentions=["@devs", "@slack"])
logging.error("Oh no!")
This will enable the handler on the root logger; tags
and mentions
may both be None
, in which case none will be included. The handler
will have a log level of logging.ERROR
, meaning only ERROR
and
CRITICAL
(or any higher custom levels) will be sent to DataDog.
A specific logger may also be specified by name:
log_error_events("some.logger")
Details
The Handler
may also be created and added to a logger manually:
import datadog
from datadog_logger import DatadogLogHandler
import logging
datadog.initialize(api_key="api-key", app_key="app-key")
datadog_handler = DatadogLogHandler(
tags=["some:tag"], mentions=["@some-mention"], level=logging.WARNING)
# Enable STDOUT logging
logging.basicConfig()
# The root logger
logger = logging.getLogger()
logger.addHandler(datadog_handler)
# This will be logged, because the level is WARNING
logger.warning("Watch out!")
# This will not
logger.info("Lets not log everything to DataDog")
When the event is created, the msg
of the log record will be passed
as the title of the event. Normally, this would be the string passed
into the logging call, with any %
substitutions applied.
The text of the event will be the formatted record. If the log
record includes exc_info
(i.e. because you called
logging.exception
, or passed exc_info
to the log function), then
the full stack trace will be included in the event text.
Any @mentions
passed via the mentions
constructor argument will be
appended to the end of the text, so this library can be used to alert,
via DataDog, on particular log messages.
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
Built Distribution
File details
Details for the file datadog_logger-1.0.2.tar.gz
.
File metadata
- Download URL: datadog_logger-1.0.2.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/5.15.0-1050-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | efc2bb467c4e49088b4054e3897ab425e1f86c48125b2336f35c109e96527f03 |
|
MD5 | 28cb8e24cc19c39116646b2fb507e472 |
|
BLAKE2b-256 | 837f60b16988f542f92914a5a5eaa979e4431b91c367dce63d6fa571471b3930 |
File details
Details for the file datadog_logger-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: datadog_logger-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/5.15.0-1050-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f1b04a158fa689891de1271620757f7bb22069e9ea5089ff049f58b85f6f24d |
|
MD5 | cb7f97a0ae7979d7cbe9c54d5c2ab7b3 |
|
BLAKE2b-256 | 8e2a11c7e300b0ef150328e8f5025a50bb0b03e88af0a52c0e3d9f45a7472431 |