Skip to main content

Python logging handler that sends messages to Loggly

Project description

Python Logging Loggly Handler

A simple Python logging Loggly handler that can be used to send to a Loggly Gen2 https endpoint. Borrowed the extra fields concept from the graypy logging library. Check out Loggly's Python logging documentation to learn more.

Installation

Download the repository using pip

sudo pip install loggly-python-handler

Use in python

Configuration

Create a Configuration file python.conf and add HTTPSHandler to Configuration File.

[handlers]
keys=HTTPSHandler

[handler_HTTPSHandler]
class=loggly.handlers.HTTPSHandler
formatter=jsonFormat
args=('https://logs-01.loggly.com/inputs/TOKEN/tag/python','POST')

[formatters]
keys=jsonFormat

[loggers]
keys=root

[logger_root]
handlers=HTTPSHandler
level=INFO

[formatter_jsonFormat]
format={ "loggerName":"%(name)s", "asciTime":"%(asctime)s", "fileName":"%(filename)s", "logRecordCreationTime":"%(created)f", "functionName":"%(funcName)s", "levelNo":"%(levelno)s", "lineNo":"%(lineno)d", "time":"%(msecs)d", "levelName":"%(levelname)s", "message":"%(message)s"}
datefmt=

Use Configuration in python file

import logging
import logging.config
import loggly.handlers

logging.config.fileConfig('python.conf')
logger = logging.getLogger('myLogger')

logger.info('Test log')

Use in Django

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
        'json': {
            'format': '{ "loggerName":"%(name)s", "asciTime":"%(asctime)s", "fileName":"%(filename)s", "logRecordCreationTime":"%(created)f", "functionName":"%(funcName)s", "levelNo":"%(levelno)s", "lineNo":"%(lineno)d", "time":"%(msecs)d", "levelName":"%(levelname)s", "message":"%(message)s"}',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'formatter': 'verbose',
        },
        'loggly': {
            'class': 'loggly.handlers.HTTPSHandler',
            'level': 'INFO',
            'formatter': 'json',
            'url': 'https://logs-01.loggly.com/inputs/TOKEN/tag/python',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console', ],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
        'your_app_name': {
            'handlers': ['console', 'loggly'],
            'level': 'INFO',
        },
    },
}

views.py

import logging

logger = logging.getLogger(__name__)

def logging_example(request):
    """logging example
    """
    logger.debug('this is DEBUG message.')
    logger.info('this is INFO message.')
    logger.warning('this is WARNING message.')
    logger.error('this is ERROR message.')
    logger.critical('this is CRITICAL message.')

    return Response({}, status=status.HTTP_200_OK)

Replace

  • TOKEN: your Loggly Customer Token

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

loggly-python-handler-1.0.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

loggly_python_handler-1.0.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file loggly-python-handler-1.0.1.tar.gz.

File metadata

  • Download URL: loggly-python-handler-1.0.1.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.3

File hashes

Hashes for loggly-python-handler-1.0.1.tar.gz
Algorithm Hash digest
SHA256 adce302d21fbc5b9647a01067243fb549e9992bfe5ab09807b8fad56c9411995
MD5 647f2d580c53a0cd95ac170b2efb2a0b
BLAKE2b-256 07104f91745f188334e1269f39afa34bdeace06e4f9b4e959e9ad311c9bef1a3

See more details on using hashes here.

File details

Details for the file loggly_python_handler-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: loggly_python_handler-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.3

File hashes

Hashes for loggly_python_handler-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0a4a826adb31ffeb76dac7ca116ccb032ed923254244160e70ddd2c3e88df4fd
MD5 e304192ec721339955c8c0e5b73acb1f
BLAKE2b-256 b2568c45cd75cd17691edc433354ed62aa593dac181440d44d23f08a02cef6fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page