From 1a5df46ecaa7d888ad021b47e037e5dfe47c7e6d Mon Sep 17 00:00:00 2001 From: W1CDN Date: Wed, 12 Jul 2023 12:43:24 -0500 Subject: [PATCH] Add basic logging. --- .gitignore | 1 + config_default.ini | 1 + kiss_and_db.py | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8bfbf57..ab303c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /logs/* config.ini *.db +*.log diff --git a/config_default.ini b/config_default.ini index 647b245..db3bf6f 100644 --- a/config_default.ini +++ b/config_default.ini @@ -17,3 +17,4 @@ kiss_port = 8001 # Development settings (not operational yet) mycall = W1CDN-15 +log_path = aprs_api.log diff --git a/kiss_and_db.py b/kiss_and_db.py index 3b63a78..7174038 100644 --- a/kiss_and_db.py +++ b/kiss_and_db.py @@ -6,6 +6,7 @@ import json import aprslib import configparser import time +import logging def read_config(): config = configparser.ConfigParser() @@ -25,6 +26,8 @@ def main(): # KISS_HOST = os.environ.get("KISS_HOST", "192.168.0.30") # KISS_PORT = os.environ.get("KISS_PORT", "8001") + logging.basicConfig(filename=config['Settings']['log_path'], encoding='utf-8', level=logging.DEBUG) + logging.debug('kiss_and_db.py running') ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port'])) ki.start() @@ -61,11 +64,12 @@ def main(): # Build an INSERT statement based on the fields we have from the frame attrib_names = ', '.join('"%s"' % w for w in a.keys()) attrib_values = ", ".join("?" * len(a.keys())) - - + + try: # Insert data sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")" + logging.debug(sql) conn.execute(sql, list(a.values())) # TODO update stations table here @@ -81,10 +85,11 @@ def main(): VALUES("+station_update+") \ ON CONFLICT([from]) \ DO UPDATE SET count = count + 1;" - print(query3) + #print(query3) + logging.debug(query3) # Insert/update data conn.execute(query3) - + conn.commit() #except: # print("Stations table couldn't be updated.") @@ -94,9 +99,11 @@ def main(): #conn.execute("DELETE FROM frames WHERE created < DATETIME('now', '"+config['Settings']['keep_time']+"')") #conn.commit() except: - print("Error with SQLite!") + #print("Error with SQLite!") + logging.error("Error with SQLite!") except: - print("Frame could not be parsed.") + #print("Frame could not be parsed.") + logging.error("Frame could not be parsed.") conn.close()