Skip to main content

Client for TaskWorkshop exceptions

Project description

TWLogger is a Python client for TaskWorkshop exceptions tracking system.

Installation

Install with pip:

$ pip install twlogger

Or add twlogger to the list of required packages in the setup.py file.

Configuration

You can use TWLogger as a WSGI middleware, as a logging handler or directly via Client object. For each configuration type you need to pass API URL or you can set it as TWLOGGER_URL environment variable. The URL should look like this:

https://taskworkshop.com/exceptions/log/API_KEY

As a WSGI Middleware

>>> from twlogger.middleware import TaskWorkshopMiddleware
>>> error_catching_wsgi_app = TaskWorkshopMiddleware(wsgi_app)

In a PasteDeploy pipeline

[filter:twlogger]
use = egg:TWLogger#twlogger
url = https://taskworkshop.com/exceptions/log/YOUR_TOKEN

[pipeline:main]
pipeline =
    twlogger
    your-app

[app:your-app]
...

As a logging handler

>>> import logging
>>> from twlogger.handler import TWLoggerHandler
>>> handler = TWLoggerHandler()
>>> logger = logging.getLogger(__name__)
>>> logger.addHandler(handler)

Using logging.config.dictConfig:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,

    'formatters': {
        'console': {
            'format': ('%(asctime)s %(levelname)s '
                       '[%(name)s][%(threadName)s] %(message)s'),
        },
    },

    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'formatter': 'console'
        },
        'twlogger': {
            'class': 'twlogger.handler.TWLoggerHandler',
            'url': 'https://taskworkshop.com/exceptions/log/YOUR_TOKEN',
            'level': 'ERROR',
        },
    },

    'loggers': {
        'root': {
            'handlers': ['console', 'twlogger'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'twlogger': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'twlogger.error': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}

Using logging.config.fileConfig:

[loggers]
keys = root, twlogger, twlogger_error

[handlers]
keys = console, twlogger

[formatters]
keys = console

[formatter_console]
format = %(asctime)s %(levelname)s [%(name)s][%(threadName)s] %(message)s

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = DEBUG
formatter = console

[handler_twlogger]
class = twlogger.handler.TWLoggerHandler
args = ('https://taskworkshop.com/exceptions/log/YOUR_TOKEN', )
level = ERROR
formatter = console

[logger_root]
handlers = console, twlogger
level = DEBUG

[logger_twlogger]
level = DEBUG
handlers = console
qualname = twlogger
propagate = 0

[logger_twlogger_error]
level = DEBUG
handlers = console
qualname = twlogger.error
propagate = 0

Manual usage

Logger: handling exceptions

Exception info and extra data will be submitted with the event.

>>> import logging
>>> logger = logging.getLogger(__name__)
>>> try:
...     x / y
... except ZeroDivisionError:
...     logger.exception('Division by zero!', extra={'x': x, 'y': y})

Logger: sending events

You can submit events of any level:

>>> import logging
>>> logger = logging.getLogger(__name__)
>>> logger.error('Some error')
>>> logger.warn('Some warning.')
>>> logger.info('Some info.', extra={'foo': 'bar'})

Client: handling exceptions

>>> from twlogger.client import Client
>>> client = Client()
>>> try:
...     1 / 0
... except ZeroDivisionError:
...     client.capture_exception()
>>> try:
...     x / y
... except ZeroDivisionError:
...     client.capture_exception(x=x, y=y)

Client: sending events

>>> from twlogger.client import Client
>>> client = Client()
>>> client.log_event('info', 'Some info log',
...                  place='File: "/foo/bar.py", line: 71, in foo_bar')

Issues and questions

Have a bug? Please create an issue on GitHub!

https://github.com/TaskWorkshop/taskworkshop-logger-python/issues

Contributing

TWLogger is an open source software and your contribution is very much appreciated.

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.

  2. Fork the repository on Github and make your changes.

  3. Write tests for your changes and follow these rules: PEP8, PEP257 and The Zen of Python.

  4. Make sure to add yourself to AUTHORS and send a pull request.

Use pytest-cov, pytest-pep8 and pytest-flakes PyTest extensions when running your tests:

$ pip install pytest pytest-cov pytest-pep8 pytest-flakes
$ py.test --cov twlogger --cov-report term-missing --pep8 --flakes

Licence

TWLogger is available under the GPL version 2 license. See LICENSE file. License is available at http://www.gnu.org/licenses/gpl-2.0.txt

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

TWLogger-0.1.0.tar.gz (16.5 kB view details)

Uploaded Source

File details

Details for the file TWLogger-0.1.0.tar.gz.

File metadata

  • Download URL: TWLogger-0.1.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for TWLogger-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e1f376640ec22c94be320c666ee27cf27ef2cc3b3c2501446505db334d660f0
MD5 0fd0482c4d4eb152d49d54168420b9a0
BLAKE2b-256 6e7c7cc714933fa26579025177869680c4b5d846753fbecdf23d19ee73f90b49

See more details on using hashes here.

Supported by

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