Logging to Loggly with Python

Before we get into the nitty gritty it's important to know the differences between the two libraries below. The first loggly python library (loggly 0.1.2) is strictly involved in getting information from devices and inputs via the Loggly admin API, not specifically logging from python. Hoover (hoover 0.5.3) handles both devices and inputs as well as logging directly from your python code.

If you prefer to use helper libraries, you can use loggly 0.1.2 or hoover 0.5.3, both of which can be installed via the Python Cheese Shop:

  easy_install hoover

or

  easy_install loggly

The documentation for Hoover can be found here at Using Hoover. Documentation for the loggly library is over on Github: http://github.com/EA2D/loggly-python.

(Add a -U to easy_install for the most up to date version of the library):

  easy_install -U hoover

Via Syslog

Syslog based logging out of python can be done with the python-syslog or python-logging modules. You'll need to configure the box that is running the code to forward logs to us. If you haven't done so, see the Logging Configuration page for more information on setting up syslog forwarding to forward to Loggly.

Assuming you've set up syslog to forward correctly, when you log out of Python to your local syslog server, it takes the events and then forwards them on to Loggly.

Here's a simple example of logging to syslog within Python:

import syslog
syslog.syslog('hello world')

In practice, you'll want to be more verbose in your logging. Loggly suggests including key-value pairs so you can query those values faster later. Here's an example which uses the python-logging module:

Via HTTP

Loggly supports sending in data over HTTP POSTs. To create an HTTP input, login to your account, then navigate to the inputs tab. Click on the add input button at the bottom of the page and then provide an input name and description for the input:

Once you create an HTTP input, you'll be taken to the input detail page, which will contain the URL you can use to send data to that particular input. If you need to rotate the SHA-2 key associated to a particular input, you can click on the generate new URL button at the bottom of the input detail page.

Here's an example which uses the httplib2 library to POST to your HTTP input:

import httplib2
insert_url = "http://logs.loggly.com/inputs/c2e53b89-5283-4b5e-b93e-01fc2b0fe060"
insert_http = httplib2.Http(timeout=10)
body = "Beavers love logs."
resp, content = insert_http.request(insert_url, "POST", body=body, headers={'content-type':'text/plain'})

Logging from AppEngine

You can log directly from your AppEngine Python code using Loggly's custom logging module handler and a Loggly HTTP input. Loggly's logging module for AppEngine uses non-blocking asynchronous HTTP connections to POST your log lines directly to Loggly. It is possible to use the hoover python library in your AppEngine application. Just copy the hoover and httplib2 folders to your AppEngine project directory. Note that logging with *.config_inputs() is not possible since the python logging in hoover is done via Syslog (off limits in AppEngine).

Using the Module

Start by downloading the module, unzipping it, and placing it your AppEngine project's directory.

Import both the logging and Loggly modules by sticking this line at the top of your code:

import logging, loggly

In your main function, create a logging object and give it one of your input URLs and a minimum logging level:

hoover = loggly.LogglyLogger('http://logs.loggly.com/inputs/d83ef00b-5214-4106-9d32-6956201c29ce', logging.INFO)

Note: Be sure you change the input endpoint URL in this example!

Finally, log a line by calling the logging module:

logging.info('Reality is merely an illusion, albeit a very persistent one.')

The result will end up looking something like this when you do a search from Loggly:

source=foobarapp-1.3473122530533495572010-12-31 23:43:26,209 level=INFO, msg="Reality is merely an illusion, albeit a very persistent one.", module=main, file="main.py", lineno=31

For more information you can check out Python's logging handler or read up on Google's docs for conducting asynchronous HTTP requests from AppEngine.

Logging to Loggly with rsyslog and Boto

Boto is a python library that allows your python to interact with AWS services. Chris Moyer's blog has an awesome implementation sending logs from Boto to Loggly check out Chris Moyer: Boto, Loggly, and rsyslog.

Using a Python syslog Proxy

Evax Software has written a Python proxy server that acts as a local syslog service. Logs sent to the proxy are rewritten to the Loggly HTTP input handler.

You can install the package by using the Cheese Shop:

easy_install logglyproxy

The package documentation and downloads can be found here. There's also a page on their site with a quick example here.

Navigation
Print/export
Toolbox