add message search function

This commit is contained in:
2024-08-10 23:24:37 +02:00
parent 1609b8fb1a
commit 1bbcb3d840
2 changed files with 17 additions and 4 deletions

View File

@@ -7,7 +7,7 @@ import shutil
class LogHandlerSQLite(logging.Handler): class LogHandlerSQLite(logging.Handler):
"""logging handler for sqlite3""" """logging handler for sqlite3"""
__AUTHOR__ = 'anima' __AUTHOR__ = 'anima'
__VERSION__ = '1.0.0' __VERSION__ = '1.1.0'
def __init__(self, level: int | str = 0, dbfile: str = 'logs.db') -> None: def __init__(self, level: int | str = 0, dbfile: str = 'logs.db') -> None:
"""logging handler for sqlite3 """logging handler for sqlite3
@@ -119,6 +119,17 @@ class LogHandlerSQLite(logging.Handler):
return self.__db_query('selectLogsByPID', [pid]) return self.__db_query('selectLogsByPID', [pid])
else: return None else: return None
def search_log(self, search: str) -> list[tuple] | None:
"""search str in log message
:param search: search string for message
:type search: str
:return: all matched log entrys or None
:rtype: list[tuple] | None
"""
return self.__db_query('selectLogsLikeMessage', [f'%{search}%'])
##> logging methods ##> logging methods
def emit(self, record: logging.LogRecord) -> None: def emit(self, record: logging.LogRecord) -> None:
"""save log to db """save log to db
@@ -148,5 +159,3 @@ if __name__ == "__main__":
log.error('Here is a info') log.error('Here is a info')
print(logDB.get_log_by_pid()) print(logDB.get_log_by_pid())
t = LogHandlerSQLite()

View File

@@ -0,0 +1,4 @@
SELECT *
FROM logs
WHERE message like :1
;