I'm running python script in which I intend to save tensorflow logs (regardless of their severity) generated during the execution of the script, to a file so that they don't pop up on the console.
Here's the code snippet:
# Setting up Info Logger
import logging, os, sys
from logging.handlers import RotatingFileHandler
def setup_logger(fileName=None, level=logging.NOTSET):
if fileName is None:
fileName = u'./test.log'
FORMAT = '%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s'
logger_file_handler = RotatingFileHandler(fileName, 'w')
logger_file_handler.setLevel(level)
formatter = logging.Formatter(FORMAT)
logger_file_handler.setFormatter(formatter)
tf_logger = logging.getLogger('tensorflow')
tf_logger.addHandler(logger_file_handler)
tf_logger.setLevel(logging.NOTSET)
tf_logger.propagate = False
logging.captureWarnings(True)
logger = logging.getLogger(__name__.split('.')[0])
warnings_logger = logging.getLogger('py.warnings')
logger.addHandler(logger_file_handler)
logger.setLevel(level)
warnings_logger.addHandler(logger_file_handler)
# Redirect all logs to disk
setup_logger(level=logging.DEBUG)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
Problem
1) The script saves all logs except the ones of tensorflow
.
2) If I comment out os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
, then I get the following line on the console:
2019-12-10 11:51:07.501527: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
What I've tried
I found out that the TF_CPP
environment variable sends these logs to sys.stderr
(correct me if I'm wrong) which gets printed on the console.
But it may lead to instability as it'll redirect even the tracebacks to log file, if I try to log stderr
.
What I expect
I intend to get the tensorflow log (like the above one) to appear in the log file.
Kindly give your valuable opinion/solution regarding this problem.
Thanks!
Preetkaran
Python Ver: 3.7.5
Tensorflow Ver: 2.0.0