0

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")))

1 Answer 1

1

This happens because when you import A all the code inside of the file runs. Specifically the call to basicConfig. The way this function works is that it only has an effect if logging is not configured already, otherwise it does nothing. So when you first import A logging is configured, and the second call to basicConfig in B doesn't have any effect.

This is documented well in the official python docs here.

Edit: File logging per module is easier done by doing something similar to this in each of the files:

import logging
local_logger = logging.getLogger(__name__)
local_logger.addHandler(logging.FileHandler('filename.log', 'a'))
local_logger.setLevel(logging.INFO)
Sign up to request clarification or add additional context in comments.

7 Comments

Ohhh, what do you suggest in that case to do? maybe to have the import inside the If statements in the main()?
Use a different logger for each file. logging.getLogger('fileA'). And don't use basicConfig , since your use case isn't basic.
Yeah, that is the way. I have also extended my answer to show the basic idea.
Awesome! Thanks alot! I didnt knew about this. Guess we learn something new everyday! =D
Also I assume this will start on both scripts if I have these config on? Even though I dont run etc class A?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.