首页 > 代码库 > 使用logging模块实现对文件增删改查操作的纪录(low B版)

使用logging模块实现对文件增删改查操作的纪录(low B版)

#!/usr/bin/env python
#对之前文件进行的增删改查操作实现日志操作,日志输出用户进行过的操作。
#!/usr/bin/env python
def log():
import logging
logger_obj=logging.getLogger()
logger_obj.setLevel(logging.INFO)
fh=logging.FileHandler(‘log.txt‘)
ch=logging.StreamHandler()
logger_obj.addHandler(fh)
logger_obj.addHandler(ch)
formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(message)s‘)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
return logger_obj
logger=log() #函数赋值给变量,可以随意调用。

# logger.debug(‘*****************‘)
# logger.info(‘this is adding operation ‘)
# logger.warning(‘this is delete operation‘)
# logger.error(‘this is changing operation‘)
# logger.critical(‘this is scaning operation‘)
def add():
logger.info(‘this is adding operation ‘)
pass
def delete():
logger.warning(‘this is delete operation‘)
pass
def change():
logger.error(‘this is changing operation‘)
pass
def scan():
logger.critical(‘this is scaning operation‘)
pass
add()
delete()
change()
scan()


个人水平有限,增删改查四个函数还未全部写完,先拿空函数占个位置吧!日后补上!!!

使用logging模块实现对文件增删改查操作的纪录(low B版)