简体   繁体   中英

Python Loggly logging setup using dictconfig

I want to try Loggly in my new PaaS python app, as I usually code against a linux server and just use the standard rotating file handler. This presents an issue as their documented configuration only covers using a .conf file and as they have a few custom methods in their preferred HTTPS handler there's a couple of tricks to configuring it any other way.

I have been using nested .py files to handle the config for the rest of my application and didn't want to change, and it looked like the dictconfig method was preferred in the documentation.

So, how to resolve this?

This approach uses the dictconfig method of the standard python logging module, and was tested on 2.7.9

Install the 'loggly-python-handler' module.

In your code, you could do the following where 'conf' is your .py config file:

import logging, logging.config
import loggly.handlers
import conf

# Init Logging
logging.config.dictConfig(conf.LOGGING)
logger = logging.getLogger(u'root')

Then, in the 'conf.py' file, you could use the following dictionary (replace your key):

LOGGING = {
    u'version': 1,
    u'handlers': {
        u'loggly': {
            u'class': u'loggly.handlers.HTTPSHandler',
            u'formatter': u'basic',
            u'level': u'DEBUG',
            u'url': u'https://logs-01.loggly.com/inputs/YOUR_KEY_HERE/tag/pytest'
        },
        u'console': {
            u'class': u'logging.StreamHandler',
            u'level': u'DEBUG',
            u'formatter': u'basic',
            u'stream': u'ext://sys.stdout',
        }
    },
    u'formatters': {
        u'basic': {
            u'format': u'%(asctime)s | %(name)15s:%(lineno)3s:%(funcName)15s | %(levelname)7s | %(message)s'
        }
    },
    u'loggers': {
        u'root': {
            u'level': u'DEBUG',
            u'handlers': [u'console', u'loggly']
        }
    }
}

This is a combination of instructions for using dictconfig from the logging module documentation: https://docs.python.org/2/library/logging.config.html

And digging through the code of the 'loggly-python-handler' module to see how its constructor worked : https://github.com/psquickitjayant/loggly-python-handler/blob/master/loggly/handlers.py

You could also test this directly from the console with (don't forget to add your key):

import logging
import loggly.handlers
handler = loggly.handlers.HTTPSHandler(url='https://logs-01.loggly.com/inputs/YOUR_KEY_HERE/tag/python')
logger = logging.getLogger(u'root')
logger.addHandler(handler)
logger.warning("test loggly connection")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM