• logging模块


    #  logging模块 ------ 日志
    #  为啥写log?
    # 为了排错    为了做数据分析   操作审计
    # 无论想打印日志里面的什么内容,都需要自己写
    # basicConfig 进行设置格式
    
    import logging
    # 输出内容有五个级别  默认
    # logging.debug('debug message')  #调试
    # logging.info('info')            信息
    # logging.warning('warning')         警告
    # logging.error('error')           错误
    # logging.critical('critical')        批判性的
    
    # logging.basicConfig函数各参数:
    # filename:指定日志文件名;
    # filemode:和file函数意义相同,指定日志文件的打开模式,'w'或者'a';
    # format:指定输出的格式和内容,format可以输出很多有用的信息,
    # datefmt:指定时间格式,同time.strftime()
    #
    # %(levelno)s:打印日志级别的数值
    #
    # %(levelname)s:打印日志级别的名称
    #
    # %(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]
    #
    # %(filename)s:打印当前执行程序名
    #
    # %(funcName)s:打印日志的当前函数
    #
    # %(lineno)d:打印日志的当前行号
    #
    # %(asctime)s:打印日志的时间
    #
    # %(thread)d:打印线程ID
    #
    # %(threadName)s:打印线程名称
    #
    # %(process)d:打印进程ID
    #
    # %(message)s:打印日志信息
    
    # logging.warning('warning message text1')
    # logging.basicConfig(
    #     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
    #     datefmt='%Y-%M-%d %H:%M:%S %p',
    #     #level= logging.DEBUG,
    # )
    #
    # logging.warning('warning message text2')
    # logging.error('error message text2')
    # logging.critical('cietical message text2')
    
    # 输出到文件
    
    # logging.basicConfig(
    #     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
    #     datefmt='%Y-%M-%d %H:%M:%S %p',
    #     filename='tmp.log',
    #     filemode='w',
    #     level= logging.DEBUG
    #
    # )
    #
    # logging.warning('warning message text1')
    # logging.error('error message text2')
    # logging.critical('cietical message text3')
    
    
    
    # 同时输出到屏幕和文件
    
    # fh = logging.FileHandler('tmp.log',encoding='utf-8')
    # fh2 = logging.FileHandler('tmp2.log',encoding='utf-8')
    #
    # sh = logging.StreamHandler()
    # logging.basicConfig(
    #     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
    #     datefmt='%Y-%M-%d %H:%M:%S %p',
    #
    #     level= logging.ERROR,
    #     handlers=[fh,sh,fh2]
    # )
    #
    # logging.warning('warning message text1')
    # logging.error('error message text2')
    # logging.critical('cietical message text3')
    
    
    # 做日志切割
    # import time
    # from logging import handlers
    # sh = logging.StreamHandler()
    # rh = handlers.RotatingFileHandler('myapp.log',maxBytes=1024,backupCount=5)  按照文件大小切割
    # fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5,encoding='utf-8') 按照时间切割
    # logging.basicConfig(
    #     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
    #     datefmt='%Y-%M-%d %H:%M:%S %p',
    #
    #     level= logging.DEBUG,
    #     handlers=[fh,rh,sh]
    # )
    #
    # for i in range(1,100000):
    #     time.sleep(0.1)
    #     logging.error('KeyboardInterrupt error %s'%str(i))
    Never compromise.
  • 相关阅读:
    6. 复习complex类的实现过程
    英文文献写作注意事项
    5. 操作符重载与临时对象
    4. 参数传递与返回值
    3. 构造函数
    JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
    JSON.NET 使用技巧
    异常处理 Exception
    HTTP报文
    HttpWebRequest类
  • 原文地址:https://www.cnblogs.com/luoluokang/p/12737037.html
Copyright © 2020-2023  润新知