Skip to main content

A Python Package for Sending Logs to LogDNA

Project description

Python package for logging to LogDNA

All Contributors


Installation

$ pip install logdna

Setup

import logging
from logdna import LogDNAHandler
import os

# Set your key as an env variable
# then import here, its best not to
# hard code your key!
key=os.environ['INGESTION_KEY']

log = logging.getLogger('logdna')
log.setLevel(logging.INFO)

options = {
  'hostname': 'pytest',
  'ip': '10.0.1.1',
  'mac': 'C0:FF:EE:C0:FF:EE'
}

# Defaults to False; when True meta objects are searchable
options['index_meta'] = True
options['custom_fields'] = 'meta'


test = LogDNAHandler(key, options)

log.addHandler(test)

log.warning("Warning message", extra={'app': 'bloop'})
log.info("Info message")

Required

Optional

  • Hostname - (string)
  • MAC Address - (string)
  • IP Address - (string)
  • Index Meta - (bool) - formatted as options['index_meta']

Usage

After initial setup, logging is as easy as:

# Simplest use case
log.info('My Sample Log Line')

# Add a custom level
log.info('My Sample Log Line', extra={ 'level': 'MyCustomLevel' })

# Include an App name with this specific log
log.info('My Sample Log Line', extra={ 'level': 'Warn', 'app': 'myAppName'})

# Pass associated objects along as metadata
meta = {
    'foo': 'bar',
    'nested': {
        'nest1': 'nested text'
    }
}

opts = {
    'level': 'warn',
    'meta': meta
}

log.info('My Sample Log Line', extra=opts)

Usage with fileConfig

To use LogDNAHandler with fileConfig (e.g., in a Django settings.py file):

import os
import logging
from logdna import LogDNAHandler #  required to register `logging.handlers.LogDNAHandler`

LOGGING = {
    # Other logging settings...
    'handlers': {
        'logdna': {
            'level': logging.DEBUG,
            'class': 'logging.handlers.LogDNAHandler',
            'key': os.environ.get('LOGDNA_INGESTION_KEY'),
            'options': {
                'app': '<app name>',
                'env': os.environ.get('ENVIRONMENT'),
                'index_meta': <True|False>,
            },
        },
    },
    'loggers': {
        '': {
            'handlers': ['logdna'],
            'level': logging.DEBUG
        },
    },
}

(This example assumes you have set environment variables for ENVIRONMENT and LOGDNA_INGESTION_KEY.)

API

LogDNAHandler(key: string, [options: dict])

key

  • Required
  • Type: string
  • Values: <your ingestion key>

The LogDNA API Key associated with your account.

options

app
  • Optional
  • Type: string
  • Default: ''
  • Values: <your custom app>

The default app named that is included in every every log line sent through this instance.

env
  • Optional
  • Type: string
  • Default: ''
  • Values: <your custom env>

The default env passed along with every log sent through this instance.

hostname
  • Optional
  • Type: string
  • Default: ''
  • Values: <your custom hostname>

The default hostname passed along with every log sent through this instance.

include_standard_meta
  • Optional
  • Type: bool
  • Default: False

Python LogRecord objects includes language-specific information that may be useful metadata in logs. Setting include_standard_meta to True automatically populates meta objects with name, pathname, and lineno from the LogRecord.

WARNING This option is deprecated and will be removed in the upcoming major release.

index_meta
  • Optional
  • Type: bool
  • Default: False

We allow meta objects to be passed with each line. By default these meta objects are stringified and not searchable, and are only displayed for informational purposes.

If this option is set to True then meta objects are parsed and searchable up to three levels deep. Any fields deeper than three levels are stringified and cannot be searched.

WARNING If this option is True, your metadata objects MUST have consistent types across all log messages or the metadata object might not be parsed properly.

level
  • Optional
  • Type: string
  • Default: Info
  • Values: Debug, Trace, Info, Warn, Error, Fatal, <your custom level>

The default level passed along with every log sent through this instance.

verbose
  • Optional
  • Type: string or bool
  • Default: True
  • Values: False or any level

Sets the verbosity of log statements for failures.

request_timeout
  • Optional
  • Type: int
  • Default: 30000

The amount of time (in ms) the request should wait for LogDNA to respond before timing out.

tags

