mirror of
https://github.com/gotoeasy/glogcenter.git
synced 2025-12-24 12:15:31 +08:00
新增python客户端支持
This commit is contained in:
16
README.md
16
README.md
@@ -199,7 +199,7 @@ curl -X POST -d '{"system":"demo", "date":"2023-01-01 01:02:03.456","text":"demo
|
||||
## 使用`golang`语言的项目,提供工具包,开箱即用
|
||||
```shell
|
||||
# 方式1)通过环境变量自动配置,程序直接使用cmn.Debug(...)写日志即可
|
||||
export GLC_ENABLE=true # 此配置默认false,要生效必须配置为true
|
||||
export GLC_ENABLE=true # 此配置默认false,要发送日志中心必须配置为true
|
||||
export GLC_API_URL='http://127.0.0.1:8080/glc/v1/log/add'
|
||||
export GLC_API_KEY='X-GLC-AUTH:glogcenter'
|
||||
export GLC_SYSTEM=demo
|
||||
@@ -229,12 +229,14 @@ func main() {
|
||||
|
||||
## 使用`python`语言的项目,提供工具包,开箱即用
|
||||
```shell
|
||||
# 暂时以下环境变量简化支持
|
||||
# GLC_API_URL配置时控制台不打印,否则打印
|
||||
export GLC_API_URL='http://127.0.0.1:8080/glc/v1/log/add'
|
||||
export GLC_API_KEY='X-GLC-AUTH:glogcenter'
|
||||
export GLC_SYSTEM=demo
|
||||
export GLC_TRACE_ID=12345
|
||||
# 支持以下环境变量配置
|
||||
export GLC_ENABLE=true # 默认false,要发送日志中心必须配置为true
|
||||
export GLC_ENABLE_CONSOLE_LOG=true # 默认true,控制台不打印时配置为false
|
||||
export GLC_API_URL='http://127.0.0.1:8080/glc/v1/log/add' # 未配置时将取消发送
|
||||
export GLC_API_KEY='X-GLC-AUTH:glogcenter' # 这是默认值,按需修改
|
||||
export GLC_SYSTEM=default # 默认default,按需修改
|
||||
export GLC_LOG_LEVEL=debug # 日志级别(debug/info/warn/error),默认debug
|
||||
export GLC_TRACE_ID=12345 # 默认空
|
||||
```
|
||||
|
||||
```python
|
||||
|
||||
@@ -1 +1,29 @@
|
||||
日志中心的 python 客户端
|
||||
|
||||
|
||||
## 使用`python`语言的项目,提供工具包,开箱即用
|
||||
```shell
|
||||
# 支持以下环境变量配置
|
||||
export GLC_ENABLE=true # 默认false,要发送日志中心必须配置为true
|
||||
export GLC_ENABLE_CONSOLE_LOG=true # 默认true,控制台不打印时配置为false
|
||||
export GLC_API_URL='http://127.0.0.1:8080/glc/v1/log/add' # 未配置时将取消发送
|
||||
export GLC_API_KEY='X-GLC-AUTH:glogcenter' # 这是默认值,按需修改
|
||||
export GLC_SYSTEM=default # 默认default,按需修改
|
||||
export GLC_LOG_LEVEL=debug # 日志级别(debug/info/warn/error),默认debug
|
||||
export GLC_TRACE_ID=12345 # 默认空
|
||||
```
|
||||
|
||||
```python
|
||||
# 安装
|
||||
pip install glogcenter
|
||||
|
||||
# 使用
|
||||
from glogcenter import glc
|
||||
|
||||
glc.debug("这是Debug级别日志")
|
||||
glc.info("这是Info级别日志", "多个参数", "会被拼接")
|
||||
gd = glc.GlcData()
|
||||
gd.user = 'abcd'
|
||||
glc.warn("这里的GlcData类型参数都不会打印", "gd只起传值作用", gd)
|
||||
glc.error("gd参数顺序无关", gd, "用法如同log库,但对GlcData做了特殊的判断处理")
|
||||
```
|
||||
|
||||
@@ -69,13 +69,11 @@ class GlcData:
|
||||
|
||||
|
||||
def post_glc_data(glc_data, logLevel):
|
||||
if not glc_data:
|
||||
if os.getenv('GLC_ENABLE') != 'true':
|
||||
return
|
||||
|
||||
url = os.getenv('GLC_API_URL')
|
||||
if url is None:
|
||||
# 不发日志中心就打印日志,否则不打印
|
||||
print(get_current_time(), logLevel, glc_data.text)
|
||||
if not url:
|
||||
return
|
||||
|
||||
data = {
|
||||
@@ -93,7 +91,7 @@ def post_glc_data(glc_data, logLevel):
|
||||
|
||||
headers = {'Content-Type': 'application/json', 'X-GLC-AUTH': 'glogcenter'}
|
||||
glc_api_key = os.getenv('GLC_API_KEY') # X-GLC-AUTH:glogcenter
|
||||
if glc_api_key and ':' in glc_api_key:
|
||||
if glc_api_key and ':' in glc_api_key:
|
||||
key_parts = glc_api_key.split(':')
|
||||
headers[key_parts[0]] = key_parts[1]
|
||||
|
||||
@@ -135,15 +133,59 @@ def argsToGlcData(*args):
|
||||
|
||||
return glc_data
|
||||
|
||||
def get_log_level():
|
||||
level = os.getenv('GLC_LOG_LEVEL')
|
||||
if level is None:
|
||||
return 0
|
||||
|
||||
level = level.lower()
|
||||
|
||||
if 'debug' in level:
|
||||
return 0
|
||||
if 'info' in level:
|
||||
return 1
|
||||
if 'warn' in level:
|
||||
return 2
|
||||
if 'error' in level:
|
||||
return 3
|
||||
|
||||
|
||||
def debug(*args):
|
||||
post_glc_data(argsToGlcData(*args), 'DEBUG')
|
||||
if get_log_level() <= 0:
|
||||
glc_data = argsToGlcData(*args)
|
||||
if not glc_data:
|
||||
return
|
||||
|
||||
if os.getenv('GLC_ENABLE_CONSOLE_LOG') != 'false':
|
||||
print(get_current_time(), '[DEBUG]', glc_data.text)
|
||||
post_glc_data(glc_data, 'DEBUG')
|
||||
|
||||
def info(*args):
|
||||
post_glc_data(argsToGlcData(*args), 'INFO')
|
||||
if get_log_level() <= 1:
|
||||
glc_data = argsToGlcData(*args)
|
||||
if not glc_data:
|
||||
return
|
||||
|
||||
if os.getenv('GLC_ENABLE_CONSOLE_LOG') != 'false':
|
||||
print(get_current_time(), ' [INFO]', glc_data.text)
|
||||
post_glc_data(glc_data, 'INFO')
|
||||
|
||||
def warn(*args):
|
||||
post_glc_data(argsToGlcData(*args), 'WARN')
|
||||
if get_log_level() <= 2:
|
||||
glc_data = argsToGlcData(*args)
|
||||
if not glc_data:
|
||||
return
|
||||
|
||||
if os.getenv('GLC_ENABLE_CONSOLE_LOG') != 'false':
|
||||
print(get_current_time(), ' [WARN]', glc_data.text)
|
||||
post_glc_data(glc_data, 'WARN')
|
||||
|
||||
def error(*args):
|
||||
post_glc_data(argsToGlcData(*args), 'ERROR')
|
||||
if get_log_level() <= 3:
|
||||
glc_data = argsToGlcData(*args)
|
||||
if not glc_data:
|
||||
return
|
||||
|
||||
if os.getenv('GLC_ENABLE_CONSOLE_LOG') != 'false':
|
||||
print(get_current_time(), '[ERROR]', glc_data.text)
|
||||
post_glc_data(glc_data, 'ERROR')
|
||||
|
||||
@@ -18,7 +18,7 @@ URL = 'https://github.com/gotoeasy/glogcenter'
|
||||
EMAIL = 'gotoeasy@163.com'
|
||||
AUTHOR = 'gotoeasy'
|
||||
REQUIRES_PYTHON = '>=3.6.0'
|
||||
VERSION = '0.1.10'
|
||||
VERSION = '0.3.0'
|
||||
|
||||
# What packages are required for this module to be executed?
|
||||
REQUIRED = [
|
||||
|
||||
Reference in New Issue
Block a user