• Python3 logging模块&ConfigParser模块


      1 '''
      2 博客园 Infi_chu
      3 '''
      4 
      5 '''
      6 logging模块
      7 该模块是关于日志相关操作的模块
      8 '''
      9 
     10 import logging
     11 
     12 # logging.debug('debug')
     13 # logging.info('info')
     14 # logging.warning('warning')      # 默认级别,上打印不出来,下可以打印,但权限可改
     15 # logging.error('error')
     16 # logging.critical('critical')
     17 
     18 '''
     19 博客园 Infi_chu
     20 '''
     21 
     22 '''
     23 日志级别等级
     24 critical>error>warning>info>debug>notset
     25 '''
     26 
     27 # 配置日志
     28 # logging.basicConfig(level=logging.DEBUG,    # 级别
     29 #                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',       # 格式
     30 #                     datefmt='%a, %d %b %Y %H:%M:%S',    # asctime的格式
     31 #                     filename='F:Python projectlog.txt',   # 文件名
     32 #                     filemode='w')   # 文件模式
     33 
     34 '''
     35 可用参数
     36 filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
     37 filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
     38 format:指定handler使用的日志显示格式。 
     39 datefmt:指定日期时间格式。 
     40 level:设置rootlogger(后边会讲解具体概念)的日志级别 
     41 stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open('test.log','w')),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。
     42 '''
     43 
     44 '''
     45 博客园 Infi_chu
     46 '''
     47 
     48 
     49 '''
     50 format参数中可能用到的格式化串:
     51 %(name)s Logger的名字
     52 %(levelno)s 数字形式的日志级别
     53 %(levelname)s 文本形式的日志级别
     54 %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
     55 %(filename)s 调用日志输出函数的模块的文件名
     56 %(module)s 调用日志输出函数的模块名
     57 %(funcName)s 调用日志输出函数的函数名
     58 %(lineno)d 调用日志输出函数的语句所在的代码行
     59 %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
     60 %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
     61 %(asctime)s 字符串形式的当前时间。默认格式是 “2017-10-18 19:05:36,765”。逗号后面的是毫秒
     62 %(thread)d 线程ID。可能没有
     63 %(threadName)s 线程名。可能没有
     64 %(process)d 进程ID。可能没有
     65 %(message)s用户输出的消息
     66 '''
     67 
     68 '''
     69 博客园 Infi_chu
     70 '''
     71 
     72 # logging.debug('debug')
     73 # logging.info('info')
     74 # logging.warning('warning')
     75 # logging.error('error')
     76 # logging.critical('critical')
     77 
     78 '''
     79 博客园 Infi_chu
     80 '''
     81 # 另外一个模块级别的函数
     82 import  logging
     83 # logger = logging.getLogger()
     84 # d1 = logging.FileHandler('asd.log')     # 创建一个文件输出对象,handler,用于写入日志文件,FileHandler是文件输出流对象
     85 # d2 = logging.StreamHandler()             # 创建一个屏幕输出对象,标准输出流,用于输出到控制台(屏幕)
     86 # format1 = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')    # 格式
     87 # d1.setFormatter(format1)    # 用上面一行代码规定的格式输出
     88 # d2.setFormatter(format1)    # 用上面一行代码规定的格式输出
     89 # logger.addHandler(d1)       # 将d1加入到logger中,文件输出显示
     90 # logger.addHandler(d2)       # 将d2加入到logger中,控制台输出显示
     91 #
     92 # logger.debug('debug')
     93 # logger.info('info')
     94 # logger.warning('warning')
     95 # logger.error('error')
     96 # logger.critical('critical')
     97 
     98 '''
     99 博客园 Infi_chu
    100 '''
    101 
    102 '''
    103 ConfigParser模块(Python3)
    104 该模块用于生成和修改常见的配置文档
    105 '''
    106 import configparser
    107 
    108 # config_file1 = configparser.ConfigParser()        # 调用配置操作句柄
    109 #
    110 # # 创建
    111 # # 方法一
    112 # config_file1['DEFAULT'] = {'ServerAliveInterval':45,
    113 #                            'Compression':'yes',
    114 #                            'CompressionLevel':9}
    115 # # 方法二
    116 # config_file1['DEFAULT1'] = {}
    117 # config_file1['DEFAULT1']['USER'] = 'bob'
    118 # # 方法三
    119 # add_group = config_file1['DEFAULT1']
    120 # add_group['GROUP'] = 'manager'
    121 #
    122 # with open('config_file.ini','w') as configfile:     # 写入,configfile不要动
    123 #     config_file1.write(configfile)
    124 
    125 '''
    126 博客园 Infi_chu
    127 '''
    128 
    129 # # 读文件
    130 # rd = configparser.ConfigParser()  # 调用配置操作句柄
    131 # rd.read('config_file.ini')
    132 # print(rd.sections())      # 读出了大标题,也就是上述配置过程中字典的键,除了默认
    133 # print(rd.defaults())      # 读出大标题,只读出了默认
    134 # print(rd['DEFAULT1']['group'])  # 取大标题下的值
    135 #
    136 # for i in rd:
    137 #     print(i)        # 查看大标题
    138 #
    139 # for i in rd['DEFAULT']:
    140 #     print(i)        # 查看大标题下的键,只显示DEFAULT
    141 #
    142 # for i in rd['DEFAULT1']:
    143 #     print(i)        # 查看大标题下的键,包括DEFAULT
    144 '''
    145 博客园 Infi_chu
    146 '''
    147 # # 删除配置信息
    148 # rmv = configparser.ConfigParser()
    149 # rmv.remove_section('DEFAULT1')
    150 # rmv.write(open('config_file1.ini','w'))
    151 # rmv.remove_option('DEFAULT','compressionlevel')     # 删除键中键
    152 # rmv.write(open('config_file.ini','w'))
    153 #
    154 # # 查看配置中是否有此条信息
    155 # find1 = configparser.ConfigParser()
    156 # print(find1.has_section('DEFAULT'))
    157 # find1.write(open('config_file.ini','r'))
    158 #
    159 # # 修改配置文件
    160 # conf = configparser.ConfigParser()
    161 # print(conf.set('DEFAULT1','group','123'))
    162 # conf.write(open('config_file.ini','w'))
    163 '''
    164 博客园 Infi_chu
    165 '''
  • 相关阅读:
    10、驱动中的阻塞与非阻塞IO
    8、Linux设备驱动的并发控制
    入职一个月考核学习
    5、映射的思考
    6、udev机制
    7、字符设备系统
    linux 内存管理之kmalloc、vmalloc、malloc、get_gree_pages的区别
    嵌入式笔试题(linux基础)
    驱动总结
    系统移植总结
  • 原文地址:https://www.cnblogs.com/Infi-chu/p/7693494.html
Copyright © 2020-2023  润新知