Log Queue
Project description
logqueue
Log Queue
Initialize
Declare thread function.
def logger_function(log_dict):
print(log_dict)
# ...
Initialize
import logqueue
logqueue.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 exception
Close and Join
logqueue.close()
logqueue.join()
ex) Use signal.
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)
# ...
logqueue.join()
Logging
logqueue.put_info("start")
# or
logqueue.info("start")
Parameters
use user variables
logqueue.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 variable
Log types
Base '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().
Parse
log_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 start
Parse Formatter
log_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}
Clear
logqueue.clear_log_formatter()
Append Formatters
date_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 Formatter
logqueue.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 formatter
logqueue.set_date_formatter("%y-%m-%d")
date_formatter = logqueue.get_date_formatter()
# %y-%m-%d
logqueue.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}:PID
Keys
class LogDictKey:
log_type
timestamp
process_id
thread_id
cpu_usage
memory_usage
file_name
file_lineno
text
trace_back
class LogFormatterKey:
date
time
timestamp
process_id
process_id_max_length
thread_id
thread_id_max_length
cpu_usage
cpu_usage_max_length
memory_usage
memory_usage_max_length
log_type
log_type_max_length
file_info
file_name
file_name_length
file_lineno
file_lineno_length
text
trace_back
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
File details
Details for the file logqueue-0.0.4.tar.gz
.
File metadata
- Download URL: logqueue-0.0.4.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4093c5e20e726ca8dd8b85877614291b4fc1758bc2f6a9cdec2317c95772ea31 |
|
MD5 | 46c0b2df595ae1cd5f20e412104d89ee |
|
BLAKE2b-256 | c8476cf6eb0cb6a83b1bae7b0b2993483146614d4aced18c55a70d2e110a3d73 |
File details
Details for the file logqueue-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: logqueue-0.0.4-py3-none-any.whl
- Upload date:
- Size: 7.1 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 | 86debef41962f099e4189531eef1dc0b42d2a399cf08b67da312d4268c836554 |
|
MD5 | a2928f23a4c3857df5da6d47af4fafd0 |
|
BLAKE2b-256 | 8a14e3f8b8e3542061c0342e54ca6a58cd8c5a10e6fcc1bd4716d9df1064ba53 |