log_util.py 1.9 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15 16 17
# 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.

import logging
import sys

R
Roc 已提交
18
from paddle.distributed.utils.log_utils import get_logger
19

R
Roc 已提交
20
logger = get_logger("INFO", __name__)
21

22

R
Roc 已提交
23 24 25
def set_log_level(level):
    """
    Set log level
26

R
Roc 已提交
27 28
    Args:
        level (str|int): a specified level
29

R
Roc 已提交
30 31 32 33 34
    Example 1:
        import paddle
        import paddle.distributed.fleet as fleet
        fleet.init()
        fleet.setLogLevel("DEBUG")
35

R
Roc 已提交
36 37 38 39 40
    Example 2:
        import paddle
        import paddle.distributed.fleet as fleet
        fleet.init()
        fleet.setLogLevel(1)
41

R
Roc 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    """
    assert isinstance(level, (str, int)), "level's type must be str or int"
    if isinstance(level, int):
        logger.setLevel(level)
    else:
        logger.setLevel(level.upper())


def get_log_level_code():
    """
    Return current log level code
    """
    return logger.getEffectiveLevel()


def get_log_level_name():
    """
    Return current log level name
    """
    return logging.getLevelName(get_log_level_code())
62 63 64 65 66 67 68 69 70 71 72 73 74


def layer_to_str(base, *args, **kwargs):
    name = base + "("
    if args:
        name += ", ".join(str(arg) for arg in args)
        if kwargs:
            name += ", "
    if kwargs:
        name += ", ".join("{}={}".format(key, str(value))
                          for key, value in kwargs.items())
    name += ")"
    return name