Im having an issue where currently I use two loggings where each script has its own logging. I have even used filename="Test1/Logs/hello.txt and Test2 for second script so the filename is seperated. My problem is that whenever I run a script, it saves only to one folder and in my case is Test1. It doesn't never logs into Test2 folder even though I run test2.py script which should save to the Test2 folder logs.
What I have done is:
# -------------------------------------------------------------------------
# CLASS A
# -------------------------------------------------------------------------
logging.basicConfig(
filename='{}{}.txt'.format(/Test1/Logs, datetime.now().strftime("%a %d %B %Y %H_%M_%S")),
filemode='a',
level=logging.INFO,
format='[%(asctime)s]:%(levelname)s:%(funcName)s - %(message)s',
datefmt=datetime.now().strftime("%H:%M:%S.%f")[:-3]
)
loggingError = logging.getLogger()
logging.warning("Starting: {}".format(datetime.now().strftime("%a %d-%B-%Y %H:%M:%S")))
class A:
def __init__(self):
self.logging = loggingError
def aW1wb3J0RmlsZXM(self):
self.logging.error(sys.exc_info(), exc_info=True)
# -------------------------------------------------------------------------
# CLASS B
# -------------------------------------------------------------------------
logging.basicConfig(
filename='{}{}.txt'.format(/Test2/Logs, datetime.now().strftime("%a %d %B %Y %H_%M_%S")),
filemode='a',
level=logging.INFO,
format='[%(asctime)s]:%(levelname)s:%(funcName)s - %(message)s',
datefmt=datetime.now().strftime("%H:%M:%S.%f")[:-3]
)
loggingError = logging.getLogger()
logging.warning("Starting: {}".format(datetime.now().strftime("%a %d-%B-%Y %H:%M:%S")))
class B:
def __init__(self):
self.logging = loggingError
def aW1wb3J0RmlsZXM(self):
self.logging.error(sys.exc_info(), exc_info=True)
import A
import B
def main():
if True:
B.main()
break
if False:
A.main()
break
if __name__ == "__main__":
try:
main()
except Exception as e:
print(e)
as you can see the only difference between loggings are the folder name filename and yet even if I run Class B it still saves to Class A folder. What is the reason for that?
UPDATED TO EACH SCRIPT:
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler('{}{}.txt'.format(logsPath, datetime.now().strftime("%a %d %B %Y %H_%M_%S")))
formatter = logging.Formatter('[%(asctime)s]:%(levelname)s:%(funcName)s - %(message)s', "%d-%m-%Y %H:%M:%S")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.warning("Starting: {}".format(datetime.now().strftime("%a %d-%B-%Y %H:%M:%S")))