python3日志日志如何封装

  介绍

本篇内容介绍了”python3日志日志如何封装”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Python主要用来做什么

Python主要应用于:1,网络开发;2、数据科学研究;3,网络爬虫;4、嵌入式应用开发,5日游戏开发;6桌面应用开发。


一个完整的程序离不开日志,无论是开发阶段,还是测试阶段,亦或程序运行阶段,都可以通过日志查看程序的运行情况,或是定位问题。

下面是对python3的日志库记录进行了封装,对于大部分的需求应该是能满足的。

<强>程序结构:

|——logger.py   |   |——singleton.py   |   |——demo.py   |   | - - -日志   |,|   |,,2018 - 10 - 12. -日志

记录器。py

import 操作系统   import 系统   import 时间   import 日志   得到singleton  import 单例   ,   ,   @Singleton ,, #,如需打印不同路径的日志(运行日志,审计日志),则不能使用单例模式(注释或删除此行)。此外,还需设定参数名。   class 日志:   def  __init__(才能自我,,set_level=癐NFO",   ,,,,,,,,的名字=os.path.split (os.path.splitext (sys.argv [0]) [0]) [1],   ,,,,,,,,log_name=time.strftime (“Y % - % - % d.log",, time.localtime ()),   ,,,,,,,,log_path=os.path.join (os.path.dirname (os.path.abspath (__file__)),,“log"),   ,,,,,,,,use_console=True):   ,,,,,,   ,,,:param  set_level:,日志级别(“NOTSET" |“DEBUG" |“INFO" |“WARNING" |“ERROR" |“CRITICAL"],默认为信息   ,,,:param 名称:,日志中打印的名字,默认为运行程序的名字   ,,,:param  log_name:,日志文件的名字,默认为当前时间(年-月-日. log)   ,,,:param  log_path:,日志文件夹的路径,默认为logger.py同级目录中的日志文件夹   ,,,:param  use_console:,是否在控制台打印,默认为真实的   ,,,,,,   ,,,if  not  set_level:   ,,,,,set_level =, self._exec_type(), #,设置set_level为None,自动获取当前运行模式   ,,,self.__logger =, logging.getLogger(名字)   ,,,self.setLevel (getattr(日志、,set_level.upper ()), if  hasattr(日志、,set_level.upper ()), else  logging.INFO), #,设置日志级别   ,,,if  not  os.path.exists (log_path):, #,创建日志目录   ,,,,,os.makedirs (log_path)   ,,,formatter =, logging.Formatter (“% (asctime) s 作用;%(名字)s 作用;% (levelname) s 作用;%(消息)s")   ,,,handler_list =,列表()   ,,,handler_list.append (logging.FileHandler (os.path.join (log_name log_path也),编码=皍tf-8"))   ,,,if  use_console:   ,,,,,handler_list.append (logging.StreamHandler ())   ,,,for  handler 拷贝handler_list:   ,,,,,handler.setFormatter (formatter)   ,,,,,self.addHandler(处理器)   ,   def 才能__getattr__(自我,,项):   ,,,return  getattr (self.logger,,项目)   ,   ,@ property   def 才能记录器(自我):   ,,,return  self.__logger   ,   ,@logger.setter   def 才能;记录器(自我,,函数):   ,,,self.__logger =函数   ,   def 才能_exec_type(自我):   ,,,return “DEBUG", if  os.environ.get (“IPYTHONENABLE"), else “INFO"

单例。py

class 单:   “才能”;“   ,,单例装饰器。   “才能”;“   __cls 才能=,dict类型()   ,   def 才能__init__(自我,,cls):   ,,,self.__key =cls   ,   def 才能;__call__(自我,,* args,, * * kwargs):   ,,,if  self.__key  not 拷贝self.cls:   ,,,,,自我[self.__key],=, self.__key (* args,, * * kwargs)   ,,,return 自我(self.__key)   ,   def 才能;__setitem__(自我,,钥匙,,值):   ,,,self.cls(例子),=,价值   ,   def 才能__getitem__(自我,,项):   ,,,return  self.cls(项)   ,   ,@ property   def 才能cls(自我):   ,,,return  self.__cls   ,   ,@cls.setter   def 才能cls(自我,,cls):   ,,,self.__cls =, cls

演示。py

import 记录器   ,   时间=x  logger.Logger (“debug")   ,   x.critical(“这是一个,critical 级别的问题!“)   x.error(“这是一个,error 级别的问题!“)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python3日志日志如何封装