xtn-tools-pro 1.0.0.1.8__py3-none-any.whl → 1.0.0.2.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -121,6 +121,17 @@ class RedisDBPro:
121
121
  """
122
122
  return self._redis.spop(table)
123
123
 
124
+ def sget_count(self, table):
125
+ """
126
+ 获取无序集合数量
127
+ :param table:
128
+ :return:
129
+ """
130
+ return self._redis.scard(table)
131
+
132
+ def get_redis(self):
133
+ return self._redis
134
+
124
135
  def incr(self, key):
125
136
  """
126
137
  对一个键的值进行自增操作
@@ -163,6 +174,8 @@ class RedisDBPro:
163
174
  return self._redis.delete(table)
164
175
 
165
176
 
177
+
178
+
166
179
  if __name__ == '__main__':
167
180
  pass
168
181
  # r = RedisDBPro(ip="127.0.0.1", port=6379, db=0, user_pass="xtn-kk")
@@ -35,6 +35,15 @@ def get_file_check_filename(file_name):
35
35
  sanitized_filename = sanitized_filename[:max_length]
36
36
  return sanitized_filename
37
37
 
38
+ def mkdirs_dir(file_path):
39
+ """
40
+ 传入一个文件路径创建对应的文件夹,如果某一段不存在则一路创建下去
41
+ :param file_path:
42
+ :return:
43
+ """
44
+ directory = os.path.dirname(file_path)
45
+ if not os.path.exists(directory):
46
+ os.makedirs(directory)
38
47
 
39
48
  if __name__ == '__main__':
40
49
  pass
@@ -13,6 +13,7 @@ import sys
13
13
  import inspect
14
14
  import logging
15
15
  from logging.handlers import BaseRotatingHandler
16
+ from xtn_tools_pro.utils.file_utils import mkdirs_dir
16
17
  from xtn_tools_pro.utils.time_utils import get_time_timestamp_to_datestr
17
18
 
18
19
 
@@ -77,7 +78,10 @@ class Log:
77
78
  is_write_to_file=False,
78
79
  color=True,
79
80
  mode='a',
80
- max_bytes=0, backup_count=0, encoding=None):
81
+ max_bytes=1024000000,
82
+ backup_count=0,
83
+ encoding="utf-8",
84
+ save_time_log_path=None):
81
85
  """
82
86
  :param name: log名
83
87
  :param path: log文件存储路径 如 D://xxx.log
@@ -89,18 +93,27 @@ class Log:
89
93
  :param max_bytes: 每个日志文件的最大字节数
90
94
  :param backup_count: 日志文件保留数量
91
95
  :param encoding: 日志文件编码
96
+ :param save_time_log_path: 保存时间日志的路径 './logs'
92
97
 
93
98
  """
94
99
  # 创建logger对象
95
100
  self.logger = logging.getLogger(name)
96
101
  # 设置日志等级
97
102
  self.logger.setLevel(log_level.upper())
103
+ self.save_time_log_path = save_time_log_path
104
+ if save_time_log_path:
105
+ # 记录当前的日期,用于之后检查日期是否已经改变
106
+ self.current_date = get_time_timestamp_to_datestr(format="%Y_%m_%d")
107
+ # 记录日志文件的路径模板,用于之后创建新的日志文件
108
+ self.path_template = self.path_template = f"{save_time_log_path}/{{date}}/{name}.log"
109
+ path = self.path_template.format(date=self.current_date)
110
+ mkdirs_dir(self.path_template.format(date=self.current_date))
98
111
 
99
112
  # 创建日志格式化器
100
113
  # formatter = logging.Formatter('[%(now_datestr)s] [%(levelname)s] [%(func_name)s] - %(message)s') # 原
101
114
  # formatter = logging.Formatter('\033[1m%(now_datestr)s] [%(levelname)s] [%(func_name)s] - %(message)s\033[0m') #加粗
102
115
  formatter = logging.Formatter(
103
- '\033[1m[%(now_datestr)s] | %(levelname)-8s | [%(func_name)s] - %(message)s\033[0m') # 加粗对齐
116
+ '[%(now_datestr)s] | %(levelname)-8s | [%(func_name)s] - %(message)s') # 加粗对齐
104
117
 
