Separating logging functions

This commit is contained in:
Dylan
2023-06-24 21:23:32 +01:00
parent fd0b209a55
commit eb3a7d9179
3 changed files with 50 additions and 28 deletions

View File

@ -4,6 +4,7 @@ from datetime import date,datetime
import json
import os
import boto3
import vxlogging as log
link_cache_system = config['config']['link_cache']
link_cache = {}
@ -21,16 +22,16 @@ if link_cache_system == "json":
f = open('links.json',)
link_cache = json.load(f)
except json.decoder.JSONDecodeError:
print(" ➤ [ X ] Failed to load cache JSON file. Creating new file.")
log.warn("Failed to load cache JSON file. Creating new file.")
link_cache = {}
except FileNotFoundError:
print(" ➤ [ X ] Failed to load cache JSON file. Creating new file.")
log.warn("Failed to load cache JSON file. Creating new file.")
link_cache = {}
finally:
f.close()
elif link_cache_system == "ram":
link_cache = {}
print("Your link_cache_system is set to 'ram' which is not recommended; this is only intended to be used for tests")
log.warn("Your link_cache_system is set to 'ram' which is not recommended; this is only intended to be used for tests")
elif link_cache_system == "db":
client = pymongo.MongoClient(config['config']['database'], connect=False)
table = config['config']['table']
@ -49,17 +50,17 @@ def addVnfToLinkCache(video_link, vnf):
try:
if link_cache_system == "db":
out = db.linkCache.update_one(vnf)
print(" ➤ [ + ] Link added to DB cache ")
log.debug("Link added to DB cache ")
return True
elif link_cache_system == "json":
link_cache[video_link] = vnf
with open("links.json", "w") as outfile:
json.dump(link_cache, outfile, indent=4, sort_keys=True, default=serializeUnknown)
print(" ➤ [ + ] Link added to JSON cache ")
log.debug("Link added to JSON cache ")
return True
elif link_cache_system == "ram": # FOR TESTS ONLY
link_cache[video_link] = vnf
print(" ➤ [ + ] Link added to RAM cache ")
log.debug("Link added to RAM cache ")
elif link_cache_system == "dynamodb": # pragma: no cover
vnf["ttl"] = int(vnf["ttl"].strftime('%s'))
table = client.Table(DYNAMO_CACHE_TBL)
@ -70,11 +71,10 @@ def addVnfToLinkCache(video_link, vnf):
'ttl':vnf["ttl"]
}
)
print(" ➤ [ + ] Link added to dynamodb cache ")
log.debug("Link added to dynamodb cache ")
return True
except Exception as e:
print(" ➤ [ X ] Failed to add link to DB cache")
print(e)
log.error("Failed to add link to DB cache: "+str(e)+" "+video_link)
return False
def getVnfFromLinkCache(video_link):
@ -83,24 +83,23 @@ def getVnfFromLinkCache(video_link):
if link_cache_system == "db":
collection = db.linkCache
vnf = collection.find_one({'tweet': video_link})
# print(vnf)
if vnf != None:
hits = ( vnf['hits'] + 1 )
print(" ➤ [ ✔ ] Link located in DB cache. " + "hits on this link so far: [" + str(hits) + "]")
log.debug("Link located in DB cache.")
query = { 'tweet': video_link }
change = { "$set" : { "hits" : hits } }
out = db.linkCache.update_one(query, change)
return vnf
else:
print(" ➤ [ X ] Link not in DB cache")
log.debug("Link not in DB cache")
return None
elif link_cache_system == "json":
if video_link in link_cache:
print("Link located in json cache")
log.debug("Link located in json cache")
vnf = link_cache[video_link]
return vnf
else:
print(" ➤ [ X ] Link not in json cache")
log.debug("Link not in json cache")
return None
elif link_cache_system == "dynamodb": # pragma: no cover
table = client.Table(DYNAMO_CACHE_TBL)
@ -110,19 +109,19 @@ def getVnfFromLinkCache(video_link):
}
)
if 'Item' in response:
print("Link located in dynamodb cache")
log.debug("Link located in dynamodb cache")
vnf = response['Item']['vnf']
return vnf
else:
print(" ➤ [ X ] Link not in dynamodb cache")
log.debug("Link not in dynamodb cache")
return None
elif link_cache_system == "ram": # FOR TESTS ONLY
if video_link in link_cache:
print("Link located in json cache")
log.debug("Link located in json cache")
vnf = link_cache[video_link]
return vnf
else:
print(" ➤ [ X ] Link not in cache")
log.debug("Link not in cache")
return None
elif link_cache_system == "none":
return None