diff --git a/python/paddle/fluid/contrib/slim/core/compressor.py b/python/paddle/fluid/contrib/slim/core/compressor.py index f6c155242023010ebc4258b99228c0275db415a1..2627f7f004bc47a5d1b2e5e22d7fe05373ae3ec8 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 d8e08c3ebef50c9808ed818dcf35443dc25f850e..42389079f8de254699b880b7a48b0f30d435c7fc 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 d6091007c1d4eba168c1d8d2387f6d7c7e9c103b..65cfbd7d86ff3783e358f73fff83d89fd98dc01a 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 f5290a49b11c6c610816a348bdb0c948da98b98c..f5e9ca5ec42390617b69514d84897d81e9dc79f6 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 3d756c95bf4374c3ed3c1aa0ad7a59edc55d290c..4f32c46999eeace82359d388f867c461105f46ea 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 2f29385870d456d13dd0ae53d86e11df547f328f..f9dce622da2a3d40bd7aac6a13071856089d3b9a 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 8bdcd8a90f9a3b1fe19449920fdc0d7c4026ecc8..6f430bc9e2fee375c813aeac1e05045b3b42afa4 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 6d3a1cf3e82a10007dbbfd848c90fbe1c88a1f4b..dcaabfadedf32b972f25b5eefde390b1549b5b47 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 12c1ce98992c32caaa300045c4adc918dd88f427..c3d977f708f443951e4d05809531161a9257e7ae 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 67830d34e1d121c514db36bc4dbc655f286d36ed..7072dc73746d1172a9626c60ff50adfe8c9e51b9 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 c7429af5ffb21ba39a108f195866f2345557a0f0..1c41a316a622ea853d03b1c65d3d56a3151971a1 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 35ddf97ff2361d8abd34b16761be99990fc3880d..1bfc966de88e5e816497eea0a7a0d0f2dd667355 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 a127f5b11b7ce681c09ef8d08281a2982e25e9eb..b15ee94f63512dcca91a8aab33d216db0fc24ed5 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 ace4b01144b41d9ac404d086838e759cf279ac28..32ba0e512f54ee142cc02cd6e6a36589daffdd4e 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 ab57137e1171b6c8e2af85bf3335142a18995260..4f88a7632123be8c7e23b377e416986044d99a71 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 56aa6741cd683e4719a310854e0222db64ee671f..b0e1039e200abb6b2a2d353d7176e4cbd4077bf8 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 0000000000000000000000000000000000000000..0933d7b904808a1d5deae1bb5add831cceb0f50e --- /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 8485d7d32fed8554c6d9afd610db230f52497da1..f991310384f769ce091197b16db953e7af94a3c3 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 48a4768782c1b4aa8ff6cfdbda9c8e8eb717d08f..c030afdd4ff9be323ccbc19ebb5e119a8c9f040b 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"