I am trying to figure out the best practice for using logging in python across multiple modules. I see here: http://docs.python.org/2/howto/logging#logging-from-multiple-modules on how to use the root logger to log across multiple modules. As the link points out though, you can't tell where in your application your messages come from as they all show name "root."
It seems to me there are two options (this assumes my modules are NOT in a package structure, but are just a bunch of modules in the same folder):
1) Use the root logger as in the example, but change the log formatting to include the filename:
# myapp.py
import logging
import mylib
def main():
logging.basicConfig(format='%(asctime)s %(filename)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) #filename='myapp.log',
logging.info('Started')
mylib.do_something()
logging.info('Finished')
if __name__ == '__main__':
main()
#mylib.py
import logging
def do_something():
logging.info('Do something')
In [4]: import myapp
In [5]: myapp.main()
03/06/2014 12:22:07 PM myapp.py INFO: Started
03/06/2014 12:22:07 PM mylib.py INFO: Do something
03/06/2014 12:22:07 PM myapp.py INFO: Finished
2) Use a root logger in the main app but a named logger in the submodules, and use 'name' instead of 'filename' in the log format:
# myapp.py
import logging
import mylib
def main():
#logging.basicConfig(format='%(asctime)s %(filename)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) #filename='myapp.log',
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) #filename='myapp.log',
logging.info('Started')
mylib.do_something()
logging.info('Finished')
if __name__ == '__main__':
main()
#mylib.py
import logging
def do_something():
#logging.info('Do something')
logger = logging.getLogger(__name__)
logger.info('Do something')
In [3]: import myapp
In [4]: myapp.main()
03/06/2014 12:27:29 PM root INFO: Started
03/06/2014 12:27:29 PM mylib INFO: Do something
03/06/2014 12:27:29 PM root INFO: Finished