I have a simple Python code that logs messages to a file using the logging module. However, when I run this code in Visual Studio Code (VS Code) outside of the interactive mode, I do not see any logs in the file. Strangely, when I run the same code in the interactive mode, the logs are successfully written to the file. Here is my code:
import logging
# Configure logger for use case 1
logger1 = logging.getLogger('use_case_1')
logger1.setLevel(logging.INFO)
# Create file handler for use case 1
handler1 = logging.FileHandler('./use_case_1.log', 'a')
formatter1 = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler1.setFormatter(formatter1)
logger1.addHandler(handler1)
# Create stream handler for use case 1 (console)
console_handler1 = logging.StreamHandler()
console_handler1.setFormatter(formatter1)
logger1.addHandler(console_handler1)
# Usage example
logger1.info('Log message for use case 1')
handler1.flush() # Flush the file handler
I would like to understand the difference between running the code in VS Code outside the interactive mode versus running it in the interactive mode, and why I am not getting any logs when running outside of the interactive mode. Any insights or suggestions would be greatly appreciated.