I'm creating centralised logging. That basically looks like the scripts below.
The logit module will create a file, based on the scripts name which called it. in this case apiCaller.
Originally i had this manually defined when calling logit, however i was searching for away for logit to determine the origin of the log itself.
There are 3 modules at play here:
main.py:
def runAnalytic(script):
importlib.import_module("monitoringScripts."+script["package"]+"."+script["module"], package=None)
packageModule = [{"package":"awesome","module":"apiCaller"}]
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(runAnalytic, packageModule)
apiCaller.py (module above)
from adminTools.logger import logit
logit.create(results[i]["items"][r]["userId"],"apiCaller") #How i currently pass the script name, i want to get rid of this.
logit.py Handles all log requires from all my other scripts (centralised logging)
import sys, logging, logging.handlers, pathlib
#Path for all log files for scriptHub
logdir = str(pathlib.Path(__file__).parent.absolute())
#Creates the log file based on a given name from the script
def create(logMessage,scriptName, level="DEBUG"):
#create filename
log_filename = logdir+"/sysLogs/"+scriptName+".logs"
#Creates the logging object
my_logger = logging.getLogger(scriptName)
my_logger.setLevel(logging.DEBUG)
#Formats the log:
formatter = logging.Formatter('%(asctime)s - %(message)s - %(name)s')
#Gives the log file a limit for 100mb if it goes bigger than this, it will create another file, but keep the old one
handler = logging.handlers.RotatingFileHandler(log_filename, maxBytes=100000000, backupCount=1)
handler.setFormatter(formatter)
#Handlers need to be cleared to stop duplicated logs.
if (my_logger.hasHandlers()):
my_logger.handlers.clear()
my_logger.addHandler(handler)
#creates the log message
my_logger.debug(logMessage)
So, I'm not sure if that helps or hinders you all lol
Essentially, instead of providing logit with the script name, i want logit to get it from the module it's called from. E.g in this case "apiCaller" would be the name that's passed through to logit.