How to redirect TensorFlow logging to a file?

后端 未结 4 863
无人及你
无人及你 2021-02-01 04:14

I\'m using TensorFlow-Slim, which has some useful logging printed out to console by tf.logging. I would like to redirect those loggings to a text file, but couldn\'

4条回答
  •  悲&欢浪女
    2021-02-01 04:45

    import logging
    
    # get TF logger
    log = logging.getLogger('tensorflow')
    log.setLevel(logging.DEBUG)
    
    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
    # create file handler which logs even debug messages
    fh = logging.FileHandler('tensorflow.log')
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    log.addHandler(fh)
    

    My solution is inspired by this thread.

提交回复
热议问题