Skip to content

Commit

Permalink
fix:修复在python<=3.8环境中的类型注解解析问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
xingweidong committed May 30, 2023
1 parent 687f76e commit e72ecc3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
logging拓展功能模块。

[Installation](#installation) | [Usage](#usage)
---------------------------------------------
***

## Installation

Expand All @@ -28,10 +28,54 @@ loggingx在使用上基本和logging一致。
将日志消息发布到redis stream消息队列,配合[handlers.RedisStreamListener](#handlersredisstreamlistener)可以解决多进程写日志文件不安全的问题。

``` python
# 示例一:直接使用

logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)
rlh = RedisStreamHandler(redis_url)
logger.addHandler(rlh)
rsh = RedisStreamHandler('redis://redis:6379/0')
logger.addHandler(rsh)

# 示例二:在配置文件中使用
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(asctime)s.%(msecs)03d %(levelname)-8s %(processName)s:%(threadName)s %(pathname)s:%(lineno)d - %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'simple': {
'format': '{name:s}: {asctime:s} {levelname} {message}',
'style': '{',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'simple'
},
'redis_stream': {
'class': 'loggingx.handlers.RedisStreamHandler',
'level': 'INFO',
'redis_url': 'redis://redis:6379/0'
}
},
'loggers': {
'cloud': {
'handlers': ['console'],
'propagate': True,
},
'cloud.request': {
'handlers': ['redis_stream'],
'level': 'INFO',
'propagate': True,
}
},
}

logging.config.dictConfig(LOGGING_CONFIG)
```

在多进程服务中使用RedisStreamHandler,然后单独启动一个进程运行RedisStreamListener,可以保证日志消息完整传递到下游处理器。
Expand All @@ -51,6 +95,6 @@ fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
# 使用RedisStreamListener消费RedisStreamHandler消息,传递到TimedRotatingFileHandler
rsl = RedisStreamListener(redis_url, fh)
rsl = RedisStreamListener('redis://redis:6379/0', fh)
rsl.start()
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "loggingx"
version = "1.0.0"
version = "1.0.1"
authors = [
{ name="xingweidong", email="614968414@qq.com" },
]
Expand Down
23 changes: 11 additions & 12 deletions src/loggingx/handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import redis
from typing import Union
from logging import LogRecord, makeLogRecord
from logging import makeLogRecord
from logging.handlers import QueueHandler, QueueListener


Expand All @@ -14,8 +13,8 @@ class RedisStreamHandler(QueueHandler):

def __init__(
self,
redis_url: str,
redis_stream_key: str = DEFAULT_REDIS_STREAM_KEY,
redis_url,
redis_stream_key = DEFAULT_REDIS_STREAM_KEY,
**redis_kwargs
):
"""
Expand All @@ -33,7 +32,7 @@ def __init__(

super().__init__(r)

def enqueue(self, record: LogRecord):
def enqueue(self, record):
"""
Override QueueHandler.enqueue(self, record)方法。
发布日志消息到redis stream消息队列。
Expand All @@ -53,10 +52,10 @@ class RedisStreamListener(QueueListener):

def __init__(
self,
redis_url: str,
redis_url,
*handlers,
respect_handler_level: bool = False,
redis_stream_key: str = DEFAULT_REDIS_STREAM_KEY,
respect_handler_level = False,
redis_stream_key = DEFAULT_REDIS_STREAM_KEY,
**redis_kwargs
):
"""
Expand Down Expand Up @@ -85,7 +84,7 @@ def __init__(

super().__init__(r, *handlers, respect_handler_level=respect_handler_level)

def dequeue(self, block) -> Union[dict[bytes, bytes], None]:
def dequeue(self, block):
"""
Override QueueHandler.dequeue(self, block)方法。
消费redis stream消息队列的日志消息。
Expand Down Expand Up @@ -116,7 +115,7 @@ def dequeue(self, block) -> Union[dict[bytes, bytes], None]:
self.queue.xdel(self.redis_stream_key, id)
return record

def redis_encode(self, value: str) -> bytes:
def redis_encode(self, value):
"""
使用redis连接实例的编码参数进行编码。
Expand All @@ -129,7 +128,7 @@ def redis_encode(self, value: str) -> bytes:

return value.encode(self.redis_encoding, self.redis_encoding_errors)

def redis_decode(self, value: bytes) -> str:
def redis_decode(self, value):
"""
使用redis连接实例的编码参数进行解码。
Expand All @@ -142,7 +141,7 @@ def redis_decode(self, value: bytes) -> str:

return value.decode(self.redis_encoding, self.redis_encoding_errors)

def prepare(self, record: dict[bytes, bytes]) -> LogRecord:
def prepare(self, record):
"""
Override QueueHandler.prepare(self, record)方法。
准备日志消息,将从redis stream消息队列消费的消息转换成LogRecord实例。
Expand Down

0 comments on commit e72ecc3

Please sign in to comment.