Skip to main content

Python logging handler for Logstash and StackDriver.

Project description

Coverage Status

TemLogger

Temlogger is a library to send logs to ELK, StackDriver(Google Cloud Logging).

Features

Temlogger gives you:

  • Flexibility to send logs to StackDriver(Google Cloud Logging) or ELK (Elastic, Logstash and Kibana).

  • Register events handlers(globally and per logger) to update log entry before send to providers.

  • 98% test coverage.

Logging Providers

  • logstash (ELK)

  • stackdriver (Google StackDriver)

  • default (don’t send logs)

Requirements

  • python 3.6+

  • python3-logstash == 0.4.80

  • google-cloud-logging>=1.14.0,<2

Instalation

pip install -e git+https://github.com/tembici/temlogger.git#egg=temlogger

Use this to specify tag version:

pip install -e git+https://github.com/tembici/temlogger.git@<tag-version>#egg=temlogger

Usage

Using environment variables:

export TEMLOGGER_PROVIDER='logstash'
export TEMLOGGER_URL='localhost'
export TEMLOGGER_PORT='5000'
export TEMLOGGER_ENVIRONMENT='staging'
import sys
import temlogger

test_logger = temlogger.getLogger('python-logstash-logger')

test_logger.error('python-logstash: test logstash error message.')
test_logger.info('python-logstash: test logstash info message.')
test_logger.warning('python-logstash: test logstash warning message.')

# add extra field to logstash message
extra = {
    'test_string': 'python version: ' + repr(sys.version_info),
    'test_boolean': True,
    'test_dict': {'a': 1, 'b': 'c'},
    'test_float': 1.23,
    'test_integer': 123,
    'test_list': [1, 2, '3'],
}
test_logger.info('temlogger: test with extra fields', extra=extra)

Example passing parameters directly to temlogger:

import sys
import temlogger

temlogger.config.set_provider('logstash')
temlogger.config.set_url('localhost')
temlogger.config.set_port(5000)
temlogger.config.set_environment('staging')

test_logger = temlogger.getLogger('python-logstash-logger')

test_logger.info('python-logstash: test logstash info message.')

# add extra field to logstash message
extra = {
    'test_string': 'python version: ' + repr(sys.version_info),
    'test_boolean': True,
    'test_dict': {'a': 1, 'b': 'c'},
    'test_float': 1.23,
    'test_integer': 123,
    'test_list': [1, 2, '3'],
}
test_logger.info('temlogger: test with extra fields', extra=extra)

Example with StackDriver

Documentation of how set ``GOOGLE_APPLICATION_CREDENTIALS` environment variable <https://cloud.google.com/docs/authentication/getting-started>`__

export TEMLOGGER_PROVIDER='stackdriver'
export GOOGLE_APPLICATION_CREDENTIALS='<path to json>'
import sys
import temlogger

test_logger = temlogger.getLogger('python-stackdriver-logger')

test_logger.info('python-stackdriver: test stackdriver info message.')

# add extra field to stackdriver message
extra = {
    'test_string': 'python version: ' + repr(sys.version_info),
    'test_boolean': True,
    'test_dict': {'a': 1, 'b': 'c'},
    'test_float': 1.23,
    'test_integer': 123,
    'test_list': [1, 2, '3'],
}
test_logger.info('temlogger: test with extra fields', extra=extra)

Using with Django

Modify your settings.py to integrate temlogger with Django’s logging:

import temlogger

host = 'localhost'

temlogger.config.set_provider('logstash')
temlogger.config.set_url('localhost')
temlogger.config.set_port(5000)
temlogger.config.set_environment('staging')

Then in others files such as views.py,models.py you can use in this way:

import temlogger

test_logger = temlogger.getLogger('python-logger')

Event Handlers

This functionality allow register handlers before send log to Logging Providers.

Register event handlers globally

Is recommended initialize event handlers early as possible, for example in settings.py for django. The below example shows how register a handler add_tracker_id_to_message globally.

import temlogger

temlogger.config.set_provider('logstash')
temlogger.config.setup_event_handlers([
    'temlogger.tests.base.add_tracker_id_to_message',
])

logger = temlogger.getLogger('python-logger')

extra = {
    'app_name': 'tembici'
}

logger.info('test with extra fields', extra=extra)

Register event handlers per logger

The below example shows how register a handler add_user_id_key for one logger.

import temlogger

def add_user_id_key(message):
    message['user_id'] = 'User Id'
    return message

temlogger.config.set_provider('logstash')

logger = temlogger.getLogger('python-logger', event_handlers=[
    'temlogger.tests.base.add_tracker_id_to_message',
    add_user_id_key
])
extra = {
    'app_name': 'tembici'
}

logger.info('test with extra fields', extra=extra)

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

temlogger-test-0.1.0b0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

temlogger_test-0.1.0b0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file temlogger-test-0.1.0b0.tar.gz.

File metadata

  • Download URL: temlogger-test-0.1.0b0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5

File hashes

Hashes for temlogger-test-0.1.0b0.tar.gz
Algorithm Hash digest
SHA256 05b4f8884f3810b1f8982afaa180f859c81f2f1e7e9a7624f69242cde2de7e03
MD5 8cf308f6cefc968fc5bdbe2a3ef8086a
BLAKE2b-256 cd7eb88fcd33631dc1ccb40968ddff54ef6324ad41b4c78df8001820a999e273

See more details on using hashes here.

File details

Details for the file temlogger_test-0.1.0b0-py3-none-any.whl.

File metadata

  • Download URL: temlogger_test-0.1.0b0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5

File hashes

Hashes for temlogger_test-0.1.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9597b5982479d92635137ee9f1df334b2d4ae118cfe5b0da1f455fd3a734070
MD5 22cd1498b65146c0ac3f581f7ecc65c3
BLAKE2b-256 fd8fb166b14f5f7715f7e8adbbf1424964e45b974694645f0d67b2ca94a2584e

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