Python Client

Install

You can install the python client with pip install amonpy or easy_install amonpy

Configuration

import amonpy
amonpy.config.address = 'http://amon_instance:port'

By default amonpy uses HTTP as a protocol for transfering logs and exceptions to the Amon API

If you want faster, scalable, async logging you can easily switch to zeromq.

  • Install zeromq following this guide,
  • Install the python zeromq client.
  • Start the ZeroMQ daemon with sudo /etc/init.d/amonmq start
  • In the amonpy configuration set the protocol to zeromq:
amonpy.config.protocol = 'zeromq'

Usage

Logging

You can use the logging module in any Python application.

import amonpy
# message - string, list, dictionary
# tags - string or list
amonpy.log(message, tags)

# Will still work and in the web interface you will see these logs with tag 'unset'
amonpy.log(message)

# Log dictionaries - useful for post data, user events, benchmarks, etc.
amonpy.log({"first_name": "John", "last_name": "Dev", "age": 29}, 'info')

# And you can log lists as well
# You can have both strings and integers in the same list
amonpy.log(['data','more data',3], 'info')


# And last but not least - tagged logging
amonpy.log(message, ['info', 'debug'])

Exception handling

At the moment the exception handling module is working with Django. Support for Flask is in the works. In the meantime you can easily add support for your framework of choice, following the guide below:

class MyFrameworkException(Exception):
        #capture exception data, and add all the information in the following dictionary:  
        
        def process_exception(self, exc):
        
                data = {
                        'exception_class': '',
                        'url': '',
                        'backtrace': ['exception line ', 'another exception line'],
                        'enviroment': '',
                        
                        # In 'data' you can add request information, session variables - it's a recursive 
                        # dictionary, so you can literally add everything important for your specific case
                        # The dictionary doesn't have a specified structure, the keys below are only example
                        'data': {'request': '', 'session': '', 'more': ''}

                }

        amonpy.exception(data)

For a real world working example you can check the source code for the Django exception handler https://github.com/martinrusev/amonpy/blob/master/amonpy/adapters/django_.py

Django

You can capture the exceptions triggered by your Django application by adding the amonpy middleware in your settings.py file.

MIDDLEWARE_CLASSES = (        
        .....
        'amonpy.DjangoExceptionMiddleware'
        )

Bugs

If you find a bug in the python client, you can use the the github issues page to report it: https://github.com/martinrusev/amonpy/issues

Meta

The Python client is hosted on Github