Logging separately by module
Question:
I have a python module that creates a logger; I call this in my main and log all messages in the main. How do I access this logger in a different module?
My code:
my_logger.py
import logging
import logging.handlers
def setup_logging(file_name):
logger = logging.getLogger(file_name)
logger.setLevel(logging.DEBUG)
runlog_formatter = logging.Formatter('%(asctime)s %(name)s line-%(lineno)dtt%(levelname)st%(message)s')
runlog_handler = logging.handlers.TimedRotatingFileHandler(file_name, when='midnight', backupCount=30)
runlog_handler.setLevel(logging.DEBUG)
runlog_handler.setFormatter(runlog_formatter)
logger.addHandler(runlog_handler)
return logger
TestCode.py:
import my_logger
import someother_module
if __name__ == '__main__':
logger = base_logger.setup_logging('testStatus.log')
logger.info('--- SCRIPT RUNNING ---n')
param1 = 'parameter1'
param2 = 'parameter2'
logger.info(someother_module.get_status(param1, param2))
logger.info('--- SCRIPT EXITING ---n')
someother_module.py
def get_status(param1, param2):
print('Parameter1: %s' % param1) # here, I would like to use the logger created in main
print('Parameter2: %s' % param2)
Can I pass the logger from main to the somether_module’s get_status?
Answers:
You’ll often find the the official docs recommend using:
# at module top-level
logger = logging.getLogger(__name__)
Because you’ll get loggers that communicate exactly where they’re logging from (foo_module.__name__
produces the full module path).
By the same mechanism, even if you’ve given it some different name, e.g. in your case logging.getLogger('testStatus.log')
, you can always fetch that same logger by passing in that same name, even if in a different module.
I have a python module that creates a logger; I call this in my main and log all messages in the main. How do I access this logger in a different module?
My code:
my_logger.py
import logging
import logging.handlers
def setup_logging(file_name):
logger = logging.getLogger(file_name)
logger.setLevel(logging.DEBUG)
runlog_formatter = logging.Formatter('%(asctime)s %(name)s line-%(lineno)dtt%(levelname)st%(message)s')
runlog_handler = logging.handlers.TimedRotatingFileHandler(file_name, when='midnight', backupCount=30)
runlog_handler.setLevel(logging.DEBUG)
runlog_handler.setFormatter(runlog_formatter)
logger.addHandler(runlog_handler)
return logger
TestCode.py:
import my_logger
import someother_module
if __name__ == '__main__':
logger = base_logger.setup_logging('testStatus.log')
logger.info('--- SCRIPT RUNNING ---n')
param1 = 'parameter1'
param2 = 'parameter2'
logger.info(someother_module.get_status(param1, param2))
logger.info('--- SCRIPT EXITING ---n')
someother_module.py
def get_status(param1, param2):
print('Parameter1: %s' % param1) # here, I would like to use the logger created in main
print('Parameter2: %s' % param2)
Can I pass the logger from main to the somether_module’s get_status?
You’ll often find the the official docs recommend using:
# at module top-level
logger = logging.getLogger(__name__)
Because you’ll get loggers that communicate exactly where they’re logging from (foo_module.__name__
produces the full module path).
By the same mechanism, even if you’ve given it some different name, e.g. in your case logging.getLogger('testStatus.log')
, you can always fetch that same logger by passing in that same name, even if in a different module.