What is the methodology for knowing where Python log statements are stored?
i.e. if i do:
import logging
log = logging.getLogger(__name__)
log.info('Test')
Where could I find the logfile? Also, when I call:
logging.getLogger(__name__)
Is that somehow related to how the logger will behave/save?
The
loggingmodule uses handlers attached to loggers to decide how, where, or even if messages ultimately get stored or displayed. You can configureloggingby default to write to a file as well. You should really read the docs, but if you calllogging.basicConfig(filename=log_file_name)wherelog_file_nameis the name of the file you want messages written to (note that you have to do this before anything else inloggingis called at all), then all messages logged to all loggers (unless some further reconfiguration happens later) will be written there. Be aware of what level the logger is set to though; if memory serves,infois below the default log level, so you’d have to includelevel=logging.INFOin the arguments tobasicConfigas well for your message to end up in the file.As to the other part of your question,
logging.getLogger(some_string)returns aLoggerobject, inserted in to the correct position in the hierarchy from the root logger, with the name being the value ofsome_string. Called with no arguments, it returns the root logger.__name__returns the name of the current module, sologging.getLogger(__name__)returns aLoggerobject with the name set to the name of the current module. This is a common pattern used withlogging, as it causes the logger structure to mirror your code’s module structure, which often makes logging messages much more useful when debugging.