logger.py 4.5 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
import logging
import datetime
import paddle.distributed as dist

_logger = None


D
dongshuilong 已提交
25
def init_logger(name='ppcls', log_file=None, log_level=logging.INFO):
L
littletomatodonkey 已提交
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
    """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)
D
dongshuilong 已提交
62
    _logger.propagate = False
W
WuHaobo 已提交
63

S
shippingwang 已提交
64

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

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

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


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


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


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


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


S
fixed  
shippingwang 已提交
98
def scaler(name, value, step, writer):
S
shippingwang 已提交
99 100 101 102 103 104 105
    """
    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.
    """
littletomatodonkey's avatar
littletomatodonkey 已提交
106 107
    if writer is None:
        return
T
Tingquan Gao 已提交
108
    writer.add_scalar(tag=name, step=step, value=value)
S
shippingwang 已提交
109 110


W
WuHaobo 已提交
111
def advertise():
W
add ad  
WuHaobo 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124
    """
    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 已提交
125
    copyright = "PaddleClas is powered by PaddlePaddle !"
W
WuHaobo 已提交
126
    ad = "For more info please go to the following website."
W
add ad  
WuHaobo 已提交
127
    website = "https://github.com/PaddlePaddle/PaddleClas"
W
WuHaobo 已提交
128
    AD_LEN = 6 + len(max([copyright, ad, website], key=len))
W
add ad  
WuHaobo 已提交
129

L
littletomatodonkey 已提交
130 131 132 133 134 135 136 137 138
    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), ))