logger.conf
[loggers] keys=root [handlers] keys=fileHandler [formatters] keys=simpleFormatter [logger_root] level=INFOhandlers=fileHandler [handler_fileHandler] class=FileHandler
formatter=simpleFormatter
args=(‘logs/app.log’,’a’) [formatter_simpleFormatter] format=%(asctime)s [%(levelname)s] [%(name)s] %(filename)s.%(funcName)s(%(lineno)d) %(message)s
datefmt=%Y-%m-%d %H:%M:%S
main.py
from logging import getLogger
from logging import config as loggerConfig
import sub
def main():
# initialize logger setting
loggerConfig.fileConfig(“logger.conf”)
logger = getLogger(__name__)
# no logger call
sub.sub_func01()
sub.sub_func02(“hello01”)
# with logger call
sub.sub_func01(logger)
sub.sub_func02(“hello02”, logger)
if __name__ == “__main__”:
main()
sub.py
from logging import getLogger
_logger = getLogger(__name__)
def sub_func01(logger=None):
logger = logger or _logger
logger.info(“log01”)
def sub_func02(str, logger=None):
logger = logger or _logger
logger.info(“log02:{}”.format(str))