Skip to main content

Python logging handler that sends messages to HTTPS endpoints.

Project description

Python Logging Handler
-----------------------------

A simple Python logging handler that can be used to send to a https endpoint. Borrowed the extra fields concept from the graypy logging library.

## History
This repository if originally forked from (https://github.com/varshneyjayant/loggly-python-handler)

## Installation
Download the repository using pip

sudo pip install loggly-python-handler

## Use in python
### Configuration

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

[handlers]
keys=RocketChatHandler

[handler_RocketChatHandler]
class=oxomo.handlers.RocketChatHandler
formatter=jsonFormat
args=('ROCKETCHAT_WEBHOOK_URL','POST')

[formatters]
keys=jsonFormat

[loggers]
keys=root

[logger_root]
handlers=RocketChatHandler
level=ERROR

[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 oxomo.handlers

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

logger.error('Test Error Log')

## Optionally log uncaught exceptions as errors

def sysexcept_handler(type, value, tb):
logger.error("Uncaught exception: {0}".format(str(value)))
sys.__excepthook__(type, value, tb)
sys.excepthook = sysexcept_handler

## 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',
},
'oxomo': {
'class': 'oxomo.handlers.RocketChatHandler',
'level': 'INFO',
'formatter': 'json',
'url': 'ROCKETCHAT_WEBHOOK_URL',
},
},
'loggers': {
'django': {
'handlers': ['console', ],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
'your_app_name': {
'handlers': ['console', 'oxomo'],
'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)

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

oxomo-0.1.2.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file oxomo-0.1.2.tar.gz.

File metadata

  • Download URL: oxomo-0.1.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for oxomo-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7eaf8d3764f0a47642edbbe3b7dc1e70c303b38e85b25624b78ff77b0f34c39d
MD5 6ad8fc19427e865336c98b1b6f1a5f20
BLAKE2b-256 078c7adfb49b52bcbb5b96ee80cf094d59837024ef1bf212c0db6d281a781005

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