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
Zarten
V2EX  ›  Python

Python 使用什么日志系统?自己写吗

  •  
  •   Zarten · Jun 12, 2019 · 3192 views
    This topic created in 2512 days ago, the information mentioned may be changed or developed.
    大家代码是如何做日志记录的呢
    9 replies    2019-06-14 11:28:56 +08:00
    lowman
        1
    lowman  
       Jun 12, 2019
    python logging 模块了解一下, 完全满足所有打印 log 的各种需求, 当然了,需要自己根据需求配置一下
    jackleeforce3615
        2
    jackleeforce3615  
       Jun 12, 2019   ❤️ 1
    程序运行,执行 init_log("xxx.log") 即可,所有日志和 print 都会输出到指定文件

    ```python

    def init_log(filename):
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
    '%(asctime)s-%(name)s-%(message)s',
    datefmt='%Y-%m-%d %H:%M:%S')

    # 使用 FileHandler 输出到文件
    fh = logging.FileHandler(filename)
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)

    # 使用 StreamHandler 输出到屏幕
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)

    logger.addHandler(ch)
    logger.addHandler(fh)


    ```
    jackleeforce3615
        3
    jackleeforce3615  
       Jun 12, 2019
    哦,v2 评论不支持 markdown?
    locoz
        4
    locoz  
       Jun 12, 2019   ❤️ 2
    我这边爬虫的日志目前是用的 logging+jsonlogger 输出成每行一个 json 格式的日志,然后用 filebeat 传到 es,用 kibana 监控,可以参考一下
    https://i.loli.net/2019/06/12/5d007543b990f61298.png
    abmin521
        5
    abmin521  
       Jun 12, 2019 via Android
    @jackleeforce3615 可以用 gist 间接支持
    deepall
        6
    deepall  
       Jun 12, 2019
    @locoz 同样,把 filebeat 换成 logstash
    locoz
        7
    locoz  
       Jun 12, 2019
    @deepall #6 logstash 不会占资源太多吗
    1800x
        8
    1800x  
       Jun 13, 2019 via Android
    一般来说
    标准输出
    由日志收集程序收集处理,可能还需要分析
    lolizeppelin
        9
    lolizeppelin  
       Jun 14, 2019
    请使用 openstack oslo log
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4884 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 09:54 · PVG 17:54 · LAX 02:54 · JFK 05:54
    ♥ Do have faith in what you're doing.