logger.py 4.7 KB
Newer Older
S
Steffy-zxf 已提交
1
#coding:utf-8
W
wuzewu 已提交
2
# Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
W
wuzewu 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

from __future__ import print_function
from __future__ import division
from __future__ import print_function
W
wuzewu 已提交
19

20
import colorlog
W
wuzewu 已提交
21
import logging
W
wuzewu 已提交
22
import math
23 24 25 26
import os
import json

from paddlehub.common.dir import CONF_HOME
W
wuzewu 已提交
27

W
wuzewu 已提交
28

29
class Logger(object):
30 31 32 33 34 35 36
    DEBUG = 10
    INFO = 20
    WARNING = 30
    ERROR = 40
    CRITICAL = 50
    TRAIN = 21
    EVAL = 22
W
wuzewu 已提交
37 38
    PLACEHOLDER = '%'
    NOLOG = "NOLOG"
39 40
    logging.addLevelName(TRAIN, 'TRAIN')
    logging.addLevelName(EVAL, 'EVAL')
W
wuzewu 已提交
41 42 43

    def __init__(self, name=None):
        if not name:
44
            name = "PaddleHub"
W
wuzewu 已提交
45
        self.logger = logging.getLogger(name)
K
kinghuin 已提交
46 47
        self.handler = logging.StreamHandler()

48 49 50 51 52 53 54 55 56 57 58 59
        self.format = colorlog.ColoredFormatter(
            '%(log_color)s[%(asctime)-15s] [%(levelname)8s] - %(message)s',
            log_colors={
                'DEBUG': 'purple',
                'INFO': 'green',
                'WARNING': 'yellow',
                'ERROR': 'red',
                'CRITICAL': 'bold_red',
                'TRAIN': 'cyan',
                'EVAL': 'blue',
            })
        self.handler.setFormatter(self.format)
K
kinghuin 已提交
60
        self.logger.addHandler(self.handler)
S
shenyuhan 已提交
61
        self.logLevel = "DEBUG"
62 63
        self.logger.setLevel(logging.DEBUG)
        self.logger.propagate = False
S
shenyuhan 已提交
64 65
        if os.path.exists(os.path.join(CONF_HOME, "config.json")):
            with open(os.path.join(CONF_HOME, "config.json"), "r") as fp:
S
shenyuhan 已提交
66
                level = json.load(fp).get("log_level", "DEBUG")
S
shenyuhan 已提交
67 68
                self.logLevel = level
                self.setLevel(level)
W
wuzewu 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81

    def _is_no_log(self):
        return self.getLevel() == Logger.NOLOG

    def setLevel(self, logLevel):
        self.logLevel = logLevel.upper()
        if not self._is_no_log():
            _logging_level = eval("logging.%s" % self.logLevel)
            self.logger.setLevel(_logging_level)

    def getLevel(self):
        return self.logLevel

82
    def __call__(self, level, msg):
W
wuzewu 已提交
83
        def _get_log_arr(msg, len_limit=30):
W
wuzewu 已提交
84 85 86 87 88 89 90 91
            ph = Logger.PLACEHOLDER
            lrspace = 2
            lc = rc = " " * lrspace
            tbspace = 1
            msgarr = str(msg).split("\n")
            if len(msgarr) == 1:
                return msgarr

W
wuzewu 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
            temp_arr = msgarr
            msgarr = []
            for text in temp_arr:
                if len(text) > len_limit:
                    for i in range(math.ceil(len(text) / len_limit)):
                        if i == 0:
                            msgarr.append(text[0:len_limit])
                        else:
                            fr = len_limit + (len_limit - 4) * (i - 1)
                            to = len_limit + (len_limit - 4) * i
                            if to > len(text):
                                to = len(text)
                            msgarr.append("===>" + text[fr:to])
                else:
                    msgarr.append(text)

W
wuzewu 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
            maxlen = -1
            for text in msgarr:
                if len(text) > maxlen:
                    maxlen = len(text)

            result = [" ", ph * (maxlen + 2 + lrspace * 2)]
            tbline = "%s%s%s" % (ph, " " * (maxlen + lrspace * 2), ph)
            for index in range(tbspace):
                result.append(tbline)
            for text in msgarr:
                text = "%s%s%s%s%s%s" % (ph, lc, text, rc, " " *
                                         (maxlen - len(text)), ph)
                result.append(text)
            for index in range(tbspace):
                result.append(tbline)
            result.append(ph * (maxlen + 2 + lrspace * 2))
            return result

        if self._is_no_log():
            return

        for msg in _get_log_arr(msg):
130
            self.logger.log(level, msg)
W
wuzewu 已提交
131 132

    def debug(self, msg):
133
        self(logger.DEBUG, msg)
W
wuzewu 已提交
134 135

    def info(self, msg):
136
        self(logger.INFO, msg)
W
wuzewu 已提交
137 138

    def warning(self, msg):
139 140 141 142
        self(logger.WARNING, msg)

    def error(self, msg):
        self(logger.ERROR, msg)
W
wuzewu 已提交
143 144

    def critical(self, msg):
145 146 147 148 149 150 151
        self(logger.CRITICAL, msg)

    def train(self, msg):
        self(logger.TRAIN, msg)

    def eval(self, msg):
        self(logger.EVAL, msg)
W
wuzewu 已提交
152 153 154


logger = Logger()