Skip to content

Commit f50baf2

Browse files
authored
Unify the logger usage (#1581)
Signed-off-by: yiliu30 <[email protected]> Signed-off-by: chensuyue <[email protected]>
1 parent 9a549c3 commit f50baf2

File tree

26 files changed

+95
-111
lines changed

26 files changed

+95
-111
lines changed

.azure-pipelines/scripts/ut/3x/collect_log_3x.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source /neural-compressor/.azure-pipelines/scripts/change_color.sh
22

3-
set -xe
3+
set -e
44
pip install coverage
55
export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.${1}
66
coverage_log="/neural-compressor/log_dir/coverage_log"

neural_compressor/common/__init__.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,34 @@
1313
# limitations under the License.
1414

1515
from neural_compressor.common.utils import (
16-
level,
1716
log,
1817
info,
19-
DEBUG,
2018
debug,
21-
warn,
2219
warning,
2320
error,
2421
fatal,
22+
level,
23+
logger,
24+
Logger,
2525
set_random_seed,
2626
set_workspace,
2727
set_resume_from,
2828
set_tensorboard,
29-
Logger,
30-
logger,
3129
)
3230
from neural_compressor.common.base_config import options
3331

3432

3533
__all__ = [
36-
"level",
37-
"log",
38-
"info",
39-
"DEBUG",
4034
"debug",
41-
"warn",
42-
"warning",
4335
"error",
4436
"fatal",
45-
"options",
46-
"Logger",
37+
"info",
38+
"level",
4739
"logger",
40+
"log",
41+
"warning",
42+
"Logger",
43+
"options",
4844
"set_workspace",
4945
"set_random_seed",
5046
"set_resume_from",

neural_compressor/common/base_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from itertools import product
2525
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
2626

27-
from neural_compressor.common import Logger
27+
from neural_compressor.common import logger
2828
from neural_compressor.common.utils import (
2929
BASE_CONFIG,
3030
COMPOSABLE_CONFIG,
@@ -36,8 +36,6 @@
3636
OP_NAME_OR_MODULE_TYPE,
3737
)
3838

39-
logger = Logger().get_logger()
40-
4139
__all__ = [
4240
"options",
4341
"register_config",

neural_compressor/common/base_tuning.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import uuid
1919
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union
2020

21-
from neural_compressor.common import Logger
21+
from neural_compressor.common import logger
2222
from neural_compressor.common.base_config import BaseConfig, ComposableConfig
2323

24-
logger = Logger().get_logger()
25-
2624
__all__ = [
2725
"Evaluator",
2826
"TuningConfig",

neural_compressor/common/utils/logger.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
import logging
2020
import os
2121

22+
__all__ = [
23+
"debug",
24+
"error",
25+
"fatal",
26+
"info",
27+
"level",
28+
"logger",
29+
"log",
30+
"warning",
31+
"Logger",
32+
]
33+
2234

2335
class Logger(object):
2436
"""Logger class."""
@@ -67,68 +79,60 @@ def _pretty_dict(value, indent=0):
6779
return repr(value)
6880

6981

70-
level = Logger().get_logger().level
71-
DEBUG = logging.DEBUG
72-
73-
7482
def log(level, msg, *args, **kwargs):
7583
"""Output log with the level as a parameter."""
7684
if isinstance(msg, dict):
7785
for _, line in enumerate(_pretty_dict(msg).split("\n")):
78-
Logger().get_logger().log(level, line, *args, **kwargs)
86+
Logger().get_logger().log(level, line, *args, **kwargs, stacklevel=2)
7987
else:
80-
Logger().get_logger().log(level, msg, *args, **kwargs)
88+
Logger().get_logger().log(level, msg, *args, **kwargs, stacklevel=2)
8189

8290

8391
def debug(msg, *args, **kwargs):
8492
"""Output log with the debug level."""
8593
if isinstance(msg, dict):
8694
for _, line in enumerate(_pretty_dict(msg).split("\n")):
87-
Logger().get_logger().debug(line, *args, **kwargs)
95+
Logger().get_logger().debug(line, *args, **kwargs, stacklevel=2)
8896
else:
89-
Logger().get_logger().debug(msg, *args, **kwargs)
97+
Logger().get_logger().debug(msg, *args, **kwargs, stacklevel=2)
9098

9199

92100
def error(msg, *args, **kwargs):
93101
"""Output log with the error level."""
94102
if isinstance(msg, dict):
95103
for _, line in enumerate(_pretty_dict(msg).split("\n")):
96-
Logger().get_logger().error(line, *args, **kwargs)
104+
Logger().get_logger().error(line, *args, **kwargs, stacklevel=2)
97105
else:
98-
Logger().get_logger().error(msg, *args, **kwargs)
106+
Logger().get_logger().error(msg, *args, **kwargs, stacklevel=2)
99107

100108

101109
def fatal(msg, *args, **kwargs):
102110
"""Output log with the fatal level."""
103111
if isinstance(msg, dict):
104112
for _, line in enumerate(_pretty_dict(msg).split("\n")):
105-
Logger().get_logger().fatal(line, *args, **kwargs)
113+
Logger().get_logger().fatal(line, *args, **kwargs, stacklevel=2)
106114
else:
107-
Logger().get_logger().fatal(msg, *args, **kwargs)
115+
Logger().get_logger().fatal(msg, *args, **kwargs, stacklevel=2)
108116

109117

110118
def info(msg, *args, **kwargs):
111119
"""Output log with the info level."""
112120
if isinstance(msg, dict):
113121
for _, line in enumerate(_pretty_dict(msg).split("\n")):
114-
Logger().get_logger().info(line, *args, **kwargs)
122+
Logger().get_logger().info(line, *args, **kwargs, stacklevel=2)
115123
else:
116-
Logger().get_logger().info(msg, *args, **kwargs)
117-
118-
119-
def warn(msg, *args, **kwargs):
120-
"""Output log with the warning level."""
121-
if isinstance(msg, dict):
122-
for _, line in enumerate(_pretty_dict(msg).split("\n")):
123-
Logger().get_logger().warning(line, *args, **kwargs)
124-
else:
125-
Logger().get_logger().warning(msg, *args, **kwargs)
124+
Logger().get_logger().info(msg, *args, **kwargs, stacklevel=2)
126125

127126

128127
def warning(msg, *args, **kwargs):
129128
"""Output log with the warning level (Alias of the method warn)."""
130129
if isinstance(msg, dict):
131130
for _, line in enumerate(_pretty_dict(msg).split("\n")):
132-
Logger().get_logger().warning(line, *args, **kwargs)
131+
Logger().get_logger().warning(line, *args, **kwargs, stacklevel=2)
133132
else:
134-
Logger().get_logger().warning(msg, *args, **kwargs)
133+
Logger().get_logger().warning(msg, *args, **kwargs, stacklevel=2)
134+
135+
136+
level = Logger().get_logger().level
137+
138+
logger = Logger().get_logger()

neural_compressor/onnxrt/algorithms/weight_only/algo_entry.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818

1919
import onnx
2020

21-
from neural_compressor.common import Logger
21+
from neural_compressor.common import logger
2222
from neural_compressor.common.utils import RTN
2323
from neural_compressor.onnxrt.quantization.config import RTNConfig
2424
from neural_compressor.onnxrt.utils.utility import register_algo
2525

26-
logger = Logger().get_logger()
27-
2826

2927
###################### RTN Algo Entry ##################################
3028
@register_algo(name=RTN)

neural_compressor/onnxrt/quantization/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323

2424
import onnx
2525

26-
from neural_compressor.common import Logger
26+
from neural_compressor.common import logger
2727
from neural_compressor.common.base_config import BaseConfig, register_config, register_supported_configs_for_fwk
2828
from neural_compressor.common.utils import DEFAULT_WHITE_LIST, OP_NAME_OR_MODULE_TYPE, RTN
2929

30-
logger = Logger().get_logger()
31-
3230
FRAMEWORK_NAME = "onnxrt"
3331

3432

neural_compressor/onnxrt/quantization/quantize.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import onnx
1919
from onnxruntime.quantization import CalibrationDataReader
2020

21-
from neural_compressor.common import Logger
21+
from neural_compressor.common import logger
2222
from neural_compressor.common.base_config import BaseConfig, ComposableConfig, config_registry
2323
from neural_compressor.onnxrt.quantization.config import FRAMEWORK_NAME
2424
from neural_compressor.onnxrt.utils.utility import algos_mapping
2525

26-
logger = Logger().get_logger()
27-
2826

2927
def need_apply(quant_config: BaseConfig, algo_name):
3028
return quant_config.name == algo_name if hasattr(quant_config, "name") else False

neural_compressor/onnxrt/utils/onnx_model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222

2323
import onnx
2424

25-
from neural_compressor.common import Logger
25+
from neural_compressor.common import logger
2626
from neural_compressor.onnxrt.utils.utility import MAXIMUM_PROTOBUF, find_by_name
2727

28-
logger = Logger().get_logger()
29-
3028

3129
class ONNXModel:
3230
"""Build ONNX model."""

neural_compressor/onnxrt/utils/utility.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import onnx
1919
from packaging.version import Version
2020

21-
from neural_compressor.common import Logger
22-
23-
logger = Logger().get_logger()
21+
from neural_compressor.common import logger
2422

2523
ONNXRT116_VERSION = Version("1.16.0")
2624
ONNXRT1161_VERSION = Version("1.16.1")

0 commit comments

Comments
 (0)