Running the code below produces two messages:
[INFO] 2017-07-14 21:42:07, printed by func A
[INFO] 2017-07-14 21:42:07, printed by func B
We know that the loggingmodule method formattercan be used to configure the message format. We can configure it to run log messages with the current time or level of detail or with a date, etc. I am wondering if there is a way to include the name of the function from which the log is created. Thus, every time a method log.info()is used , there is a function name and, possibly, even a line number of the code.
import logging
formatter = logging.Formatter("[%(levelname)s] %(asctime)s, %(message)s", "%Y-%m-%d %H:%M:%S")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
log = logging.getLogger(__name__)
log.addHandler(handler)
log.setLevel(logging.DEBUG)
def funct_A():
log.info('printed by func A')
def funct_B():
log.info('printed by func B')
funct_A()
funct_B()
source
share