logging拓展功能模块
Project description
loggingx
logging拓展功能模块。
Installation
安装loggingx,只需要执行:
pip install loggingx
loggingx部分功能需要依赖消息队列,需要安装相关依赖,可以通过以下命令安装必要的依赖:
pip install "loggingx[queue]"
Usage
loggingx在使用上基本和logging一致。
handlers.RedisStreamHandler
将日志消息发布到redis stream消息队列,配合handlers.RedisStreamListener可以解决多进程写日志文件不安全的问题。
# 示例一:直接使用
logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)
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,可以保证日志消息完整传递到下游处理器。
完整示例可以参考examples。
handlers.RedisStreamListener
消费redis stream消息队列的日志消息,传递到下游handlers。
RedisStreamListener不属于处理器,需要和handlers.RedisStreamHandler配合工作。
# 创建TimedRotatingFileHandler实例
fh = logging.handlers.TimedRotatingFileHandler('logs/cloud.log', when='M', backupCount=5)
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
# 使用RedisStreamListener消费RedisStreamHandler消息,传递到TimedRotatingFileHandler
rsl = RedisStreamListener('redis://redis:6379/0', fh)
rsl.start()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
loggingx-1.0.1.tar.gz
(5.0 kB
view details)
Built Distribution
File details
Details for the file loggingx-1.0.1.tar.gz
.
File metadata
- Download URL: loggingx-1.0.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9fae27453ff56bc14eb6693d73d1031ccbcca2460cbd86e23c9da54bb721be1 |
|
MD5 | 6e7707aae1a326d7f46bf1847c749f56 |
|
BLAKE2b-256 | 5b2b1ffaeb1bf893c3223f6eee4dc559de8640642ac461fb0728fd4b0c3f12b6 |
File details
Details for the file loggingx-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: loggingx-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 556bb97244c2a7381ff17ee372e72f64f315f86f512ebeae160eb9ee36c6cd14 |
|
MD5 | e018ca76a85777685b0c846f31bbd4c1 |
|
BLAKE2b-256 | 35ee338243050b79809fcd6322259b79db474cbb09f3d22904501299f845ba60 |