V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
billion
V2EX  ›  Python

多进程操作如何正确使用 Python logging TimedRotatingFileHandler

  •  
  •   billion ·
    kingname · 2015-12-23 21:50:01 +08:00 · 5415 次点击
    这是一个创建于 3048 天前的主题,其中的信息可能已经有所发展或是发生改变。
    楼主有 24 台服务器,上面的某个程序会使用 Python logging 来写 Log ,使用 TimedRotatingFileHandler 来让 Log 在每天午夜自动添加时间后缀。 Log 存放在一台服务器上,所有的其他服务器都来写这一个 Log 文件。

    这样就会导致在午夜更新文件名的时候,出现 Windows Error 32 , 因为这个文件还在被其他 logging 占用,所以无法正常修改文件名。

    请教各位,如何才能解决这个问题。
    8 条回复    2015-12-25 21:27:22 +08:00
    tolbkni
        1
    tolbkni  
       2015-12-23 22:02:26 +08:00
    可以用 RemoteSyslogHandler 发送到 syslog 后端,用 logrotate 来切分日志
    knightdf
        2
    knightdf  
       2015-12-23 22:14:20 +08:00
    多进程下 python 的 RotatingFileHandler 有 BUG 的,自己重写下 doRollover 函数就好了,我都是自己改写的。
    class MultiProcessTimedRotatingFileHandler(TimedRotatingFileHandler)
    mouer
        3
    mouer  
       2015-12-24 10:02:31 +08:00
    启动一个记录 log 的 python 程序,然后用 sockerHandler 写到这个程序上,这个程序再用 TimedRotatingFileHandler
    mulog
        4
    mulog  
       2015-12-24 11:32:10 +08:00
    我是另起了一个进程 如 @mouer 所说用 SocketHandler
    billion
        5
    billion  
    OP
       2015-12-24 12:00:31 +08:00
    @mouer 这确实是一个好的办法。但是由于网络存在不可信的因素,所以这样做的话,可能会漏掉 log.
    Jordan
        6
    Jordan  
       2015-12-24 13:02:39 +08:00
    @tolbkni 正解
    billion
        7
    billion  
    OP
       2015-12-24 13:11:26 +08:00
    @tolbkni 似乎只种方式只能在 Unix 下面使用, Windows 下面就不行了。
    sujin190
        8
    sujin190  
       2015-12-25 21:27:22 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5493 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:54 · PVG 16:54 · LAX 01:54 · JFK 04:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.