Log Queue
Project description
logqueue
Log Queue
threading logqueue.get()
Declare thread function.
import logqueue
def log_thread_function():
while True:
log_dict = logqueue.get()
if not log_dict:
break
print(log_dict)
threading
import threading
log_thread = threading.Thread(target=log_thread_function)
log_thread.start()
# ...
log_thread.join()
or
import threading
threading.Thread(target=log_thread_function, daemon=True).start()
# ...
Close
Implement 'close()' when 'daemon=False' in thread.
No need 'close()' when 'daemon=True' in thread.
import signal
import logqueue
def signal_handler(_, frame):
logqueue.close()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGABRT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# ... implement logqueue.get()
log_thread.start()
# ...
log_thread.join()
Flush queue
in log thread function.
def log_thread_function():
while True:
log_dict = logqueue.get()
if not log_dict:
print("Got None : Close")
break
print(log_dict)
print("Flush queue")
while not logqueue.empty():
log_dict = logqueue.get()
print(log_dict)
Logging
logqueue.info("start")
log_dict = logqueue.get()
print(log_dict)
# implement log data to parse, input file, insert database.
output:
{'timestamp': 1700000000.100001,
'process_id': 1234,
'thread_id': 1234567890,
'log_type': 'information',
'file_name': 'test.py',
'file_lineno': 1,
'text': 'start'}
log_str = logqueue.parse(log_dict) # same dict above
print(log_str)
output:
2023-11-15 07:13:20.100001 234:PID 4567890:TID info test.py:1 start
**kwargs
logqueue.info("hi", alarm_meesage="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': 'start',
'alarm_meesage': "alarm",
'input_database': True}
Log function types
logqueue.etc(log_type:str, *objs:object, **kwargs)
logqueue.info(*objs:object, **kwargs)
logqueue.debug(*objs:object, **kwargs)
logqueue.exception(*objs:object, **kwargs)
logqueue.signal(*objs:object, **kwargs)
Parse
Change log string format. (this is default format)
change_log_format(LogKey.date, LogKey.time, LogKey.process_id, LogKey.thread_id, LogKey.log_type, LogKey.file_info, LogKey.text)
== "{date} {time} {process_id} {thread_id} {log_type} {file_info} {text}"
Change log string format
change_log_format(LogKey.date, LogKey.time, LogKey.log_type, LogKey.file_name, LogKey.text)
== "{date} {time} {log_type} {file_name} {text}"
output:
2023-11-15 07:13:20.100001 info test.py start
LogKeys
keys for parse log
LogKey.date
LogKey.time
LogKey.process_id
LogKey.thread_id
LogKey.log_type
LogKey.file_info
LogKey.file_name
LogKey.file_lineno
LogKey.text
LogKey.traceback
option or data keys
OptionKey.timestamp
OptionKey.process_id_length
OptionKey.thread_id_length
OptionKey.log_type_length
OptionKey.file_name_length
OptionKey.file_lineno_length
Each string format can change use keys.
change_date_format(format_str:str) # '%Y-%m-%d'
change_time_format(format_str:str) # '%H:%M:%S.%f'
change_process_id_format(format_str:str)
# f"{{{LogKey.process_id}:0{{{OptionKey.process_id_length}}}d}}:PID"
change_thread_id_format(format_str:str)
# f"{{{LogKey.thread_id}:0{{{OptionKey.thread_id_length}}}d}}:TID"
# ...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logqueue-0.0.1.tar.gz.
File metadata
- Download URL: logqueue-0.0.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88b5505c4794b714a8bfa088d52ac9a534e71cf2bad2775beda17161bc8ff607
|
|
| MD5 |
dbf54b6ea3a887a16c9882418c62e67b
|
|
| BLAKE2b-256 |
060498c46ef718cb6c53c3a2ca8c20678fd2251b9db13291127900e891bbe045
|
File details
Details for the file logqueue-0.0.1-py3-none-any.whl.
File metadata
- Download URL: logqueue-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d45771af26be95c6eae55e9cc505a073ff82fe91ceefaa2997abe4826fa5e418
|
|
| MD5 |
eafb1b03562672dee895527facafd441
|
|
| BLAKE2b-256 |
157ec33725b430d204a796e671f5a4debcd3cd7db6a01e540f5afee9f054811a
|