List of tags used to dynamically group hosts. More information on tags is available at How Do I Use Host Tags?

url
  • Optional
  • Type: string
  • Default: 'https://logs.logdna.com/logs/ingest'

A custom ingestion endpoint to stream log lines into.

custom_fields
  • Optional
  • Type: list<string>
  • Default: ['args', 'name', 'pathname', 'lineno']

List of fields out of record object to include in the meta object. By default, args, name, pathname, and lineno will be included.

log_error_response
  • Optional
  • Type: bool
  • Default: False

Enables logging of the API response when an HTTP error is encountered

log(line, [options])

line

  • Required
  • Type: string
  • Default: ''

The log line to be sent to LogDNA.

options

level
  • Optional
  • Type: string
  • Default: Info
  • Values: Debug, Trace, Info, Warn, Error, Fatal, <your custom level>

The level passed along with this log line.

app
  • Optional
  • Type: string
  • Default: ''
  • Values: <your custom app>

The app passed along with this log line.

env
  • Optional
  • Type: string
  • Default: ''
  • Values: <your custom env>

The environment passed with this log line.

meta
  • Optional
  • Type: dict
  • Default: None

A standard dictonary containing additional metadata about the log line that is passed. Please ensure values are JSON serializable.

NOTE: Values that are not JSON serializable will be removed and the respective keys will be added to the __errors string.

index_meta
  • Optional
  • Type: bool
  • Default: False

We allow meta objects to be passed with each line. By default these meta objects will be stringified and will not be searchable, but will be displayed for informational purposes.

If this option is turned to true then meta objects will be parsed and will be searchable up to three levels deep. Any fields deeper than three levels will be stringified and cannot be searched.

WARNING When this option is true, your metadata objects across all types of log messages MUST have consistent types or the metadata object may not be parsed properly!

timestamp

The time in seconds since the epoch to use for the log timestamp. It must be within one day or current time - if it is not, it is ignored and time.time() is used in its place.

Development

This project makes use of the poetry package manager for local development.

$ poetry install

Scripts

lint Run linting rules w/o attempting to fix them

$ poetry run task lint

lint:fix

Run lint rules against all local python files and attempt to fix where possible.

$ poetry run task lint:fix

test:

Runs all unit tests and generates coverage reports

poetry run task test

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Muaz Siddiqui

💻 📖 ⚠️

Samir Musali

💻 📖 ⚠️

vilyapilya

💻 🚧 ⚠️

Mike Hu

📖

Eric Satterwhite

💻 📖 ⚠️ 🔧

Łukasz Bołdys (Lukasz Boldys)

💻 🐛

Ryan

📖

Mike Huang

💻 🐛

Dan Maas

💻

DChai

📖

Jakob de Maeyer

💻

Andrey Babak

💻

Mike S

💻 📖

Brennan Ashton

💻

Christian Hotz-Behofsits

💻 🐛

Kurtiss Hare

🐛

Alexey Kinev

🐛

matthiasfru

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © LogDNA Copyright © 2017 LogDNA, released under an MIT license. See the LICENSE file and https://opensource.org/licenses/MIT

Happy Logging!

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

logdna-1.18.12.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

logdna-1.18.12-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file logdna-1.18.12.tar.gz.

File metadata

  • Download URL: logdna-1.18.12.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.10

File hashes

Hashes for logdna-1.18.12.tar.gz
Algorithm Hash digest
SHA256 ce9d4ae3be39128c605a5062d8d5e57cb482d3e377e36164d19932e163646cb0
MD5 0777df4fd0b4d3cdad4809291de12de2
BLAKE2b-256 dc6ef0de50e7b646bfae6f666a7eb8a98dd78af4fde2d53e58c9edfe396c7093

See more details on using hashes here.

File details

Details for the file logdna-1.18.12-py3-none-any.whl.

File metadata

  • Download URL: logdna-1.18.12-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.10

File hashes

Hashes for logdna-1.18.12-py3-none-any.whl
Algorithm Hash digest
SHA256 a30b71e0c9d61b20b017990a2601f422934d969ede1eb581bde3e7d9e7e8a648
MD5 132dd8fd9f7087859d8d512f9c32a9c8
BLAKE2b-256 17ee84be073de85b5216653384130b03d9acdae83af805d7909572b0efc160c9

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