Need to work on running update queries, that combine plsql and normal sql statements and potentially multiple extracts for one sql file, to make multiple files, and probably to also export to xlsx file format.
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
import logging
|
|
import os
|
|
import sqlite3
|
|
|
|
DEFAULT_SEPARATOR = '|'
|
|
DEFAULT_DATA_TYPE = 'TEXT'
|
|
|
|
|
|
#WARNING: attributes must be choosen from https://docs.python.org/3/library/logging.html#formatter-objects
|
|
DEFAULT_ATTRIBUTES_LIST = ['asctime', 'levelname', 'name', 'message']
|
|
|
|
|
|
class SQLiteHandler(logging.Handler):
|
|
"""
|
|
Logging handler for SQLite
|
|
Based on Yarin Kessler's sqlite_handler.py https://gist.github.com/ykessler/2662203#file_sqlite_handler.py
|
|
"""
|
|
|
|
def __init__(self, database:str = "local.db", table = "log", attributes_list=None):
|
|
"""
|
|
SQLiteHandler class constructor
|
|
Parameters:
|
|
self: instance of the class
|
|
database: database
|
|
table: log table name
|
|
attributes_list: log table columns
|
|
Returns:
|
|
None
|
|
"""
|
|
#super(SQLiteHandler, self).__init__() # for python 2.X
|
|
super().__init__() # for python 3.X
|
|
if attributes_list is None:
|
|
attributes_list = DEFAULT_ATTRIBUTES_LIST
|
|
self.database = database
|
|
self.table = table
|
|
self.attributes = attributes_list
|
|
|
|
# Create table if needed
|
|
create_table_sql = 'CREATE TABLE IF NOT EXISTS ' + self.table + ' (' + ((' ' + DEFAULT_DATA_TYPE + ', ').join(self.attributes)) + ' ' + DEFAULT_DATA_TYPE + ');'
|
|
#print(create_table_sql)
|
|
conn = sqlite3.connect(self.database)
|
|
conn.execute(create_table_sql)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def emit(self, record):
|
|
"""
|
|
Save the log record
|
|
Parameters:
|
|
self: instance of the class
|
|
record: log record to be saved
|
|
Returns:
|
|
None
|
|
"""
|
|
# Use default formatting if no formatter is set
|
|
self.format(record)
|
|
|
|
#print(record.__dict__)
|
|
record_values = [record.__dict__[k] for k in self.attributes]
|
|
str_record_values = ', '.join("'{0}'".format(v.replace("'", '').replace('"', '').replace('\n', ' ')) for v in record_values)
|
|
#print(str_record_values)
|
|
|
|
insert_sql = 'INSERT INTO ' + self.table + ' (' + (', '.join(self.attributes)) + ') VALUES (' + str_record_values + ');'
|
|
#print(insert_sql)
|
|
conn = sqlite3.connect(self.database)
|
|
conn.execute(insert_sql)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def init_logger() -> logging.Logger:
|
|
# changing level we can change frome what level we want to log the events
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
attributes_list = ['asctime', 'levelname', 'message']
|
|
formatter = logging.Formatter('%(' + ((')s' + DEFAULT_SEPARATOR + '%(').join(attributes_list)) + ')s')
|
|
|
|
console_handler = logging.StreamHandler()
|
|
console_handler.setLevel(logging.DEBUG)
|
|
console_handler.setFormatter(formatter)
|
|
|
|
sql_handler = SQLiteHandler()
|
|
sql_handler.setLevel(logging.INFO)
|
|
sql_handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(console_handler)
|
|
logger.addHandler(sql_handler)
|
|
|
|
return logger |