用于向logsense发送日志的python日志处理程序
logsense-logger的Python项目详细描述
logsense记录器python
提供一组用于工作的包装类和方法 使用python和LogSense
用法
安装
pip3 install logsense-logger
包装日志
from logsense.handler import LogSenseHandler
from os import getenv
import logging
logsense_token = getenv('LOGSENSE_TOKEN', None)
log = logging.getLogger()
log.addHandler(LogSenseHandler(logsense_token))
log.setLevel(logging.INFO)
class Example:
def __init__(self):
self.bar = 42
def foo(self):
logging.info("Answer: {}".format(self.bar))
if __name__ == "__main__":
ex = Example()
ex.foo()
现在,只需运行应用程序,例如
LOGSENSE_TOKEN="63da4903-01e9-d1a4-82a8-9cf8cd63b7b5" python sample_logging.py
日志应该流向logsense
指标
该包还包括一个关于如何测量方法持续时间的简单示例
并有能力跟踪指标。所有需要做的就是提供
LOGSENSE_TOKEN
环境变量和than注释
要用@measure_duration()
度量的方法。例如:
from logsense.metrics import measure_duration, setup_metrics
from os import getenv
logsense_token = getenv('LOGSENSE_TOKEN')
setup_metrics('myapp', logsense_token)
class MyComplexProcess:
def __init__(self, how_many_times):
self._how_many_times = how_many_times
@measure_duration(extracted_params=['a'])
def foo(self, a):
x = 0
for i in range(a*self._how_many_times):
x = x+1
return x
示例
查看example以获取实际使用示例