azp*_*p74 5 python windows log-rotation
(我已经搜索过,但没有找到这个问题的副本,但很高兴得到证明).
我需要从一些Python代码中旋转日志.代码在Windows上运行(Server 2008 R2).
最初我使用TimedRotatingFileHandler(来自Python的logging.handlers包),但这不能正常工作,因为我所理解的是它与多处理有关的问题(subprocess.check_call用于启动另一个应用程序).
我已经检查了ConcurrentLogHandler,看起来它可能会起作用,但我有点担心自2013年以来它没有更新(虽然最近提出了问题).
更新:一个开放的bug(自2013年起)表明ConcurrentLogHandler不能与Python 2.7/Windows一起使用.在记录时,代码就会挂起.
我应该使用最佳实践Windows解决方案吗?
也许我遗漏了一些东西,但是Python的日志记录模块附带了一个RotatingFileHandler:
import logging
import time
from logging.handlers import RotatingFileHandler
#----------------------------------------------------------------------
def create_rotating_log(path):
"""
Creates a rotating log
"""
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
# add a rotating handler
handler = RotatingFileHandler(path, maxBytes=20,
backupCount=5)
logger.addHandler(handler)
for i in range(10):
logger.info("This is test log line %s" % i)
time.sleep(1.5)
#----------------------------------------------------------------------
if __name__ == "__main__":
log_file = r"c:\path\to\test.log"
create_rotating_log(log_file)
Run Code Online (Sandbox Code Playgroud)
这对于我在 Windows 7 上使用 Python 2.7 来说效果很好。以下是一些更详细的链接:
| 归档时间: |
|
| 查看次数: |
1355 次 |
| 最近记录: |