为python脚本设计自己的log模块
2018-02-25 13:13
399 查看
这个方法是来自《python网络实战》(清华大学出版社)中推荐使用的log方法,原理是利用log功能抽象出一个专门的模块负责log记录,剥离之后模块可以提供更多的自定义空间,使用起来非常方便
import loggingimport getpassimport sys
#定义Mylog类,管理log信息class MyLog(object): def __init__(self): self.user = getpass.getuser() self.logger = logging.getLogger(self.user) self.logger.setLevel(logging.DEBUG)
#日志文件名 self.logFile = sys.argv[0][0:-3] + '.log' self.formatter = logging.Formatter('%(asctime)-12s %(levelname)- 8s %(name)-10s %(message)-12s\r\n')
#日志显示到屏幕并输出到文档 self.logHand = logging.FileHandler(self.logFile, encoding='utf-8') self.logHand.setFormatter(self.formatter) self.logHand.setLevel(logging.DEBUG)
self.logHandSt = logging.StreamHandler() self.logHandSt.setFormatter(self.formatter) self.logHandSt.setLevel(logging.DEBUG)
self.logger.addHandler(self.logHand) self.logger.addHandler(self.logHandSt)
#日志的5个级别对应5个函数 def debug(self, msg): self.logger.debug(msg)
def info(self, msg): self.logger.info(msg)
def warn(self, msg): self.logger.warn(msg)
def error(self, msg): self.logger.error(msg)
def critical(self, msg): self.logger.critical(msg)
#测试代码 __name__是访问当前函数名的方法,如果作为模块就不是main函数 下面的方法不会执行if __name__ == '__main__': mylog = MyLog() mylog.debug(u"I'm 测试中文") mylog.info("I;m info") mylog.warn("warning") mylog.error("Error") mylog.critical("this is a critical")
在别的模块中引用时,需要import Mylog,然后调用里面的方法即可
import loggingimport getpassimport sys
#定义Mylog类,管理log信息class MyLog(object): def __init__(self): self.user = getpass.getuser() self.logger = logging.getLogger(self.user) self.logger.setLevel(logging.DEBUG)
#日志文件名 self.logFile = sys.argv[0][0:-3] + '.log' self.formatter = logging.Formatter('%(asctime)-12s %(levelname)- 8s %(name)-10s %(message)-12s\r\n')
#日志显示到屏幕并输出到文档 self.logHand = logging.FileHandler(self.logFile, encoding='utf-8') self.logHand.setFormatter(self.formatter) self.logHand.setLevel(logging.DEBUG)
self.logHandSt = logging.StreamHandler() self.logHandSt.setFormatter(self.formatter) self.logHandSt.setLevel(logging.DEBUG)
self.logger.addHandler(self.logHand) self.logger.addHandler(self.logHandSt)
#日志的5个级别对应5个函数 def debug(self, msg): self.logger.debug(msg)
def info(self, msg): self.logger.info(msg)
def warn(self, msg): self.logger.warn(msg)
def error(self, msg): self.logger.error(msg)
def critical(self, msg): self.logger.critical(msg)
#测试代码 __name__是访问当前函数名的方法,如果作为模块就不是main函数 下面的方法不会执行if __name__ == '__main__': mylog = MyLog() mylog.debug(u"I'm 测试中文") mylog.info("I;m info") mylog.warn("warning") mylog.error("Error") mylog.critical("this is a critical")
在别的模块中引用时,需要import Mylog,然后调用里面的方法即可
相关文章推荐
- 怎么设计自己的模块
- python下导入自己定义的模块的方法
- Python 发送 RMAN 备份 Log 脚本
- python学习十二(自己制作模块并安装到系统、静态方法)
- Python的log模块日志写两遍的问题
- 自己用Python写的iOS项目自动打包脚本
- 【脚本语言系列】关于Python设计模式,你需要知道的事情
- pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)
- python 中导入自己定义的模块路径
- CovertUtils:一个用于设计和创建后门的Python模块
- 【脚本语言系列】关于Python数据库访问专用模块,你需要知道的事
- 基于python智慧树刷课脚本在C#语言下的设计与实现
- python脚本初探---新手如何直接编写一个hello world模块即可执行的.py文件
- pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)
- python logging模块打印log到指定文件
- 基于dragonboard 410c的智能魔镜设计(8)——UI的python脚本实现
- python file模块 替换输入内容脚本
- 用Python脚本定制自己的Web服务器 推荐
- c++导入python模块,如果python脚本与exe不在同一目录,需要增加搜索路径
- 开发自己的脚本引擎(二)脚本语法的设计。