105
118
  # formatter = BoldFormatter('[%(now_datestr)s] [%(levelname)s] [%(func_name)s] - %(message)s') # 加粗
106
119
 
@@ -139,31 +152,53 @@ class Log:
139
152
  'CRITICAL': 'bold_red',
140
153
  }
141
154
  color_formatter = ColoredFormatter(
142
- '\033[1m%(log_color)s[%(now_datestr)s] | %(levelname)-8s | [%(func_name)s] - %(message)s\033[0m',
155
+ # '\033[1m%(log_color)s[%(now_datestr)s] | %(levelname)-8s | [%(func_name)s] - %(message)s\033[0m',
156
+ '%(log_color)s[%(now_datestr)s] | %(levelname)-8s | [%(func_name)s] - %(message)s',
143
157
  log_colors=log_colors) # 加粗对齐
144
158
  # 设置控制台处理器的格式化器为带颜色的格式化器
145
159
  console_handler.setFormatter(color_formatter)
146
160
  except ImportError:
147
161
  pass
148
162
 
163
+ def _get_log_file_path(self):
164
+ """获取日志文件的路径,如果日期已经改变就创建一个新的日志文件"""
165
+ if not self.save_time_log_path: return
166
+ date = get_time_timestamp_to_datestr(format="%Y_%m_%d")
167
+ if date != self.current_date:
168
+ # 日期已经改变,更新当前的日期
169
+ self.current_date = date
170
+ # 更新文件处理器的文件路径
171
+ path = self.path_template.format(date=date)
172
+ mkdirs_dir(path)
173
+ for handler in self.logger.handlers:
174
+ if isinstance(handler, RotatingFileHandler):
175
+ handler.baseFilename = path
176
+ handler.doRollover()
177
+ break
178
+
149
179
  def debug(self, message):
150
180
  # 记录DEBUG级别的日志
181
+ self._get_log_file_path()
151
182
  self.logger.debug(message, extra=self._get_caller_name_extra())
152
183
 
153
184
  def info(self, message):
154
185
  # 记录INFO级别的日志
186
+ self._get_log_file_path()
155
187
  self.logger.info(message, extra=self._get_caller_name_extra())
156
188
 
157
189
  def warning(self, message):
158
190
  # 记录WARNING级别的日志
191
+ self._get_log_file_path()
159
192
  self.logger.warning(message, extra=self._get_caller_name_extra())
160
193
 
161
194
  def error(self, message):
162
195
  # 记录ERROR级别的日志
196
+ self._get_log_file_path()
163
197
  self.logger.error(message, extra=self._get_caller_name_extra())
164
198
 
165
199
  def critical(self, message):
166
200
  # 记录CRITICAL级别的日志
201
+ self._get_log_file_path()
167
202
  self.logger.critical(message, extra=self._get_caller_name_extra())
168
203
 
169
204
  def _get_caller_name_extra(self):
@@ -185,8 +220,11 @@ class Log:
185
220
  if __name__ == '__main__':
186
221
  pass
187
222
  logger = Log('mylogger', './xxx.log', log_level='DEBUG', is_write_to_console=True, is_write_to_file=True,
188
- color=True, mode='a', max_bytes=1024, backup_count=3)
189
- logger.debug("debug message")
190
- logger.info("info level message")
191
- logger.warning("warning level message")
192
- logger.critical("critical level message")
223
+ color=True, mode='a', save_time_log_path='./logs')
224
+ # import time
225
+ # while True:
226
+ # logger.debug("debug message")
227
+ # logger.info("info level message")
228
+ # logger.warning("warning level message")
229
+ # logger.critical("critical level message 你好")
230
+ # time.sleep(1)
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.0.1.8
3
+ Version: 1.0.0.2.0
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
- Author-email: czw011122@163.com
6
+ Author-email: czw011122@gmail.com
7
7
  Classifier: Programming Language :: Python :: 3
8
8
  Requires-Python: >=3
9
9
  Description-Content-Type: text/markdown
