Skip to content

Commit

Permalink
refactoring: move config_pylogger out of the sample app
Browse files Browse the repository at this point in the history
Soon we will be reusing this function in other sample apps, so let's
move it to app_utils.
  • Loading branch information
nzmora committed May 16, 2018
1 parent ba653d9 commit 792e9e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
27 changes: 26 additions & 1 deletion apputils/execution_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
# limitations under the License.
#

"""Log information regarfing the execution environment.
"""Log information regarding the execution environment.
This is helpful if you want to recreate an experiment at a later time, or if
you want to understand the environment in which you execute the training.
"""

import sys
import os
import time
import platform
import logging
import logging.config
import numpy as np
import torch
from git import Repo
Expand Down Expand Up @@ -76,3 +78,26 @@ def log_git_state():
logger.debug("Numpy: %s", np.__version__)
log_git_state()
logger.debug("App args: %s", app_args)


def config_pylogger(log_cfg_file, experiment_name):
"""Configure the Python logger.
For each execution of the application, we'd like to create a unique log directory.
By default this library is named using the date and time of day, to that directories
can be sorted by recency. You can also name yor experiments and prefix the log
directory with this name. This can be useful when accessing experiment data from
TensorBoard, for example.
"""
timestr = time.strftime("%Y.%m.%d-%H%M%S")
filename = timestr if experiment_name is None else experiment_name + '___' + timestr
logdir = './logs' + '/' + filename
if not os.path.exists(logdir):
os.makedirs(logdir)
log_filename = os.path.join(logdir, filename + '.log')
logging.config.fileConfig(os.path.join(os.getcwd(), log_cfg_file), defaults={'logfilename': log_filename})
msglogger = logging.getLogger()
msglogger.logdir = logdir
msglogger.log_filename = log_filename
msglogger.info('Log file for this run: ' + os.path.realpath(log_filename))
return msglogger
19 changes: 2 additions & 17 deletions examples/classifier_compression/compress_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,6 @@
parser.add_argument('--name', '-n', metavar='NAME', default=None, help='Experiment name')


def config_logger(experiment_name):
# The Distiller library writes logs to the Python logger, so we configure it.
timestr = time.strftime("%Y.%m.%d-%H%M%S")
filename = timestr if experiment_name is None else experiment_name + '___' + timestr
logdir = './logs' + '/' + filename
if not os.path.exists(logdir):
os.makedirs(logdir)
log_filename = os.path.join(logdir, filename + '.log')
logging.config.fileConfig(os.path.join(script_dir, 'logging.conf'), defaults={'logfilename': log_filename})
msglogger = logging.getLogger()
msglogger.logdir = logdir
msglogger.log_filename = log_filename
msglogger.info('Log file for this run: ' + os.path.realpath(log_filename))
return msglogger

def check_pytorch_version():
if torch.__version__ < '0.4.0':
print("\nNOTICE:")
Expand All @@ -161,7 +146,7 @@ def main():
global msglogger
check_pytorch_version()
args = parser.parse_args()
msglogger = config_logger(args.name)
msglogger = apputils.config_pylogger('logging.conf', args.name)

# Log various details about the execution environment. It is sometimes useful
# to refer to past experiment executions and this information may be useful.
Expand Down Expand Up @@ -514,7 +499,7 @@ def get_inference_var(tensor):
except Exception as e:
if msglogger is not None:
msglogger.error(traceback.format_exc())
exit(1)
raise e
finally:
if msglogger is not None:
msglogger.info('')
Expand Down

0 comments on commit 792e9e3

Please sign in to comment.