python日志管理
来源:互联网 发布:js防止跨站脚本攻击 编辑:程序博客网 时间:2024/05/22 07:07
python日志管理
开发过程中日志管理是很重要的,Java中有 log4j插件,具体格式为时间、类型、日志内容,那么python中如何构建这个日志
# -*- coding: utf-8 -*-import datetimeimport loggingimport logging.handlersimport os# import fileutildef log(log_message, log_file = 'log.txt', terminator_print = False):# if terminator_print:# try:# print log_message# except:# print 'An exception occured when print log message.' bug_file = file(log_file, 'a') date_time = datetime.datetime.now() bug_file.write(date_time.strftime('%Y-%m-%d %H:%M:%S') + '\n') bug_file.write(log_message + '\n') bug_file.close()def write(message, aim_file): aim_file = file(aim_file, 'a') aim_file.write(message) aim_file.close()class logger(object): def __init__(self, name = 'root', log_level = logging.DEBUG, log_filename = os.path.join('log', 'log.txt'), maxBytes = 1000000, backupCount = 5): self.logger = logging.getLogger(name) self.logger.setLevel(log_level) log_directory = os.sep.join(log_filename.split(os.sep)[:-1]) if (not os.path.exists(log_directory)) or (not os.path.isdir(log_directory)): os.makedirs(log_directory) handler = logging.handlers.RotatingFileHandler(log_filename, maxBytes = maxBytes, backupCount = backupCount) formatter = logging.Formatter('%(asctime)s %(levelname)s %(filename)s %(lineno)d %(module)s %(funcName)s %(message)s') handler.setFormatter(formatter) self.logger.addHandler(handler) def settings(self, name = 'root', log_level = logging.DEBUG, log_filename = os.path.join('log', 'log.txt'), maxBytes = 1000000, backupCount = 5): self.__init__(name, log_level, log_filename, maxBytes, backupCount)logger = logger()if __name__ == '__main__': logger.logger.critical('This is a critical.') logger.logger.debug('This is a debug.') logger.logger.info('This is a info.') logger.logger.error('This is an error.') logger.logger.exception('This is an exception.')
联系我
本人qq群 463175657
0 0
- python日志管理
- python日志管理
- python模块——logging(日志管理)
- python模块——logging(日志管理)
- python模块——logging(日志管理)
- python模块——logging(日志管理)
- python模块——logging(日志管理)
- python日志管理模块logging学习
- Python学习笔记 --- 实现日志管理服务
- 日志管理
- 日志管理
- 日志管理
- 日志管理
- 日志管理
- 日志管理
- 日志管理
- 日志管理
- 日志管理
- 洛谷 P3092 [USACO13NOV]没有找零No Change
- python基础学习之循环,列表
- Java注解(2)-自定义注解、注解处理器
- 九度OJ题目1153:括号匹配问题
- 获得容器的绝对路径
- python日志管理
- iOS UISearchBar使用全解 搜索条使用大全 swift3
- 使用LitePal操作数据库
- Java常用类
- webpack.config.js配置文件
- 关于 local host 修改 如何修改电脑上的host 同时映射到手机上
- xshell连接Ubuntu
- swust.oj.1072
- Android内存泄露自动检测神器LeakCanary