@@ -4,21 +4,21 @@ xtn_tools_pro/tools_flie.py,sha256=-FID31G8AnV2u2djRO3Ae3Ei9ld9bSBWgzwqhYzZ5JQ,1
4
4
  xtn_tools_pro/tools_time.py,sha256=DMjsw9h4E_mrPsanPA8CEhpUE1AA6Z2FU4OJqJKZc1k,4867
5
5
  xtn_tools_pro/db/MongoDB.py,sha256=zQLfZ-mhZWCFgY3dPF3dRAzAR3C8hrXO_yLZw9cUUUY,5573
6
6
  xtn_tools_pro/db/MysqlDB.py,sha256=SBJrcjbZdxmtTKPGwl57NthPhs4uX8J3P6o_rK01O4k,13373
7
- xtn_tools_pro/db/RedisDB.py,sha256=XSdBQKViL4HM1hjI5nYnuUhA6qKB8H5LJjxWMqUfPWg,6228
7
+ xtn_tools_pro/db/RedisDB.py,sha256=YSWxYEPE2jp6r1ZKam2LyPgL0S2S9lGDmt36PLSwThg,6470
8
8
  xtn_tools_pro/db/__init__.py,sha256=Zg91UWS02TO0Ba_0AY56s0oabRy93xLNFkpIIL_6mMM,416
9
9
  xtn_tools_pro/proxy/XiaoXiangProxy.py,sha256=45SsMZDc-3Ta4THX38vScjaeRBJSp420AJwBeogC_-I,11049
10
10
  xtn_tools_pro/proxy/__init__.py,sha256=WRwh6s2lruMu5buh0ejo9EK54kWT_VQhCsFGNFAmcyo,418
11
11
  xtn_tools_pro/proxy/proxy.py,sha256=No6E1pFY5yx2F4976pXPrLtq-QEVp79KupzcufjSN58,8703
12
12
  xtn_tools_pro/utils/__init__.py,sha256=I1_n_NP23F2lBqlF4EOlnOdLYxM8M4pbn63UhJN1hRE,418
13
13
  xtn_tools_pro/utils/crypto.py,sha256=RZ5AET4udlraACWMeNF-17JiZ2R6Ahb47_j4tjkV7LE,3190
14
- xtn_tools_pro/utils/file_utils.py,sha256=4Bmb1ISxqQC-7doPXDyY2o-H0m4upj_-hlLrO36sKJg,1509
14
+ xtn_tools_pro/utils/file_utils.py,sha256=EoWz0ULgS1uY86KpsP2Ofxj4BGP2gdHnUrdGAGRmpl0,1813
15
15
  xtn_tools_pro/utils/helpers.py,sha256=eHhvhcw_jcUhCA6HwOqpD4XU_V_ZoHDgj3qtLR8yzx8,3812
16
- xtn_tools_pro/utils/log.py,sha256=8CRMiY7Q9LWGJLhfL0YfpANBwLets9f2qWfMOe5PpNM,8139
16
+ xtn_tools_pro/utils/log.py,sha256=XZChGrKWgAz42o_gFi_d7DL28svF1OvAw7NS_xkIgnQ,10056
17
17
  xtn_tools_pro/utils/retry.py,sha256=0wjHsR5DBBKpv4naMfxiky8kprrZes4WURIfFQ4H708,1657
18
18
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
19
19
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
20
- xtn_tools_pro-1.0.0.1.8.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
- xtn_tools_pro-1.0.0.1.8.dist-info/METADATA,sha256=mkaGze-wjwp0p_fsGoXwFQgQSFB0e_MSYA9ucqSBxAU,453
22
- xtn_tools_pro-1.0.0.1.8.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
23
- xtn_tools_pro-1.0.0.1.8.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
24
- xtn_tools_pro-1.0.0.1.8.dist-info/RECORD,,
20
+ xtn_tools_pro-1.0.0.2.0.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
+ xtn_tools_pro-1.0.0.2.0.dist-info/METADATA,sha256=x0QZDrArajGJGb16SkgXrVEbLSQ9zd1kcX5fkTkMUj4,455
22
+ xtn_tools_pro-1.0.0.2.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
23
+ xtn_tools_pro-1.0.0.2.0.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
24
+ xtn_tools_pro-1.0.0.2.0.dist-info/RECORD,,