推荐学习书目
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 · Dec 23, 2015 · 6429 views
    This topic created in 3805 days ago, the information mentioned may be changed or developed.
    楼主有 24 台服务器,上面的某个程序会使用 Python logging 来写 Log ,使用 TimedRotatingFileHandler 来让 Log 在每天午夜自动添加时间后缀。 Log 存放在一台服务器上,所有的其他服务器都来写这一个 Log 文件。

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

    请教各位,如何才能解决这个问题。
    8 replies    2015-12-25 21:27:22 +08:00
    tolbkni
        1
    tolbkni  
       Dec 23, 2015
    可以用 RemoteSyslogHandler 发送到 syslog 后端,用 logrotate 来切分日志
    knightdf
        2
    knightdf  
       Dec 23, 2015
    多进程下 python 的 RotatingFileHandler 有 BUG 的,自己重写下 doRollover 函数就好了,我都是自己改写的。
    class MultiProcessTimedRotatingFileHandler(TimedRotatingFileHandler)
    mouer
        3
    mouer  
       Dec 24, 2015
    启动一个记录 log 的 python 程序,然后用 sockerHandler 写到这个程序上,这个程序再用 TimedRotatingFileHandler
    mulog
        4
    mulog  
       Dec 24, 2015
    我是另起了一个进程 如 @mouer 所说用 SocketHandler
    billion
        5
    billion  
    OP
       Dec 24, 2015
    @mouer 这确实是一个好的办法。但是由于网络存在不可信的因素,所以这样做的话,可能会漏掉 log.
    Jordan
        6
    Jordan  
       Dec 24, 2015
    @tolbkni 正解
    billion
        7
    billion  
    OP
       Dec 24, 2015
    @tolbkni 似乎只种方式只能在 Unix 下面使用, Windows 下面就不行了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1278 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 17:14 · PVG 01:14 · LAX 10:14 · JFK 13:14
    ♥ Do have faith in what you're doing.