logger.py 3.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 16
import logging
import os
S
shippingwang 已提交
17
import datetime
W
WuHaobo 已提交
18

S
shippingwang 已提交
19 20 21 22
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s: %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S")
S
shippingwang 已提交
23 24 25


def time_zone(sec, fmt):
littletomatodonkey's avatar
littletomatodonkey 已提交
26
    real_time = datetime.datetime.now()
S
shippingwang 已提交
27 28 29 30
    return real_time.timetuple()


logging.Formatter.converter = time_zone
W
WuHaobo 已提交
31
_logger = logging.getLogger(__name__)
W
WuHaobo 已提交
32

S
shippingwang 已提交
33 34 35 36 37 38 39 40 41 42
Color = {
    'RED': '\033[31m',
    'HEADER': '\033[35m',  # deep purple
    'PURPLE': '\033[95m',  # purple
    'OKBLUE': '\033[94m',
    'OKGREEN': '\033[92m',
    'WARNING': '\033[93m',
    'FAIL': '\033[91m',
    'ENDC': '\033[0m'
}
S
shippingwang 已提交
43

S
shippingwang 已提交
44

S
shippingwang 已提交
45 46 47
def coloring(message, color="OKGREEN"):
    assert color in Color.keys()
    if os.environ.get('PADDLECLAS_COLORING', False):
S
shippingwang 已提交
48
        return Color[color] + str(message) + Color["ENDC"]
S
shippingwang 已提交
49 50
    else:
        return message
W
WuHaobo 已提交
51

S
shippingwang 已提交
52

W
WuHaobo 已提交
53
def anti_fleet(log):
W
WuHaobo 已提交
54
    """
S
shippingwang 已提交
55 56
    logs will print multi-times when calling Fleet API.
    Only display single log and ignore the others.
W
WuHaobo 已提交
57
    """
W
WuHaobo 已提交
58

W
WuHaobo 已提交
59 60 61
    def wrapper(fmt, *args):
        if int(os.getenv("PADDLE_TRAINER_ID", 0)) == 0:
            log(fmt, *args)
W
WuHaobo 已提交
62

W
WuHaobo 已提交
63
    return wrapper
W
WuHaobo 已提交
64 65


W
WuHaobo 已提交
66
@anti_fleet
W
WuHaobo 已提交
67 68 69 70
def info(fmt, *args):
    _logger.info(fmt, *args)


W
WuHaobo 已提交
71
@anti_fleet
W
WuHaobo 已提交
72
def warning(fmt, *args):
S
shippingwang 已提交
73
    _logger.warning(coloring(fmt, "RED"), *args)
W
WuHaobo 已提交
74 75


W
WuHaobo 已提交
76
@anti_fleet
W
WuHaobo 已提交
77
def error(fmt, *args):
S
shippingwang 已提交
78
    _logger.error(coloring(fmt, "FAIL"), *args)
W
add ad  
WuHaobo 已提交
79 80


S
fixed  
shippingwang 已提交
81
def scaler(name, value, step, writer):
S
shippingwang 已提交
82 83 84 85 86 87 88
    """
    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.
    """
S
fixed  
shippingwang 已提交
89
    writer.add_scalar(name, value, step)
S
shippingwang 已提交
90 91


W
WuHaobo 已提交
92
def advertise():
W
add ad  
WuHaobo 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
    """
    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 已提交
106
    copyright = "PaddleClas is powered by PaddlePaddle !"
W
WuHaobo 已提交
107
    ad = "For more info please go to the following website."
W
add ad  
WuHaobo 已提交
108
    website = "https://github.com/PaddlePaddle/PaddleClas"
W
WuHaobo 已提交
109
    AD_LEN = 6 + len(max([copyright, ad, website], key=len))
W
add ad  
WuHaobo 已提交
110

S
shippingwang 已提交
111 112 113 114 115 116 117 118 119 120
    info(
        coloring("\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), ), "RED"))