From 7c7afef7f354756e6fc900053e7aced3e4ef6400 Mon Sep 17 00:00:00 2001 From: Kaipeng Deng Date: Wed, 19 Jun 2019 11:03:48 +0800 Subject: [PATCH] [cherry-pick] Fix logging to release/1.5 (#18026) * fix logging unable. test=develop * unset sys.stdout for stream handler. test=develop * fix newly add basicConfig. test=develop * fix import error. test=release/1.5 --- .../fluid/contrib/slim/core/compressor.py | 6 +-- .../distillation/distillation_strategy.py | 6 +-- .../contrib/slim/nas/controller_server.py | 9 ++-- .../contrib/slim/nas/light_nas_strategy.py | 9 ++-- .../fluid/contrib/slim/nas/search_agent.py | 6 +-- .../contrib/slim/prune/auto_prune_strategy.py | 6 +-- .../contrib/slim/prune/prune_strategy.py | 6 +-- .../mkldnn_post_training_strategy.py | 6 +-- .../quantization/quantization_strategy.py | 6 +-- .../fluid/contrib/slim/searcher/controller.py | 6 +-- .../test_mkldnn_int8_quantization_strategy.py | 6 +-- .../paddle/fluid/contrib/utils/hdfs_utils.py | 6 +-- .../fluid/contrib/utils/lookup_table_utils.py | 8 +-- .../fleet/tests/ctr_dataset_reader.py | 6 +-- .../incubate/fleet/tests/fleet_deep_ctr.py | 6 +-- python/paddle/fluid/io.py | 6 +-- python/paddle/fluid/log_helper.py | 52 +++++++++++++++++++ python/paddle/fluid/net_drawer.py | 4 +- .../fluid/tests/unittests/dist_ctr_reader.py | 6 +-- 19 files changed, 111 insertions(+), 55 deletions(-) create mode 100644 python/paddle/fluid/log_helper.py diff --git a/python/paddle/fluid/contrib/slim/core/compressor.py b/python/paddle/fluid/contrib/slim/core/compressor.py index f6c15524202..2627f7f004b 100644 --- a/python/paddle/fluid/contrib/slim/core/compressor.py +++ b/python/paddle/fluid/contrib/slim/core/compressor.py @@ -19,6 +19,7 @@ from .... import io from .... import profiler from .... import scope_guard from ....data_feeder import DataFeeder +from ....log_helper import get_logger from ..graph import * from .config import ConfigFactory import numpy as np @@ -33,9 +34,8 @@ import traceback __all__ = ['Context', 'Compressor'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') def cached_reader(reader, sampled_rate, cache_path, cached_id): diff --git a/python/paddle/fluid/contrib/slim/distillation/distillation_strategy.py b/python/paddle/fluid/contrib/slim/distillation/distillation_strategy.py index d8e08c3ebef..42389079f8d 100644 --- a/python/paddle/fluid/contrib/slim/distillation/distillation_strategy.py +++ b/python/paddle/fluid/contrib/slim/distillation/distillation_strategy.py @@ -14,14 +14,14 @@ from ..core.strategy import Strategy from ....framework import Program, Variable, program_guard +from ....log_helper import get_logger from .... import Executor import logging __all__ = ['DistillationStrategy'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class DistillationStrategy(Strategy): diff --git a/python/paddle/fluid/contrib/slim/nas/controller_server.py b/python/paddle/fluid/contrib/slim/nas/controller_server.py index d6091007c1d..65cfbd7d86f 100644 --- a/python/paddle/fluid/contrib/slim/nas/controller_server.py +++ b/python/paddle/fluid/contrib/slim/nas/controller_server.py @@ -15,13 +15,14 @@ import logging import socket from threading import Thread +from ....log_helper import get_logger __all__ = ['ControllerServer'] -logging.basicConfig( - format='ControllerServer-%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, + logging.INFO, + fmt='ControllerServer-%(asctime)s-%(levelname)s: %(message)s') class ControllerServer(object): diff --git a/python/paddle/fluid/contrib/slim/nas/light_nas_strategy.py b/python/paddle/fluid/contrib/slim/nas/light_nas_strategy.py index f5290a49b11..f5e9ca5ec42 100644 --- a/python/paddle/fluid/contrib/slim/nas/light_nas_strategy.py +++ b/python/paddle/fluid/contrib/slim/nas/light_nas_strategy.py @@ -16,6 +16,7 @@ from ..graph import GraphWrapper from .controller_server import ControllerServer from .search_agent import SearchAgent from ....executor import Executor +from ....log_helper import get_logger import re import logging import functools @@ -24,10 +25,10 @@ from .lock import lock, unlock __all__ = ['LightNASStrategy'] -logging.basicConfig( - format='LightNASStrategy-%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, + logging.INFO, + fmt='LightNASStrategy-%(asctime)s-%(levelname)s: %(message)s') class LightNASStrategy(Strategy): diff --git a/python/paddle/fluid/contrib/slim/nas/search_agent.py b/python/paddle/fluid/contrib/slim/nas/search_agent.py index 3d756c95bf4..4f32c46999e 100644 --- a/python/paddle/fluid/contrib/slim/nas/search_agent.py +++ b/python/paddle/fluid/contrib/slim/nas/search_agent.py @@ -14,12 +14,12 @@ import logging import socket +from ....log_helper import get_logger __all__ = ['SearchAgent'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class SearchAgent(object): diff --git a/python/paddle/fluid/contrib/slim/prune/auto_prune_strategy.py b/python/paddle/fluid/contrib/slim/prune/auto_prune_strategy.py index 2f29385870d..f9dce622da2 100644 --- a/python/paddle/fluid/contrib/slim/prune/auto_prune_strategy.py +++ b/python/paddle/fluid/contrib/slim/prune/auto_prune_strategy.py @@ -17,12 +17,12 @@ import re import logging import functools import copy +from ....log_helper import get_logger __all__ = ['AutoPruneStrategy'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class AutoPruneStrategy(PruneStrategy): diff --git a/python/paddle/fluid/contrib/slim/prune/prune_strategy.py b/python/paddle/fluid/contrib/slim/prune/prune_strategy.py index 8bdcd8a90f9..6f430bc9e2f 100644 --- a/python/paddle/fluid/contrib/slim/prune/prune_strategy.py +++ b/python/paddle/fluid/contrib/slim/prune/prune_strategy.py @@ -15,6 +15,7 @@ from ..core.strategy import Strategy from ..graph import VarWrapper, OpWrapper, GraphWrapper from ....framework import Program, program_guard, Parameter +from ....log_helper import get_logger from .... import layers import prettytable as pt import numpy as np @@ -28,9 +29,8 @@ import sys __all__ = ['SensitivePruneStrategy', 'UniformPruneStrategy', 'PruneStrategy'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class PruneStrategy(Strategy): diff --git a/python/paddle/fluid/contrib/slim/quantization/mkldnn_post_training_strategy.py b/python/paddle/fluid/contrib/slim/quantization/mkldnn_post_training_strategy.py index 6d3a1cf3e82..dcaabfadedf 100644 --- a/python/paddle/fluid/contrib/slim/quantization/mkldnn_post_training_strategy.py +++ b/python/paddle/fluid/contrib/slim/quantization/mkldnn_post_training_strategy.py @@ -18,12 +18,12 @@ import six import numpy as np from .... import core from ..core.strategy import Strategy +from ....log_helper import get_logger __all__ = ['MKLDNNPostTrainingQuantStrategy'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class MKLDNNPostTrainingQuantStrategy(Strategy): diff --git a/python/paddle/fluid/contrib/slim/quantization/quantization_strategy.py b/python/paddle/fluid/contrib/slim/quantization/quantization_strategy.py index 12c1ce98992..c3d977f708f 100644 --- a/python/paddle/fluid/contrib/slim/quantization/quantization_strategy.py +++ b/python/paddle/fluid/contrib/slim/quantization/quantization_strategy.py @@ -21,14 +21,14 @@ from .... import core from ....compiler import CompiledProgram from ....compiler import BuildStrategy from ....framework import IrGraph, Variable, Program +from ....log_helper import get_logger from ..core.strategy import Strategy from .quantization_pass import * __all__ = ['QuantizationStrategy'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class QuantizationStrategy(Strategy): diff --git a/python/paddle/fluid/contrib/slim/searcher/controller.py b/python/paddle/fluid/contrib/slim/searcher/controller.py index 67830d34e1d..7072dc73746 100644 --- a/python/paddle/fluid/contrib/slim/searcher/controller.py +++ b/python/paddle/fluid/contrib/slim/searcher/controller.py @@ -17,12 +17,12 @@ import numpy as np import copy import math import logging +from ....log_helper import get_logger __all__ = ['EvolutionaryController', 'SAController'] -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class EvolutionaryController(object): diff --git a/python/paddle/fluid/contrib/slim/tests/test_mkldnn_int8_quantization_strategy.py b/python/paddle/fluid/contrib/slim/tests/test_mkldnn_int8_quantization_strategy.py index c7429af5ffb..1c41a316a62 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_mkldnn_int8_quantization_strategy.py +++ b/python/paddle/fluid/contrib/slim/tests/test_mkldnn_int8_quantization_strategy.py @@ -24,10 +24,10 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.contrib.slim.core import Compressor +from paddle.fluid.log_helper import get_logger -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') def parse_args(): diff --git a/python/paddle/fluid/contrib/utils/hdfs_utils.py b/python/paddle/fluid/contrib/utils/hdfs_utils.py index 35ddf97ff23..1bfc966de88 100644 --- a/python/paddle/fluid/contrib/utils/hdfs_utils.py +++ b/python/paddle/fluid/contrib/utils/hdfs_utils.py @@ -24,12 +24,12 @@ import copy import errno import logging +from paddle.fluid.log_helper import get_logger __all__ = ["HDFSClient", "multi_download", "multi_upload"] -logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s') -_logger = logging.getLogger("hdfs_utils") -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') class HDFSClient(object): diff --git a/python/paddle/fluid/contrib/utils/lookup_table_utils.py b/python/paddle/fluid/contrib/utils/lookup_table_utils.py index a127f5b11b7..b15ee94f635 100644 --- a/python/paddle/fluid/contrib/utils/lookup_table_utils.py +++ b/python/paddle/fluid/contrib/utils/lookup_table_utils.py @@ -22,15 +22,17 @@ import paddle from paddle.fluid import core from paddle.fluid import io from paddle.fluid import Program +from paddle.fluid.log_helper import get_logger __all__ = [ "load_persistables_for_increment", "load_persistables_for_inference", "convert_dist_to_sparse_program" ] -logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s') -_logger = logging.getLogger("lookup_table_utils") -_logger.setLevel(logging.INFO) +_logger = get_logger( + 'lookup_table_utils', + logging.INFO, + fmt='%(asctime)s-%(levelname)s: %(message)s') model_filename = "__model__" lookup_table_dir = "__lookup_table__" diff --git a/python/paddle/fluid/incubate/fleet/tests/ctr_dataset_reader.py b/python/paddle/fluid/incubate/fleet/tests/ctr_dataset_reader.py index ace4b01144b..32ba0e512f5 100644 --- a/python/paddle/fluid/incubate/fleet/tests/ctr_dataset_reader.py +++ b/python/paddle/fluid/incubate/fleet/tests/ctr_dataset_reader.py @@ -20,10 +20,10 @@ import os import paddle import paddle.fluid.incubate.data_generator as data_generator +from paddle.fluid.log_helper import get_logger -logging.basicConfig() -logger = logging.getLogger("paddle") -logger.setLevel(logging.INFO) +logger = get_logger( + "paddle", logging.INFO, fmt='%(asctime)s - %(levelname)s - %(message)s') DATA_URL = "http://paddle-ctr-data.bj.bcebos.com/avazu_ctr_data.tgz" DATA_MD5 = "c11df99fbd14e53cd4bfa6567344b26e" diff --git a/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py b/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py index ab57137e117..4f88a763212 100644 --- a/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py +++ b/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py @@ -20,12 +20,12 @@ import paddle.fluid as fluid import paddle.fluid.incubate.fleet.base.role_maker as role_maker from paddle.fluid.incubate.fleet.parameter_server.distributed_transpiler import fleet from paddle.fluid.transpiler.distribute_transpiler import DistributeTranspilerConfig +from paddle.fluid.log_helper import get_logger import ctr_dataset_reader -logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s') -logger = logging.getLogger("fluid") -logger.setLevel(logging.INFO) +logger = get_logger( + "fluid", logging.INFO, fmt='%(asctime)s - %(levelname)s - %(message)s') def parse_args(): diff --git a/python/paddle/fluid/io.py b/python/paddle/fluid/io.py index 56aa6741cd6..b0e1039e200 100644 --- a/python/paddle/fluid/io.py +++ b/python/paddle/fluid/io.py @@ -25,6 +25,7 @@ from paddle.fluid import layers from paddle.fluid.executor import Executor from paddle.fluid.evaluator import Evaluator from paddle.fluid.framework import Program, Parameter, default_main_program, default_startup_program, Variable, program_guard +from paddle.fluid.log_helper import get_logger from . import reader from .reader import * from . import core @@ -35,9 +36,8 @@ __all__ = [ 'load_persistables', 'save_inference_model', 'load_inference_model' ] + reader.__all__ -logging.basicConfig(format='%(asctime)s-%(levelname)s: %(message)s') -_logger = logging.getLogger(__name__) -_logger.setLevel(logging.INFO) +_logger = get_logger( + __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s') def is_parameter(var): diff --git a/python/paddle/fluid/log_helper.py b/python/paddle/fluid/log_helper.py new file mode 100644 index 00000000000..0933d7b9048 --- /dev/null +++ b/python/paddle/fluid/log_helper.py @@ -0,0 +1,52 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +from __future__ import print_function + +import logging + +__all__ = ['get_logger'] + + +def get_logger(name, level, fmt=None): + """ + Get logger from logging with given name, level and format without + setting logging basicConfig. For setting basicConfig in paddle + will disable basicConfig setting after import paddle. + + Args: + name (str): The logger name. + level (logging.LEVEL): The base level of the logger + fmt (str): Format of logger output + + Returns: + logging.Logger: logging logger with given setttings + + Examples: + .. code-block:: python + + logger = log_helper.get_logger(__name__, logging.INFO, + fmt='%(asctime)s-%(levelname)s: %(message)s') + """ + + logger = logging.getLogger(name) + logger.setLevel(level) + handler = logging.StreamHandler() + + if fmt: + formatter = logging.Formatter(fmt=fmt) + handler.setFormatter(formatter) + + logger.addHandler(handler) + return logger diff --git a/python/paddle/fluid/net_drawer.py b/python/paddle/fluid/net_drawer.py index 8485d7d32fe..f991310384f 100644 --- a/python/paddle/fluid/net_drawer.py +++ b/python/paddle/fluid/net_drawer.py @@ -21,9 +21,9 @@ from collections import defaultdict import paddle.fluid.core as core import paddle.fluid.proto.framework_pb2 as framework_pb2 +from paddle.fluid.log_helper import get_logger -logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) +logger = get_logger(__name__, logging.INFO) try: from .graphviz import Graph diff --git a/python/paddle/fluid/tests/unittests/dist_ctr_reader.py b/python/paddle/fluid/tests/unittests/dist_ctr_reader.py index 48a4768782c..c030afdd4ff 100644 --- a/python/paddle/fluid/tests/unittests/dist_ctr_reader.py +++ b/python/paddle/fluid/tests/unittests/dist_ctr_reader.py @@ -16,9 +16,9 @@ import logging import paddle import tarfile -logging.basicConfig() -logger = logging.getLogger("paddle") -logger.setLevel(logging.INFO) +from paddle.fluid.log_helper import get_logger + +logger = get_logger("paddle", logging.INFO) DATA_URL = "http://paddle-ctr-data.bj.bcebos.com/avazu_ctr_data.tgz" DATA_MD5 = "c11df99fbd14e53cd4bfa6567344b26e" -- GitLab