簡體   English   中英

使用dictconfig進行Python Loggly日志記錄設置

[英]Python Loggly logging setup using dictconfig

我想在我的新PaaS python應用程序中嘗試Loggly,因為我通常使用標准的旋轉文件處理程序對Linux服務器進行編碼。 這提出了一個問題,因為他們的文檔配置僅涵蓋使用.conf文件,並且因為他們在首選的HTTPS處理程序中有一些自定義方法,所以有任何其他方式配置它的技巧。

我一直在使用嵌套的.py文件來處理我的應用程序的其余部分的配置,並且不想更改,看起來dictconfig方法在文檔中是首選。

那么,如何解決這個問題呢?

此方法使用標准python日志記錄模塊的dictconfig方法,並在2.7.9上進行了測試

安裝'loggly-python-handler'模塊。

在您的代碼中,您可以執行以下操作:'conf'是您的.py配置文件:

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

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

然后,在'conf.py'文件中,您可以使用以下字典(替換您的密鑰):

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']
        }
    }
}

這是使用日志記錄模塊文檔中的dictconfig的說明組合: https ://docs.python.org/2/library/logging.config.html

並挖掘'loggly-python-handler'模塊的代碼,看看它的構造函數是如何工作的: https//github.com/psquickitjayant/loggly-python-handler/blob/master/loggly/handlers.py

你也可以直接從控制台測試它(不要忘記添加你的密鑰):

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM