I noticed that when I call another function it can still access the logger even though it wasn’t passed. For example, the following file always writes to testlogger.log
:
import logging
from datetime import datetime
def printHello():
print('Hello')
logger.info('printed hello')
printGoodbye()
def printGoodbye():
print('Goodbye')
logger.info('printed goodbye')
if __name__ == "__main__":
logger = logging.getLogger(__name__)
FORMAT = '%(name)s - %(levelname)s - %(message)s'
logging.basicConfig(filename='testlogger.log', level=logging.DEBUG, format=FORMAT)
logger.info(datetime.now().strftime('%H:%M %d-%m-%Y.log'))
logger.info('About to call function printHello')
printHello()
logger.info('Now exiting')
Is this how it should be done? Also, the project I’m working on is divided into several modules, which, when completed, will call each other. Should there be a separate log file for each module? If so, is the convention just to name the log file after the script that created it?