python UDP / TCP日志记录到logly的代理
项目描述
Py2Loggly 是一个代理服务器,在 UDP 和 TCP 上侦听来自 python 的 UDP/TCP 日志记录处理程序的消息,它将所有这些消息转发到loggly.com。
安装
安装依赖项
py2loggly 只依赖于gevent
安装 py2loggly
从来源
$ python setup.py install
来自皮皮
$ pip install py2loggly
用法
启动服务器
唯一需要的参数是--loggly-token来提供你的 loggly TOKEN。
$ py2loggly --loggly-token $LOGGLY_TOKEN
默认 TCP 端口为 9020,默认 UDP 端口为 9021。
参数
usage: py2loggly [-h] [-v] [--bind-ip IP] [--tcp-port PORT] [--udp-port PORT]
[--fqdn] [--hostname HOSTNAME] --loggly-token TOKEN
[--tags TAG1 TAG2 [TAG1 TAG2 ...]]
Proxy for python UDP/TCP logging to loggly
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
--bind-ip IP IP address to listen on
--tcp-port PORT TCP port to listen on
--udp-port PORT UDP port to listen on
--fqdn Use the fully qualified domain name
--hostname HOSTNAME Use the provided hostname instead of guessing it.
--loggly-token TOKEN the loggly TOKEN.
--tags TAG1 TAG2 [TAG1 TAG2 ...]
Tags for the event message
向 py2loggly 发送消息
您可以使用Python 的logging.handlers中的SockerHandler或DatagramHandler
使用DatagramHandler的最大优势是日志不会减慢您的应用程序,如果 py2loggly 没有运行,它不会影响您的应用程序。
所以推荐的设置是在每台服务器上本地运行 py2loggly 并使用DatadgramHandler。
例子
import logging, logging.handlers
rootLogger = logging.getLogger('')
rootLogger.setLevel(logging.DEBUG)
socketHandler = logging.handlers.SocketHandler('localhost',
logging.handlers.DEFAULT_TCP_LOGGING_PORT)
# don't bother with a formatter, since a socket handler sends the event as
# an unformatted pickle
rootLogger.addHandler(socketHandler)
# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')
执照
麻省理工学院