V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
simple2025
0D
V2EX  ›  Python

请问一下各位,在 web 框架里面 logging 是怎么写的呢

  •  
  •   simple2025 · Jun 10, 2017 · 2371 views
    This topic created in 3251 days ago, the information mentioned may be changed or developed.
    比如
    有一个接口,我会记录用户的请求数据 dataA,比如 querystring 再加其他一些数据,
    但是我又不想把 dataA 直接用 print
    输出到 sys.stdout 里面去,因为这样 dataA 并不好找,所以我就用了 FileRotateHandler,并设置文件最大为 50M,
    然而这样会产生一个问题就是,当我用 supervisor 启动多个 web 进程的时候,我发现有许多日志文件大为在 5M 
    的时候就被切割出来了。如果要解决这种问题是不是只有使用队列这一种方式呢
    
    
    6 replies    2017-06-10 23:43:06 +08:00
    0asis
        1
    0asis  
       Jun 10, 2017
    一般框架都会自带 log 模块吧
    simple2025
        2
    simple2025  
    OP
       Jun 10, 2017
    @0asis log 模块无法处理由于多个 supervisor 进程 导致的多进程问题吧,除非让 supervisor 来处理
    Fishdrowned
        3
    Fishdrowned  
       Jun 10, 2017 via Android
    不懂 python。你可以 log 到 rsyslog,然后用 logrotate 分割 log 文件
    SErHo
        4
    SErHo  
       Jun 10, 2017   ❤️ 1
    我一般采用下面两种方法:

    1. 日志直接输出到 stdout,如直接 print 或者 logging.StreamHandler,然后配置 supervisor 的 stdout_logfile 来将标准输出记录到文件

    2. FileRotateHandler 不是进程安全的,多个进程使用会在切文件的时候产生竞争,造成文件切的大小不准确,可以实现一个多进程安全的 RotatingFileHandler

    https://gist.github.com/SerhoLiu/a3d7be43df882af80ef98bc375fc6046.js
    simple2025
        5
    simple2025  
    OP
       Jun 10, 2017
    @SErHo 就是不能输出到 stdout 里面呀,因为这个日志要单独保存。。方便日后查看
    Ge4Los
        6
    Ge4Los  
       Jun 10, 2017   ❤️ 1
    FileRotateHandler 不是进程安全的。
    用 WatchedFileHandler 来写日志,多个进程可以写同一个日志文件。
    有切日志的需求,用 logrotate 来切。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5472 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 05:58 · PVG 13:58 · LAX 22:58 · JFK 01:58
    ♥ Do have faith in what you're doing.