logger.py 3.6 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
from imp import reload
reload(logging)

S
shippingwang 已提交
22 23 24 25
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s: %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S")
S
shippingwang 已提交
26 27 28 29 30 31 32 33


def time_zone(sec, fmt):
    real_time = datetime.datetime.now() + datetime.timedelta(hours=8)
    return real_time.timetuple()


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

S
shippingwang 已提交
36 37 38 39 40 41 42 43 44 45
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 已提交
46

S
shippingwang 已提交
47

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

S
shippingwang 已提交
55

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

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

W
WuHaobo 已提交
66
    return wrapper
W
WuHaobo 已提交
67 68


W
WuHaobo 已提交
69
@anti_fleet
W
WuHaobo 已提交
70 71 72 73
def info(fmt, *args):
    _logger.info(fmt, *args)


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


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


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


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

S
shippingwang 已提交
114 115 116 117 118 119 120 121 122 123
    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"))