A python package for sending logs to LogDNA

logdna, updated 🕥 2023-02-08 05:16:36

Python package for logging to LogDNA

All Contributors


Installation

bash $ pip install logdna

Setup

```python 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 * LogDNA Ingestion Key

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: ```python

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):

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

LOGGING = { # Other logging settings... 'handlers': { 'logdna': { 'level': logging.DEBUG, 'class': 'logging.handlers.LogDNAHandler', 'key': os.environ.get('LOGDNA_INGESTION_KEY'), 'options': { 'app': '', 'env': os.environ.get('ENVIRONMENT'), 'index_meta': , }, }, }, '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.

shell $ poetry install

Scripts

lint Run linting rules w/o attempting to fix them

shell $ poetry run task lint

lint:fix

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

shell $ poetry run task lint:fix

test:

Runs all unit tests and generates coverage reports

shell 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!

Issues

chore(deps): bump cryptography from 39.0.0 to 39.0.1

opened on 2023-02-08 05:16:35 by dependabot[bot]

Bumps cryptography from 39.0.0 to 39.0.1.

Changelog

Sourced from cryptography's changelog.

39.0.1 - 2023-02-07


* **SECURITY ISSUE** - Fixed a bug where ``Cipher.update_into`` accepted Python
  buffer protocol objects, but allowed immutable buffers. **CVE-2023-23931**
* Updated Windows, macOS, and Linux wheels to be compiled with OpenSSL 3.0.8.

.. _v39-0-0:

Commits


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/logdna/python/network/alerts).

Regression in 1.18.1 causing logging.config.dictConfig to hang

opened on 2022-01-07 01:08:03 by paltman

After upgrading from 1.18.0, we noticed running our celery worker hanging and tracked it down to this:

```python from logging.config import dictConfig from django.conf import settings from celery.signals import setup_logging

@setup_logging.connect def config_loggers(args, *kwargs): dictConfig(settings.LOGGING) ```

Double-checking and removing celery from the picture, we could get a hang by just calling dictConfig from the shell.

Downgrading to 1.18.0 resolved the issue.

Breaking the manual hang in the shell results in this stack trace:

```

dictConfig(settings.LOGGING) ^CTraceback (most recent call last): File "/app/.heroku/python/lib/python3.9/code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "/app/.heroku/python/lib/python3.9/logging/config.py", line 809, in dictConfig dictConfigClass(config).configure() File "/app/.heroku/python/lib/python3.9/logging/config.py", line 537, in configure _clearExistingHandlers() File "/app/.heroku/python/lib/python3.9/logging/config.py", line 274, in _clearExistingHandlers logging.shutdown(logging._handlerList[:]) File "/app/.heroku/python/lib/python3.9/logging/init.py", line 2142, in shutdown h.close() File "/app/.heroku/python/lib/python3.9/site-packages/logdna/logdna.py", line 265, in close self.request_thread_pool.shutdown(wait=True) File "/app/.heroku/python/lib/python3.9/concurrent/futures/thread.py", line 235, in shutdown t.join() File "/app/.heroku/python/lib/python3.9/threading.py", line 1053, in join self._wait_for_tstate_lock() File "/app/.heroku/python/lib/python3.9/threading.py", line 1073, in _wait_for_tstate_lock if lock.acquire(block, timeout): KeyboardInterrupt ```

Improve Code Coverage

opened on 2021-07-26 15:25:35 by smusali

This task consists of subtasks of getting this library fully covered and having integration testing

Releases

v1.18.6 2023-01-27 22:08:39

Fix

v1.18.5 2023-01-27 16:53:21

Fix

  • chore: Upgraded pytest to resolve security vulnerability in py <= 1.11.0 (#92) (905b06a)

v1.18.3 2022-12-07 17:39:46

Fix

  • Add documentation for log_error_response option (#88) (d5bf85c)

v1.18.2 2022-05-10 15:58:44

Fix

v1.18.1 2021-11-18 22:40:26

Fix

  • threading: Account for secondary buffer flush and deadlock (d00b952)

v1.18.0 2021-07-26 15:20:36

Fix

  • opts: Repair logging options for each call (e326e4c)