logger.py 2.4 KB
Newer Older
B
barriery 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import logging
16
import logging.config
B
barriery 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import os


class SectionLevelFilter(object):
    def __init__(self, levels):
        self._levels = levels

    def filter(self, logRecord):
        return logRecord.levelno in self._levels


log_dir = "PipelineServingLogs"
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

32 33 34 35 36 37 38 39 40 41 42 43 44
logger_config = {
    "version": 1,
    "formatters": {
        "normal_fmt": {
            "format":
            "%(levelname)s %(asctime)s [%(filename)s:%(lineno)d] %(message)s",
        },
        "tracer_fmt": {
            "format": "%(asctime)s %(message)s",
        },
    },
    "handlers": {
        "f_pipeline.log": {
T
TeslaZhao 已提交
45
            "class": "logging.handlers.RotatingFileHandler",
46 47 48
            "level": "INFO",
            "formatter": "normal_fmt",
            "filename": os.path.join(log_dir, "pipeline.log"),
T
TeslaZhao 已提交
49 50
            "maxBytes": 512000000,
            "backupCount": 20,
51 52
        },
        "f_pipeline.log.wf": {
T
TeslaZhao 已提交
53
            "class": "logging.handlers.RotatingFileHandler",
54 55 56
            "level": "WARNING",
            "formatter": "normal_fmt",
            "filename": os.path.join(log_dir, "pipeline.log.wf"),
T
TeslaZhao 已提交
57 58
            "maxBytes": 512000000,
            "backupCount": 10,
59 60
        },
        "f_tracer.log": {
T
TeslaZhao 已提交
61
            "class": "logging.handlers.RotatingFileHandler",
62 63 64
            "level": "INFO",
            "formatter": "tracer_fmt",
            "filename": os.path.join(log_dir, "pipeline.tracer"),
T
TeslaZhao 已提交
65 66
            "maxBytes": 512000000,
            "backupCount": 5,
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        },
    },
    "loggers": {
        # propagate = True
        ".".join(__name__.split(".")[:-1] + ["profiler"]): {
            "level": "INFO",
            "handlers": ["f_tracer.log"],
        },
    },
    "root": {
        "level": "DEBUG",
        "handlers": ["f_pipeline.log", "f_pipeline.log.wf"],
    },
}

logging.config.dictConfig(logger_config)