From 8885274ff36eb3d4c18905aeff0e5e877539d8e7 Mon Sep 17 00:00:00 2001 From: WuHaobo Date: Wed, 22 Apr 2020 17:36:21 +0800 Subject: [PATCH] Decorate logger to display only once --- ppcls/utils/__init__.py | 5 +-- ppcls/utils/logger.py | 68 ++++++++++++----------------------------- 2 files changed, 23 insertions(+), 50 deletions(-) diff --git a/ppcls/utils/__init__.py b/ppcls/utils/__init__.py index c4e3d255..458a54b6 100644 --- a/ppcls/utils/__init__.py +++ b/ppcls/utils/__init__.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import model_zoo -from . import misc from . import logger +from . import misc +from . import model_zoo from .save_load import init_model, save_model from .config import get_config from .misc import AverageMeter + diff --git a/ppcls/utils/logger.py b/ppcls/utils/logger.py index 43cb2f06..b5372243 100644 --- a/ppcls/utils/logger.py +++ b/ppcls/utils/logger.py @@ -11,63 +11,35 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import logging -logging.basicConfig() -DEBUG = logging.DEBUG # 10 -INFO = logging.INFO # 20 -WARN = logging.WARN # 30 -ERROR = logging.ERROR # 40 +import logging +import os +logging.basicConfig(level=logging.INFO) +_logger = logging.getLogger(__name__) -class Logger(object): +def anti_fleet(log): """ - Logger + Because of the fucking Fleet, logs will print multi-times. + So we only display one of them and ignore the others. """ - - def __init__(self, level=DEBUG): - self.init(level) - - def init(self, level=DEBUG): - """ - init - """ - self._logger = logging.getLogger() - self._logger.setLevel(level) - - def info(self, fmt, *args): - """info""" - self._logger.info(fmt, *args) - - def warning(self, fmt, *args): - """warning""" - self._logger.warning(fmt, *args) - - def error(self, fmt, *args): - """error""" - self._logger.error(fmt, *args) - - -_logger = Logger() - - -def init(level=DEBUG): - """init for external""" - _logger.init(level) - - + def wrapper(fmt, *args): + if int(os.getenv("PADDLE_TRAINER_ID", 0)) == 0: + log(fmt, *args) + return wrapper + +@anti_fleet def info(fmt, *args): - """info""" _logger.info(fmt, *args) +@anti_fleet def warning(fmt, *args): - """warn""" _logger.warning(fmt, *args) +@anti_fleet def error(fmt, *args): - """error""" _logger.error(fmt, *args) @@ -86,16 +58,16 @@ def advertise(): """ copyright = "PaddleClas is powered by PaddlePaddle !" - info = "For more info please go to the following website." + ad = "For more info please go to the following website." website = "https://github.com/PaddlePaddle/PaddleClas" - AD_LEN = 6 + len(max([copyright, info, website], key=len)) + AD_LEN = 6 + len(max([copyright, ad, website], key=len)) - _logger.info("\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format( + info("\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format( "=" * (AD_LEN + 4), "=={}==".format(copyright.center(AD_LEN)), "=" * (AD_LEN + 4), "=={}==".format(' ' * AD_LEN), - "=={}==".format(info.center(AD_LEN)), + "=={}==".format(ad.center(AD_LEN)), "=={}==".format(' ' * AD_LEN), "=={}==".format(website.center(AD_LEN)), - "=" * (AD_LEN + 4), )) + "=" * (AD_LEN + 4) )) -- GitLab