zoukankan      html  css  js  c++  java
  • [Python] Create a Log for your Python application


    Print statements will get you a long way in monitoring the behavior of your application, but logging will get your further. Learn how to implement logging in this lesson to generate INFO, WARNING, ERROR, and DEBUG logs for your application.

    import sys
    import getopt
    import logging
    
    # pass in: python3 my_log.py -l info
    
    # Get command line options
    # short: l:
    # long: [log=]
    opts, args = getopt.getopt(sys.argv[1:], "l:", ["log="])
    
    print("opts", opts) #[('-l', 'info')]
    print("args", args) #[]
    
    # default log level
    log_level="INFO"
    
    for opt, arg in opts: #opt: -l, arg: info
        if opt in ("-l", "--log"):
            log_level = getattr(logging, arg.upper())
    
    logging.basicConfig(filename="./demo.log", level=log_level, format='%(asctime)s %(levelname)s:%(message)s')
    
    
    for i in range(0, 100):
        if i % 5 == 0:
            logging.debug('Found a number divisible by 5: {0}'.format(i))
        else:
            logging.info('At number {0}'.format(i))
    
    logging.warning('Finished sequence')
  • 相关阅读:
    10046 event 知多少
    10046 event 知多少
    awr相关指标解析
    父子关系展示
    secureCRT启动xmanager图形化工具
    linux单用户模式
    Tor
    windows下的unix工具集:UnxUtils
    OPENLDAP
    Windows命令行重命名文件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8023103.html
Copyright © 2011-2022 走看看