logger.py 2.0 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# -*- coding: UTF-8 -*-
import os
import logging
import platform

from logging.handlers import RotatingFileHandler


def is_osx():
    p = platform.platform()
    return p.find('macOS') >= 0 or p.find('Darwin') >= 0


def get_root_log_dir(config, options):
    action = 'default'
    if options.action:
        action = options.action
    if is_osx():
        return '/tmp/csdn/ai/{}'.format(action)
    else:
        is_dev = (options.cluster is None) or (options.cluster == 'dev')
        if is_dev:
            return '../log/csdn/ai/{}'.format(action)
        else:
            return '/var/csdn/csdn/ai/{}'.format(action)


class TruncatedFileHandler(RotatingFileHandler):
    '''
    日志文件按固定大小自动分割
    '''

    def __init__(self, filename, mode='a', maxBytes=0, encoding=None, delay=0):
        super(TruncatedFileHandler, self).__init__(
            filename, mode, maxBytes, 0, encoding, delay)

    def doRollover(self):
        """Truncate the file"""
        if self.stream:
            self.stream.close()
        dfn = self.baseFilename + ".1"
        if os.path.exists(dfn):
            os.remove(dfn)
        os.rename(self.baseFilename, dfn)
        os.remove(dfn)
        self.mode = 'w'
        self.stream = self._open()


def init_log(config, options):
    # 创建日志目录
    root_log_dir = get_root_log_dir(config, options)
    os.makedirs(root_log_dir, exist_ok=True)
    print('root_log_dir:', root_log_dir)

    # 文件日志控制器
    log_filename = root_log_dir+'/app.log'
    file_handler = TruncatedFileHandler(log_filename, "w", 10*1024)

    # 控制台日志控制器
    console_handler = logging.StreamHandler()

    # 日志配置
    logging.basicConfig(
        # 日志格式
        format="[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d at %(funcName)s]: %(message)s",
        # 日期格式
        datefmt='%Y-%m-%d %H:%M:%S',
        # 日志级别
        level=logging.INFO,
        # 输出目标,日志文件+控制台
        handlers=[
            file_handler,
            console_handler
        ]
    )