63 WARNING_COLOR = YELLOW
114 if color
is not None:
115 prefix =
"{};".format(ConsoleColor.ShowType.DEFAULT)
116 suffix =
"{}m{}".format(color, text)
117 out =
"\033[{};{}\033[0m".format(prefix, suffix)
123 _instance_lock = threading.RLock()
125 def __init__(self, path, print_flag=False, save_flag=False, max_clear=10):
132 :param print_flag:是否打印日志 默认False
134 :param save_flag:是否保存日志 默认False
136 :param max_clear:日志储存最大限制,默认10MB 单位:MB
152 ALog.log(msg=msg, **kwargs)
155 def format_text(field: LogStatus, line, obj, task_name, msg, ned_text=
False,
162 write_repr = others.fullname(obj)
164 write_repr =
'OBJECT IS NULL'
166 write_repr =
'OBJECT CAN`T NOT PARSE'
167 t = datetime.datetime.utcnow().strftime(
'%Y-%m-%d %H:%M:%S.%f')
168 pure_text =
' '.join([str(t), str(field.value), str(line), str(hex(id(obj))),
169 '[{}]'.format(task_name), str(write_repr), f
" : {msg}"])
170 t = ConsoleWrite.format_color(f
"{t}".ljust(FieldsLength.DATETIME_FORMAT), ConsoleColor.FontColor.CYAN)
171 _field = ConsoleWrite.format_color(f
"{field.value}".rjust(FieldsLength.INFO_FORMAT),
172 ConsoleColor.FontColor.GREEN
173 if field == LogStatus.Info
174 else ConsoleColor.FontColor.RED
175 if field == LogStatus.Error
176 else ConsoleColor.FontColor.YELLOW
177 if field == LogStatus.Warn
178 else ConsoleColor.FontColor.YELLOW)
179 line = f
"{line}".rjust(FieldsLength.LINE_FORMAT)
180 hex_id = ConsoleWrite.format_color(f
" {str(hex(id(obj)))}", ConsoleColor.FontColor.PINK)
181 task_name = ConsoleWrite.format_color(f
"{task_name}".rjust(FieldsLength.TASK_FORMAT),
182 ConsoleColor.FontColor.PURPLE)
183 write_repr = ConsoleWrite.format_color(write_repr,
184 ConsoleColor.FontColor.LIGHT_GREEN
185 if field != LogStatus.Error
186 else ConsoleColor.FontColor.RED)
187 msg = ConsoleWrite.format_color(f
" : {msg}", text_color)
188 info =
"{}{}{}{}{}{}{}".format(t, _field, line, hex_id,
' [{}] '.format(task_name), write_repr, msg)
190 return info, pure_text
194 def log(msg, obj=None, line=sys._getframe().f_back.f_lineno, task_name=
'TEXT', LogObject=
None,
195 field: LogStatus = LogStatus.Info, func=
None,
203 :param task_name:任务对象的值
204 :param LogObject:写出文件的对象
206 :param func:日志执行后的自定义操作
211 write_repr = others.fullname(obj)
213 write_repr =
'OBJECT IS NULL'
215 write_repr =
'OBJECT CAN`T NOT PARSE'
219 t = datetime.datetime.utcnow().strftime(
'%Y-%m-%d %H:%M:%S.%f')[:-3]
220 con_text =
' '.join([str(t), str(field.value), str(line), str(hex(id(obj))),
221 '[{}]'.format(task_name), str(write_repr), f
" : {msg}"])
222 info = ALog.format_text(field, line, obj, task_name, msg, text_color=text_color)
226 def __log_obj_write__(_object):
227 if _object
is not None:
228 if field == LogStatus.Info:
229 _object.info(con_text, pure_text=info)
230 elif field == LogStatus.Error:
231 _object.error(con_text, pure_text=info)
232 elif field == LogStatus.Warn:
233 _object.warn(con_text, pure_text=info)
235 _object.info(con_text, pure_text=info)
238 if hasattr(obj,
'log_obj'):
239 __log_obj_write__(obj.log_obj)
241 __log_obj_write__(LogObject)
243 __log_obj_write__(LogObject)
251 ALog.log(task_name=
'WARNING', field=LogStatus.Warn, **kwargs)
254 def log_error(msg, obj=None, line=sys._getframe().f_back.f_lineno, task_name=
'ERROR',
255 LogObject=
None, raise_exception=
False):
259 :param obj:执行的对象,当允许抛出异常时,则指明该对象为一个Exception或他的子类
260 :param task_name:线程唯一名称
261 :param LogObject:日志对象
262 :param raise_exception:是否抛出异常
268 exc_type, exc_value, exc_traceback_obj = sys.exc_info()
269 extracted_list = traceback.extract_tb(exc_traceback_obj)
270 for item
in traceback.StackSummary.from_list(extracted_list).format():
274 if isinstance(obj, type):
279 text.append(f
'{obj.__name__} :{msg}')
282 text.append(f
'{obj.__class__.__name__} :{msg}')
283 ALog.log(msg=
''.join(text), obj=obj, line=line, task_name=task_name,
284 LogObject=LogObject
if LogObject
is not None else None, field=LogStatus.Error,
285 text_color=ConsoleColor.FontColor.RED)
287 def template(self, status: LogStatus, *content, **kwargs):
289 _path = log_cache.get_filename(self.
path, self.
max_clear, status)
290 if status == LogStatus.Info:
291 logo_show =
'info_logo_show'
292 elif status == LogStatus.Warn:
293 logo_show =
'warn_logo_show'
294 elif status == LogStatus.Error:
295 logo_show =
'error_logo_show'
297 logo_show =
'info_logo_show'
299 ls = getattr(log_cache, logo_show)
301 setattr(log_cache, logo_show,
True)
304 text = kwargs[
'pure_text'] \
305 if 'pure_text' in kwargs.keys()
else ALog.format_text(status, sys._getframe().f_back.f_lineno, self,
309 def info(self, *content, **kwargs):
328 self.
template(LogStatus.Info, *content, **kwargs)
330 def warn(self, *content, **kwargs):
336 self.
template(LogStatus.Warn, *content, **kwargs)
338 def error(self, *content, **kwargs):
344 self.
template(LogStatus.Error, *content, **kwargs)
355 write(path, *content)
363 _STATIC_TXT = os.path.join(
'', self.
path + end_path)
367 instance = Singleton.createObject(cls)
374 def gen(cls, _object) -> ALog:
375 return _object.log_obj