From a5da11b6700cba35cf0340527c592ed3112d8f38 Mon Sep 17 00:00:00 2001 From: wuzewu Date: Wed, 13 Feb 2019 15:06:53 +0800 Subject: [PATCH] add logger class --- paddle_hub/logger.py | 90 +++++++++++++++++++++++++++++++++++++++++--- paddle_hub/module.py | 8 ++-- 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/paddle_hub/logger.py b/paddle_hub/logger.py index b0ac2d17..bc44e765 100644 --- a/paddle_hub/logger.py +++ b/paddle_hub/logger.py @@ -17,9 +17,87 @@ from __future__ import division from __future__ import print_function import logging -log_level = logging.DEBUG -logging.basicConfig( - format= - '[%(asctime)-15s] [%(levelname)8s] - %(message)s (%(filename)s:%(lineno)s)') -logger = logging.getLogger('paddle-hub') -logger.setLevel(log_level) + +class Logger: + + PLACEHOLDER = '%' + NOLOG = "NOLOG" + + def __init__(self, name=None): + logging.basicConfig( + format='[%(asctime)-15s] [%(levelname)8s] - %(message)s') + + if not name: + name = "paddle-hub" + + self.logger = logging.getLogger(name) + self.logLevel = "DEBUG" + self.logger.setLevel(self._get_logging_level()) + + def _is_no_log(self): + return self.getLevel() == Logger.NOLOG + + def _get_logging_level(self): + return eval("logging.%s" % self.logLevel) + + def setLevel(self, logLevel): + self.logLevel = logLevel.upper() + if not self._is_no_log(): + _logging_level = eval("logging.%s" % self.logLevel) + self.logger.setLevel(_logging_level) + + def getLevel(self): + return self.logLevel + + def __call__(self, type, msg): + def _get_log_arr(msg): + ph = Logger.PLACEHOLDER + lrspace = 2 + lc = rc = " " * lrspace + tbspace = 1 + msgarr = str(msg).split("\n") + if len(msgarr) == 1: + return msgarr + + maxlen = -1 + for text in msgarr: + if len(text) > maxlen: + maxlen = len(text) + + result = [" ", ph * (maxlen + 2 + lrspace * 2)] + tbline = "%s%s%s" % (ph, " " * (maxlen + lrspace * 2), ph) + for index in range(tbspace): + result.append(tbline) + for text in msgarr: + text = "%s%s%s%s%s%s" % (ph, lc, text, rc, " " * + (maxlen - len(text)), ph) + result.append(text) + for index in range(tbspace): + result.append(tbline) + result.append(ph * (maxlen + 2 + lrspace * 2)) + return result + + if self._is_no_log(): + return + + func = eval("self.logger.%s" % type) + for msg in _get_log_arr(msg): + func(msg) + + def debug(self, msg): + self("debug", msg) + + def info(self, msg): + self("info", msg) + + def error(self, msg): + self("error", msg) + + def warning(self, msg): + self("warning", msg) + + def critical(self, msg): + self("critical", msg) + + +logger = Logger() diff --git a/paddle_hub/module.py b/paddle_hub/module.py index 4cbfc799..cc9130e2 100644 --- a/paddle_hub/module.py +++ b/paddle_hub/module.py @@ -173,10 +173,8 @@ class Module(object): # remove feed fetch operator and variable ModuleUtils.remove_feed_fetch_op(self.inference_program) - # print("inference_program") - # print(self.inference_program) - print("**feed_target_names**\n{}".format(self.feed_target_names)) - print("**fetch_targets**\n{}".format(self.fetch_targets)) + logger.info("**feed_target_names**\n{}".format(self.feed_target_names)) + logger.info("**fetch_targets**\n{}".format(self.fetch_targets)) self._process_parameter() program = self.get_inference_program().clone() @@ -519,7 +517,7 @@ class ModuleUtils(object): def remove_feed_fetch_op(program): """ remove feed and fetch operator and variable for fine-tuning """ - print("remove feed fetch op") + logger.info("remove feed fetch op") block = program.global_block() need_to_remove_op_index = [] for i, op in enumerate(block.ops): -- GitLab