zoukankan      html  css  js  c++  java
  • python基础12--logging等模块

    logging模块:

    import logging
    #日志级别:debug<info<warning<error<critical<NOTSET ,不设置等级默认是显示warning以上的级别
    logging.debug("debug message")
    logging.info("info message")
    logging.warning("warning message")
    logging.error("error nessage")
    logging.critical("critical message")
    #结果为
    # WARNING:root:warning message
    # ERROR:root:error nessage
    # CRITICAL:root:critical message
    
    #logging.basicConfig方式只能在屏幕显示或写入文件
    logging.basicConfig( level = logging.DEBUG,  #设定logging级别
                         filename="logger.txt",  # 如无次参数,结果显示在屏幕上,加上参数生成一个文本,并把结果存入文本中
                         filemode="w",            # #文件默认是追加,改成写的模式,保证每次都是五条信息
                         format="%(asctime)s %(name)s  [%(lineno)d]  %(message)s")   #asctime(固定时间格式),name(用户名称),lineno(日志所在文件的行数),message(日志信息)
    logging.debug("debug message")
    logging.info("info message")
    logging.warning("warning message")
    logging.error("error nessage")
    logging.critical("critical message")
    #结果为
    # DEBUG:root:debug message
    # INFO:root:info message
    # WARNING:root:warning message
    # ERROR:root:error nessage
    # CRITICAL:root:critical message
    
    
    #logger方式能写入文件显示和在屏幕中显示
    def logger():
        logger=logging.getLogger()
        fh=logging.FileHandler("logger_test")   #往文件中写入内容
        ch=logging.StreamHandler()     #往屏幕中显示内容
        fm=logging.Formatter("%(asctime)s  %(message)s  %(name)s")  #设定一个格式
        fh.setFormatter(fm)   #把fh,ch都应用成fm的格式
        ch.setFormatter(fm)
        logger.addHandler(fh)
        logger.addHandler(ch)
        return logger
    
    logger=logger()     #直接调用logger函数
    logger.setLevel("DEBUG")
    logger.debug("debug message")
    logger.info("info message")
    logger.error("error nessage")
    logger.warning("warning message")
    logger.critical("critical message")
    
    
    
    #补充:假设又logger、logger1、logger2三个对象,也是实现以上功能,当logger1(级别为debug)和logger2(级别为info)在getlogger时加上了同样的名字,相当于在root下面建了子用户--my logger,设定级别时
    #按最低的显示(info),当logger(默认为root用户)和logger1(级别为debug)运行时,logger打印warning以上级别的信息,而logger1打印debug以上级别的信息,因为my logger用户是root用户的子用户,当logger
    # 有运行,就会再打印一次给root用户。
    View Code

    configparser模块:

    import configparser   #创建一个配置文件
    config=configparser.ConfigParser()    #相当于创建了config={}
    config['default']={'SeverAliveInterval':'45',
                       'Compresion':'yes',
                       'CompressionLevel':'9',
                       'ForwardXll':'yes'}
    config['bitbucket.org']={}
    config['bitbucket.org']['User']='hg'
    config['topsecret.server.com']={}
    topsecret=config['topsecret.server.com']
    topsecret['Host Port']='50022'
    topsecret['ForwardXll']='no'
    with open ("example_test","w") as f:
        config.write(f)
    
    
    import configparser
    config=configparser.ConfigParser()
    config.read('example_test')
    #--------------查
    print(config.sections())      #查看文件的块  #['bitbucket.org', 'topsecret.server.com'],DEFAULT是默认的块,不显示,常用于重要或重复信息
    print(config['bitbucket.org']['user'])   #hg
    print('sdasd.org' in config)              #False
    for key in config['bitbucket.org']:
        print(key)
    #结果如下,除了打印取的键外,默认模块的键也自动显示
    # user
    # compresion
    # forwardxll
    # compressionlevel
    # severaliveinterval
    print(config.options('topsecret.server.com'))     #['host port', 'forwardxll', 'compresion', 'compressionlevel', 'severaliveinterval']
    print(config.items('topsecret.server.com'))       #[('compresion', 'yes'), ('forwardxll', 'no'), ('compressionlevel', '9'), ('severaliveinterval', '45'), ('host port', '50022')]
    print(config.get('topsecret.server.com','compresion'))  #yes 如当前模块没有,从默认模块找
    
    #----------------------删、改、增
    config.add_section('yuan')
    config.set("yuan",'k1','all')    #增加键值对
    config.remove_option("topsecret.server.com","forwardxll")   #删除块的某个键
    config.remove_section("topsecret.server.com")                #删除整个块
    config.write(open("example_new","w"))                        #建议这种打开文件的方式,不用再close
    View Code

    hashlib模块:

    #用于加密的有md5模块和sha模块,sha模块包括了SHA1,SHA224,SHA256,SHA384,SHA512,sha算法和md5算法使用方式相同
    import hashlib    #明文转化成固定位数的密文
    obj=hashlib.md5()
    obj.update("hello".encode("utf8"))
    print(obj.hexdigest())     #5d41402abc4b2a76b9719d911017c592,把不同长度的字符串编码成固定位数的密文,且是唯一的,但容易被反解
    
    #往md5加上字符串,就难以被反解
    import hashlib    
    obj=hashlib.md5()
    obj=hashlib.md5("sda".encode("utf8"))
    obj.update("hello".encode("utf8"))
    print(obj.hexdigest())      #a9fddcda5af26214d02ae67994f15e1e
    
    import hashlib
    obj=hashlib.md5()
    obj.update("hello".encode("utf8"))
    obj.update("world".encode("utf8"))
    print(obj.hexdigest())             #fc5e038d38a57032085441e7fe7010b0,相当于为helloworld加密
    View Code
  • 相关阅读:
    ASP.NET MVC 重点教程一周年版 第二回 UrlRouting
    ASP.NET MVC 重点教程一周年版 第三回 Controller与View
    DynamicData for Asp.net Mvc留言本实例 下篇 更新
    Asp.net MVC视频教程 18 单选与复选框
    使用ASP.NET MVC Futures 中的异步Action
    ASP.NET MVC RC 升级要注意的几点
    ATL、MFC、WTL CString 的今生前世
    msvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义
    关于Windows内存的一些参考文章
    Windows访问令牌相关使用方法
  • 原文地址:https://www.cnblogs.com/sakura-gyt/p/12913134.html
Copyright © 2011-2022 走看看