Added logging.

This commit is contained in:
Jonathan Branan 2024-07-11 16:13:45 -05:00
parent 7f2110988a
commit ce67df5b58
7 changed files with 82 additions and 12 deletions

5
.gitignore vendored
View File

@ -1,3 +1,4 @@
config.toml
*config.toml
*.json
__pycache__/
__pycache__/
*.log

View File

@ -10,4 +10,6 @@ query = "SELECT [Id],[Version] FROM [EFTDB].[dbo].[tbl_Schema_Version]"
filename ="./data.json"
[logging]
use_log = true
use_log = true
logLevel = "debug"
logPath = "./inex.log"

View 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

Binary file not shown.

20
inex.py
View File

@ -1,9 +1,10 @@
import pandas
import pyodbc
import os
import logging
import datetime
from tomllib import load
import inexLogging
from inexLogging import inexLog
import inexConnect
import json
import decimal
@ -20,7 +21,7 @@ class Inex:
self.pd = pandas
self.db = pyodbc
self.tm = datetime
self.il = inexLogging
self.il = logging
self.ic = inexConnect
# set config
@ -31,17 +32,28 @@ class Inex:
self.dbPassword = self.config["database"]["password"]
self.dbQuery = self.config["database"]["query"]
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
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)
# 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:
json.dump(self.data, f, cls=Encoder)
# TODO: Move this class to it's own file
class Encoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):

View File

@ -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'
print(connectionString)
connection = lib.connect(connectionString)
if self.useLog:
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()
return cursor
def databaseQuery(cursor, query, args=(), one=False):
cur = cursor.execute(query, args)
def databaseQuery(self, cursor, query, args=(), one=False):
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) \
for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
if self.useLog:
self.il.debug(f"Databse connection closed")
return (r[0] if r else None) if one else r

View File

@ -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)