Get Output From the logging Module in IPython Notebook

前端 未结 9 1579
悲哀的现实
悲哀的现实 2020-11-30 19:19

When I running the following inside IPython Notebook I don\'t see any output:

import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug(\"test\")         


        
9条回答
  •  一整个雨季
    2020-11-30 19:52

    I wanted a simple and straightforward answer to this, with nicely styled output so here's my recommendation

    import sys
    import logging
    
    logging.basicConfig(
        format='%(asctime)s [%(levelname)s] %(name)s - %(message)s',
        level=logging.INFO,
        datefmt='%Y-%m-%d %H:%M:%S',
        stream=sys.stdout,
    )
    log = logging.getLogger('notebook')
    

    Then you can use log.info() or any of the other logging levels anywhere in your notebook with output that looks like this

    2020-10-28 17:07:08 [INFO] notebook - Hello world
    2020-10-28 17:12:22 [INFO] notebook - More info here
    2020-10-28 17:12:22 [INFO] notebook - And some more
    

提交回复
热议问题