设为首页 加入收藏

TOP

Python-14-常用模块(七)
2019-07-15 16:09:59 】 浏览:219
Tags:Python-14- 常用 模块
# %(relativeCreated)d:输出日志信息时的,自Logger创建以来的毫秒数 # %(asctime)s:字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 # %(thread)d:线程ID。可能没有 # %(threadName)s:线程名。可能没有 # %(process)d:进程ID。可能没有 # %(message)s:用户输出的消息

3. logging模块的Formatter,Handler,Logger,Filter对象

def log():
    # logger对象:负责产生日志,然后交给Filter过滤,然后交给不同的Handler输出
    logger = logging.getLogger('mylogger')
    # Filter对象:不常用,略
    # Handler对象:接收logger传来的日志,然后控制输出
    fh = logging.FileHandler('log_test')  # 打印到文件
    ch = logging.StreamHandler()  # 打印到终端
    # Formatter对象:日志格式
    fm = logging.Formatter('%(asctime)s %(filename)s [%(lineno)d] %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p')
    # 为Handler对象绑定格式
    fh.setFormatter(fm)
    ch.setFormatter(fm)
    # 将Handler添加给logger并设置日志级别
    logger.addHandler(fh)
    logger.addHandler(ch)
    logger.setLevel('DEBUG')
    return logger


# 测试
logger = log()
logger.info('hello')
logging.debug('kjhk')
logging.error('nhdvh')
logging.warning('sg')
logging.critical('sv')

4. logger的继承

import logging

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                              datefmt='%Y-%m-%d %H:%M:%S %p', )

ch = logging.StreamHandler()
ch.setFormatter(formatter)

logger1 = logging.getLogger('root')
logger2 = logging.getLogger('root.child1')
logger3 = logging.getLogger('root.child1.child2')

logger1.addHandler(ch)
logger2.addHandler(ch)
logger3.addHandler(ch)
logger1.setLevel(10)
logger2.setLevel(10)
logger3.setLevel(10)

logger1.debug('log1 debug')
logger2.debug('log2 debug')
logger3.debug('log3 debug')
'''
2017-07-28 22:22:05 PM - root - DEBUG -test:  log1 debug
2017-07-28 22:22:05 PM - root.child1 - DEBUG -test:  log2 debug
2017-07-28 22:22:05 PM - root.child1 - DEBUG -test:  log2 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test:  log3 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test:  log3 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test:  log3 debug
'''

十、configparser模块

configparser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。

1. 配置文件样式

# 注释1
; 注释2

[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31

[section2]
k1 = v1

2. 创建

import configparser
  
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}
  
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)

3. 读取

import configparser

config = configparser.ConfigParser()
config.read('confile')

# 查看所有的标题
res = config.sections()  # ['section1', 'section2']
print(res)

# 查看标题section1下所有key=value的key
options = config.options('section1')
print(options)  # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

# 查看标题section1下所有key=value的(key,value)格式
item_list = config.items('section1')
print(item_list)  # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('a
首页 上一页 4 5 6 7 下一页 尾页 7/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用Python递归做个多层次的文件执行 下一篇数据库-高级部分

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目