Added logging.
This commit is contained in:
parent
7f2110988a
commit
ce67df5b58
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
config.toml
|
*config.toml
|
||||||
*.json
|
*.json
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
*.log
|
@ -10,4 +10,6 @@ query = "SELECT [Id],[Version] FROM [EFTDB].[dbo].[tbl_Schema_Version]"
|
|||||||
filename ="./data.json"
|
filename ="./data.json"
|
||||||
|
|
||||||
[logging]
|
[logging]
|
||||||
use_log = true
|
use_log = true
|
||||||
|
logLevel = "debug"
|
||||||
|
logPath = "./inex.log"
|
15
executable/config.toml.example
Normal file
15
executable/config.toml.example
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[database]
|
||||||
|
driver = "ODBC Driver 18 for SQL Server"
|
||||||
|
server = "192.168.x.x"
|
||||||
|
database = "EFTDB"
|
||||||
|
user = "a"
|
||||||
|
password = "a"
|
||||||
|
query = "SELECT [Id],[Version] FROM [EFTDB].[dbo].[tbl_Schema_Version]"
|
||||||
|
|
||||||
|
[output]
|
||||||
|
filename ="./data.json"
|
||||||
|
|
||||||
|
[logging]
|
||||||
|
use_log = true
|
||||||
|
logLevel = "debug"
|
||||||
|
logPath = "./inex.log"
|
BIN
executable/inex.exe
Normal file
BIN
executable/inex.exe
Normal file
Binary file not shown.
20
inex.py
20
inex.py
@ -1,9 +1,10 @@
|
|||||||
import pandas
|
import pandas
|
||||||
import pyodbc
|
import pyodbc
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
from tomllib import load
|
from tomllib import load
|
||||||
import inexLogging
|
from inexLogging import inexLog
|
||||||
import inexConnect
|
import inexConnect
|
||||||
import json
|
import json
|
||||||
import decimal
|
import decimal
|
||||||
@ -20,7 +21,7 @@ class Inex:
|
|||||||
self.pd = pandas
|
self.pd = pandas
|
||||||
self.db = pyodbc
|
self.db = pyodbc
|
||||||
self.tm = datetime
|
self.tm = datetime
|
||||||
self.il = inexLogging
|
self.il = logging
|
||||||
self.ic = inexConnect
|
self.ic = inexConnect
|
||||||
|
|
||||||
# set config
|
# set config
|
||||||
@ -31,17 +32,28 @@ class Inex:
|
|||||||
self.dbPassword = self.config["database"]["password"]
|
self.dbPassword = self.config["database"]["password"]
|
||||||
self.dbQuery = self.config["database"]["query"]
|
self.dbQuery = self.config["database"]["query"]
|
||||||
self.outputFile = self.config["output"]["filename"]
|
self.outputFile = self.config["output"]["filename"]
|
||||||
|
self.useLog = self.config["logging"]["useLog"]
|
||||||
|
self.logPath = self.config["logging"]["logPath"]
|
||||||
|
self.logLevel = self.config["logging"]["logLevel"]
|
||||||
|
|
||||||
|
#Setup logging
|
||||||
|
inexLog(self)
|
||||||
|
|
||||||
# create the connection to the database
|
# create the connection to the database
|
||||||
self.cursor = self.ic.connectDatabase(self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
|
self.cursor = self.ic.connectDatabase(self, self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
|
||||||
|
|
||||||
self.data = self.ic.databaseQuery(self.cursor, self.dbQuery)
|
self.data = self.ic.databaseQuery(self, self.cursor, self.dbQuery)
|
||||||
|
|
||||||
# print(self.data)
|
# print(self.data)
|
||||||
|
|
||||||
|
# TODO: move this to its own function
|
||||||
|
if self.useLog:
|
||||||
|
self.il.warning(f"Writing to '{self.outputFile}'.")
|
||||||
|
|
||||||
with open(self.outputFile, "w") as f:
|
with open(self.outputFile, "w") as f:
|
||||||
json.dump(self.data, f, cls=Encoder)
|
json.dump(self.data, f, cls=Encoder)
|
||||||
|
|
||||||
|
# TODO: Move this class to it's own file
|
||||||
class Encoder(json.JSONEncoder):
|
class Encoder(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
if isinstance(o, decimal.Decimal):
|
if isinstance(o, decimal.Decimal):
|
||||||
|
@ -1,13 +1,42 @@
|
|||||||
def connectDatabase(lib, driver, server, database, user, password):
|
def connectDatabase(self, lib, driver, server, database, user, password):
|
||||||
|
"""Connects to the database. Requires a windows driver to do so.
|
||||||
|
Typically there is one installed by default"""
|
||||||
|
|
||||||
connectionString = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={user};PWD={password};TrustServerCertificate=yes'
|
connectionString = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={user};PWD={password};TrustServerCertificate=yes'
|
||||||
print(connectionString)
|
if self.useLog:
|
||||||
connection = lib.connect(connectionString)
|
self.il.debug(f"Connection String: connectionString")
|
||||||
|
self.il.info(f"Connecting to {database}@{server} with driver[{driver}].")
|
||||||
|
try:
|
||||||
|
connection = lib.connect(connectionString)
|
||||||
|
except lib.Error as ex:
|
||||||
|
sqlstate = ex.args[1]
|
||||||
|
if self.useLog:
|
||||||
|
self.il.error(sqlstate)
|
||||||
|
if self.useLog:
|
||||||
|
self.il.debug(f"Connected.")
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
return cursor
|
return cursor
|
||||||
|
|
||||||
def databaseQuery(cursor, query, args=(), one=False):
|
def databaseQuery(self, cursor, query, args=(), one=False):
|
||||||
cur = cursor.execute(query, args)
|
if self.useLog:
|
||||||
|
self.il.debug(f"Query:")
|
||||||
|
self.il.debug(query)
|
||||||
|
self.il.info(f"Sending query:{query[0:20]}...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
cur = cursor.execute(query, args)
|
||||||
|
except cur.Error as ex:
|
||||||
|
sqlstate = ex.args[1]
|
||||||
|
if self.useLog:
|
||||||
|
self.il.error(sqlstate)
|
||||||
|
|
||||||
|
if self.useLog:
|
||||||
|
self.il.debug(f"Processing database response...")
|
||||||
r = [dict((cur.description[i][0], value) \
|
r = [dict((cur.description[i][0], value) \
|
||||||
for i, value in enumerate(row)) for row in cur.fetchall()]
|
for i, value in enumerate(row)) for row in cur.fetchall()]
|
||||||
|
|
||||||
cur.connection.close()
|
cur.connection.close()
|
||||||
|
if self.useLog:
|
||||||
|
self.il.debug(f"Databse connection closed")
|
||||||
return (r[0] if r else None) if one else r
|
return (r[0] if r else None) if one else r
|
@ -0,0 +1,11 @@
|
|||||||
|
def inexLog(self):
|
||||||
|
"""Setting up the log file, if self.useLog is set to true and self.logLevel is DEBUG, INFO, WARNING or ERROR"""
|
||||||
|
if self.useLog:
|
||||||
|
if self.logLevel.lower() == 'debug':
|
||||||
|
self.il.basicConfig(filename=self.logPath, format='%(asctime)s:%(levelname)s:%(message)s', encoding='utf-8', datefmt='%m/%d/%Y %I:%M:%S %p',level=self.il.DEBUG)
|
||||||
|
elif self.logLevel.lower() == 'info':
|
||||||
|
self.il.basicConfig(filename=self.logPath, format='%(asctime)s:%(levelname)s:%(message)s', encoding='utf-8', datefmt='%m/%d/%Y %I:%M:%S %p',level=self.il.INFO)
|
||||||
|
elif self.logLevel.lower() == 'warning':
|
||||||
|
self.il.basicConfig(filename=self.logPath, format='%(asctime)s:%(levelname)s:%(message)s', encoding='utf-8', datefmt='%m/%d/%Y %I:%M:%S %p',level=self.il.WARNING)
|
||||||
|
elif self.logLevel.lower() == 'error':
|
||||||
|
self.il.basicConfig(filename=self.logPath, format='%(asctime)s:%(levelname)s:%(message)s', encoding='utf-8', datefmt='%m/%d/%Y %I:%M:%S %p',level=self.il.ERROR)
|
Loading…
x
Reference in New Issue
Block a user