Package: logqueue
logqueueLog QueueInitializeDeclare thread function.deflogger_function(log_dict):print(log_dict)# ...Initializeimportlogqueuelogqueue.initialize(logger_function)# ...output:{'log_type': 'exception','timestamp': 1700000000.100001,'process_id': 1234,'thread_id': 1234567890,'cpu_usage': 12, # if exist psutil'memory_usage': 12, # if exist psutil'file_name': 'test.py','file_lineno': 1,'text': 'start','trace_back': 'error'} # if exceptionClose and Joinlogqueue.close()logqueue.join()ex) Use signal.importsignalimportlogqueuedefsignal_handler(_,frame):logqueue.close()signal.signal(signal.SIGINT,signal_handler)signal.signal(signal.SIGABRT,signal_handler)signal.signal(signal.SIGTERM,signal_handler)# ...logqueue.join()Logginglogqueue.put_info("start")# orlogqueue.info("start")Parametersuse user variableslogqueue.info("hi",alarm_message="alarm",input_database=True)log_dict=logqueue.get()print(log_dict)output:{'timestamp': 1700000000.100001,'process_id': 1234,'thread_id': 1234567890,'log_type': 'information','file_name': 'test.py','file_lineno': 1,'text': 'hi','alarm_meesage': "alarm", # user variable'input_database': True} # user variableLog typesBase 'put()'logqueue.put(log_type:str,*objs:object,**kwargs)logqueue.info(*objs:object,**kwargs)logqueue.put_info(*objs:object,**kwargs)logqueue.put(LogType.INFORMATION,*objs:object,**kwargs)logqueue.debug(*objs:object,**kwargs)logqueue.put_debug(*objs:object,**kwargs)logqueue.put(LogType.DEBUG,*objs:object,**kwargs)logqueue.warning(*objs:object,**kwargs)logqueue.put_warning(*objs:object,**kwargs)logqueue.put(LogType.WARNING,*objs:object,**kwargs)logqueue.exception(*objs:object,**kwargs)logqueue.put_exception(*objs:object,**kwargs)logqueue.put(LogType.EXCEPTION,*objs:object,**kwargs)# 'trace_back' into log data. (logqueue.get())logqueue.signal(*objs:object,**kwargs)logqueue.put_signal(*objs:object,**kwargs)logqueue.put(LogType.SIGNAL,*objs:object,**kwargs)# line break when parse().Parselog_str=logqueue.parse(log_dict)print(log_str)output:2023-11-15 07:13:20.100001 12%:CPU 12%:Mem 234:PID 4567890:TID info test.py:1 startParse Formatterlog_formatter=logqueue.get_log_formatter()# default log formatters# {date} {time} {process_id:0{process_id_max_length}d}:PID {thread_id:0{thread_id_max_length}d}:TID {file_name:>{file_name_length}}:{file_lineno:<{file_lineno_length}} {log_type:{log_type_max_length}} {text}Clearlogqueue.clear_log_formatter()Append Formattersdate_formatter=f"{{{logqueue.LogFormatterKey.date}}}"time_formatter=f"{{{logqueue.LogFormatterKey.time}}}"append_log_formatter(date_formatter)append_log_formatter(time_formatter)log_formatter=logqueue.get_log_formatter()# {date} {time}pid_formatter=logqueue.get_process_id_formatter()# == f"{{{logqueue.LogFormatterKey.process_id}:0{{{logqueue.LogFormatterKey.process_id_max_length}}}d}}:PID"file_name_formatter=f"{{{logqueue.LogFormatterKey.file_name}:>{{{logqueue.LogFormatterKey.file_name_length}}}}}"text=logqueue.get_text_formatter()logqueue.append_log_formatters(pid_formatter,file_name_formatter,text)log_formatter=logqueue.get_log_formatter()# {date} {time} {process_id:0{process_id_max_length}d}:PID {file_name:>{file_name_length}} {text}Replace Formatterlogqueue.replace_log_formatter(pid_formatter)log_formatter=logqueue.get_log_formatter()# {date} {time} {file_name:>{file_name_length}}logqueue.replace_log_formatter(time_formatter,pid_formatter)log_formatter=logqueue.get_log_formatter()# {date} {process_id:0{process_id_max_length}d}:PID {file_name:>{file_name_length}}Change each formatterlogqueue.set_date_formatter("%y-%m-%d")date_formatter=logqueue.get_date_formatter()# %y-%m-%dlogqueue.set_process_id_formatter(f"{{{logqueue.LogFormatterKey.process_id}:0{{{logqueue.LogFormatterKey.process_id_max_length}}}d}}:PID")process_id_formatter=logqueue.get_process_id_formatter()# {process_id:0{process_id_max_length}d}:PIDKeysclassLogDictKey:log_typetimestampprocess_idthread_idcpu_usagememory_usagefile_namefile_linenotexttrace_backclassLogFormatterKey:datetimetimestampprocess_idprocess_id_max_lengththread_idthread_id_max_lengthcpu_usagecpu_usage_max_lengthmemory_usagememory_usage_max_lengthlog_typelog_type_max_lengthfile_infofile_namefile_name_lengthfile_linenofile_lineno_lengthtexttrace_back