logger.py 2.6 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
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"
F
felixhjh 已提交
28 29 30 31
if 'SERVING_LOG_PATH' in os.environ:
    serving_log_path = os.environ['SERVING_LOG_PATH']
    log_dir = os.path.join(serving_log_path, log_dir)

B
barriery 已提交
32 33 34
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

35 36 37 38 39 40 41 42 43 44 45 46 47
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": {
48
            "class": "logging.handlers.RotatingFileHandler",
49 50 51
            "level": "INFO",
            "formatter": "normal_fmt",
            "filename": os.path.join(log_dir, "pipeline.log"),
52 53
            "maxBytes": 512000000,
            "backupCount": 20,
54 55
        },
        "f_pipeline.log.wf": {
56
            "class": "logging.handlers.RotatingFileHandler",
57 58 59
            "level": "WARNING",
            "formatter": "normal_fmt",
            "filename": os.path.join(log_dir, "pipeline.log.wf"),
60 61
            "maxBytes": 512000000,
            "backupCount": 10,
62 63
        },
        "f_tracer.log": {
64
            "class": "logging.handlers.RotatingFileHandler",
65 66 67
            "level": "INFO",
            "formatter": "tracer_fmt",
            "filename": os.path.join(log_dir, "pipeline.tracer"),
68 69
            "maxBytes": 512000000,
            "backupCount": 5,
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        },
    },
    "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)