Goal: To run an experiment 'A' 10 times, each time with a different input. Experiment A returns a floating point value as its output (let us call it result). All the 10 results of the 10 experiments should be saved in a results file (using logging). There is a lot of logging that needs to be done while running experiment A (each time). All the intermediate outputs of the (10) experiments should be written to (10) different log files.
In other words, If my experiment directory is 'exp_dir'. There should exist a results (log) file after the end of the experiment that saves the output of each of the experiment (only the output needs to be saved, nothing else). During the process of running each experiment I will create sub directories (exp1, exp2 and so on) for each of the 10 experiments within the exp_dir and log the outcomes of the experiments in the corresponding sub directories (experiment 1 outcome should be saved in exp1 sub directory). I am planning to do this in a single python file using for loop.
Could you let me know how to do the logging separately? This is what I tried (instead of 10 experiments I tried on 2 experiments)
import logging
import os
class MyLogger(object):
def __init__(self):
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
def activateLogFile(self, f):
d = os.path.dirname(f)
os.makedirs(d, exist_ok=True)
handler = logging.FileHandler(f)
self.logger.addHandler(handler)
def doLogging(self, fn, info):
self.activateLogFile(fn)
self.logger.info("{i}\n".format(i=info))
def run():
exp_dir = os.getcwd()
myLog.doLogging("{d}/res.log".format(d=exp_dir), "This is where results should be logged")
myLog.logger.propagate = False
for i in range(1, 3):
myLog.doLogging("{d}/exp{i}/info.log".format(i=i, d=exp_dir), "some logging")
myLog.doLogging("{d}/res.log".format(d=exp_dir), "Experiments done")
myLog = MyLogger()
if __name__ == "__main__":
run()
But everything that is logged in exp2 is also logged in exp1 and res.log consists of all the logging (exp1, exp2 also)
I appreciate your help