log.client
一款Python日志客户端,支持直接写入Elasticsearch。
v1.2.6:
修复
1、修复 ES bulk 写入 read timeout 导致日志丢失的问题:原代码异常捕获使用了 requests.exceptions.*,但 elasticsearch-py 7.13 走 urllib3 实际抛 elasticsearch.exceptions.ConnectionTimeout,导致连接异常分支永不命中,日志重试队列死循环。
2、修复连接异常时重建了错误对象(requests.Session)而非真正被 bulk 使用的 self.es 客户端,导致坏连接池被持续复用。
优化
1、ES 客户端增加超时与重试配置:timeout=30(原默认 10s)、retry_on_timeout=True、max_retries=3,应对瞬时网络抖动时自动换连接重试。
2、helpers.bulk() 增加 request_timeout=30,防止单次 bulk 请求超时。
3、新增 _build_es() 方法统一构造 ES 客户端,连接异常时重建 self.es 丢弃陈旧连接池。
v1.2.2:
新功能
1、支持通过环境变设置日志组件配置
v1.2.1:
新功能
1、支持ElasticSearch最小安全性设置。
v1.1.1:
功能优化:
1、支持批量写入Elasticsearch(依赖Elasticsearch包)。
2、增加日志队列慢的异常处理。
Usage:
1、创建配置文件config.ini:
[log]
app_name = YOUR-APPNAME
es_urls = http://es-ip:9200
es_index_name = applog-20221228
# 1.2.1版本新增配置
es_username = elastic
es_password = search
app_name: 应用名称
es_urls: Elasticsearch 地址。填写多个时,用,分隔。
es_index_name: Elasticsearch 索引名称
1.2.2 支持同名的环境变量替换config.ini.
优先使用环境变量的设置.
linux:
export app_name=testapp
export es_urls=http://es-ip:9200
2、创建配置文件logging.ini:
[loggers]
keys=root
[handlers]
keys=consoleHandler,qHandler
[formatters]
keys=simpleFmt,detailFmt
[logger_root]
level=DEBUG
handlers=consoleHandler,qHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=detailFmt
args=(sys.stdout,)
[handler_qHandler]
class=qlog.logger.QlogHandler
level=INFO
formatter=simpleFmt
args=()
[formatter_detailFmt]
format="%(asctime)-15s %(levelname)s %(filename)s %(funcName)s lineNo: %(lineno)d details: %(message)s"
[formatter_simpleFmt]
format="%(message)s"
3、日志写入测试
import logging
import logging.config
from time import sleep
logging.config.fileConfig('logging.ini')
logger = logging.getLogger('root')
logger.info('这是一条中文字符测试日志')
logger.info('this is a info log.')
logger.warning('this is a warning log.')
sleep(30)
4、查看Elasticsearch日志
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file log_client-1.2.6.tar.gz.
File metadata
- Download URL: log_client-1.2.6.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80b6c6aca2d7ceb51eedce11999805b0c5c4c105209fbf1eb364ae7bdce8fac2
|
|
| MD5 |
4c5b829d256481ceafb07889f3184499
|
|
| BLAKE2b-256 |
ad7b06c4e50b42d5c98c0643edab69ec501c974e05f03393246f2a9d98df18b6
|
File details
Details for the file log_client-1.2.6-py3-none-any.whl.
File metadata
- Download URL: log_client-1.2.6-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c7200a5e07df51e12a88e5ff7af8d0431a835e56021bf4923b7b665d1713b39
|
|
| MD5 |
b5969351b79e21c5074b1d0c1debf9a5
|
|
| BLAKE2b-256 |
dc5c38b8ef47f4ca506fdac522bcc43cf518c083378b439e068aad80b901b97d
|