logger.py 4.4 KB
Newer Older
W
add ad  
WuHaobo 已提交
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
W
WuHaobo 已提交
2
#
W
add ad  
WuHaobo 已提交
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
W
WuHaobo 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
W
add ad  
WuHaobo 已提交
9 10 11 12 13
# 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.
W
WuHaobo 已提交
14

W
WuHaobo 已提交
15
import os
L
littletomatodonkey 已提交
16
import sys
W
WuHaobo 已提交
17

L
littletomatodonkey 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
import logging
import datetime
import paddle.distributed as dist

_logger = None


def init_logger(name='root', log_file=None, log_level=logging.INFO):
    """Initialize and get a logger by name.
    If the logger has not been initialized, this method will initialize the
    logger by adding one or two handlers, otherwise the initialized logger will
    be directly returned. During initialization, a StreamHandler will always be
    added. If `log_file` is specified a FileHandler will also be added.
    Args:
        name (str): Logger name.
        log_file (str | None): The log filename. If specified, a FileHandler
            will be added to the logger.
        log_level (int): The logger level. Note that only the process of
            rank 0 is affected, and other processes will set the level to
            "Error" thus be silent most of the time.
    Returns:
        logging.Logger: The expected logger.
    """
    global _logger
    assert _logger is None, "logger should not be initialized twice or more."
    _logger = logging.getLogger(name)

    formatter = logging.Formatter(
        '[%(asctime)s] %(name)s %(levelname)s: %(message)s',
        datefmt="%Y/%m/%d %H:%M:%S")

    stream_handler = logging.StreamHandler(stream=sys.stdout)
    stream_handler.setFormatter(formatter)
    _logger.addHandler(stream_handler)
    if log_file is not None and dist.get_rank() == 0:
        log_file_folder = os.path.split(log_file)[0]
        os.makedirs(log_file_folder, exist_ok=True)
        file_handler = logging.FileHandler(log_file, 'a')
        file_handler.setFormatter(formatter)
        _logger.addHandler(file_handler)
    if dist.get_rank() == 0:
        _logger.setLevel(log_level)
S
shippingwang 已提交
60
    else:
L
littletomatodonkey 已提交
61
        _logger.setLevel(logging.ERROR)
W
WuHaobo 已提交
62

S
shippingwang 已提交
63

L
littletomatodonkey 已提交
64
def log_at_trainer0(log):
W
WuHaobo 已提交
65
    """
S
shippingwang 已提交
66 67
    logs will print multi-times when calling Fleet API.
    Only display single log and ignore the others.
W
WuHaobo 已提交
68
    """
W
WuHaobo 已提交
69

W
WuHaobo 已提交
70
    def wrapper(fmt, *args):
L
littletomatodonkey 已提交
71
        if dist.get_rank() == 0:
W
WuHaobo 已提交
72
            log(fmt, *args)
W
WuHaobo 已提交
73

W
WuHaobo 已提交
74
    return wrapper
W
WuHaobo 已提交
75 76


L
littletomatodonkey 已提交
77
@log_at_trainer0
W
WuHaobo 已提交
78 79 80 81
def info(fmt, *args):
    _logger.info(fmt, *args)


L
littletomatodonkey 已提交
82 83 84 85 86 87
@log_at_trainer0
def debug(fmt, *args):
    _logger.debug(fmt, *args)


@log_at_trainer0
W
WuHaobo 已提交
88
def warning(fmt, *args):
L
littletomatodonkey 已提交
89
    _logger.warning(fmt, *args)
W
WuHaobo 已提交
90 91


L
littletomatodonkey 已提交
92
@log_at_trainer0
W
WuHaobo 已提交
93
def error(fmt, *args):
L
littletomatodonkey 已提交
94
    _logger.error(fmt, *args)
W
add ad  
WuHaobo 已提交
95 96


S
fixed  
shippingwang 已提交
97
def scaler(name, value, step, writer):
S
shippingwang 已提交
98 99 100 101 102 103 104
    """
    This function will draw a scalar curve generated by the visualdl.
    Usage: Install visualdl: pip3 install visualdl==2.0.0b4
           and then:
           visualdl --logdir ./scalar --host 0.0.0.0 --port 8830 
           to preview loss corve in real time.
    """
T
Tingquan Gao 已提交
105
    writer.add_scalar(tag=name, step=step, value=value)
S
shippingwang 已提交
106 107


W
WuHaobo 已提交
108
def advertise():
W
add ad  
WuHaobo 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
    """
    Show the advertising message like the following:

    ===========================================================
    ==        PaddleClas is powered by PaddlePaddle !        ==
    ===========================================================
    ==                                                       ==
    ==   For more info please go to the following website.   ==
    ==                                                       ==
    ==       https://github.com/PaddlePaddle/PaddleClas      ==
    ===========================================================

    """
W
add ad  
WuHaobo 已提交
122
    copyright = "PaddleClas is powered by PaddlePaddle !"
W
WuHaobo 已提交
123
    ad = "For more info please go to the following website."
W
add ad  
WuHaobo 已提交
124
    website = "https://github.com/PaddlePaddle/PaddleClas"
W
WuHaobo 已提交
125
    AD_LEN = 6 + len(max([copyright, ad, website], key=len))
W
add ad  
WuHaobo 已提交
126

L
littletomatodonkey 已提交
127 128 129 130 131 132 133 134 135
    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(ad.center(AD_LEN)),
        "=={}==".format(' ' * AD_LEN),
        "=={}==".format(website.center(AD_LEN)),
        "=" * (AD_LEN + 4), ))