自定义python日志文件系统

Posted by Rpl on December 10, 2019

python的日志系统非常实用,可以自定义使用。

以下程序是基于logging模块编辑的一个自定义的logging系统,包括自定义日志输出级别、输出格式、输出方式, 自定义日志文件切分方式, 自定义日志分类等功能。

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# coding=utf-8
"""
Author: rpl

date: 19-12-9 下午2:46
desc:
"""

import os
import socket
import datetime

import logging
from logging import handlers


# 日志文件的保存位置
Log_path = os.path.dirname(__file__)


# 用字典保存日志级别
format_dict = {
    1: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
    2: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - filename:%(filename)s - line:%(lineno)d '
                         '- message:%(message)s'),
    3: None,  # 不做任何格式
}


def get_host_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('8.8.8.8', 80))
    ip = s.getsockname()[0]

    return ip


class Logger(object):
    def __init__(self, log_filename=None, log_objname='Log', classify=None,
                 format_type=1, detial_time=True, stdout=True,
                 time_split=False, when='D', interval=1,
                 chunk_split=False, maxchunk=1024*1024*10,
                 backupCount=10,
                 ip=True):
        """

        :param log_filename: 日志文件名, 是否输出到文件, 默认否
        :param log_objname: 日志类名

        :param classify: 日志分类,用于分类输入到不同的文件夹, 默认无
        :param format_type: 日志的输出格式,可自定义

        :param detial_time: 日志文件是否输出详细时间(时分秒), 默认输出
        :param stdout: 是否打印到屏幕, 默认打印到屏幕

        :param time_split: 是否按时间切分日志文件
        :param when: D按天切分
        :param interval: 间隔

        :param chunl_split: 是否按文件大小切分文件
        :param maxchunk 10  切分标准  1024*1024*10是超过10M切分
        :param backupCount 最大备份数 10 最多保留10个切分文件,之后会丢弃最旧的。
        :param ip ip=True 获取本机ip作为不同机器日志文件的识别标志
        """

        self.logger = logging.getLogger(log_objname)
        self.logger.setLevel(logging.DEBUG)
        if ip:
            self.ip = get_host_ip().split('.')[-1]

        formatter = format_dict[format_type]

        if detial_time:  # 日志文件名 详细时间, 带时分秒
            log_date = datetime.datetime.now().strftime('.%Y-%m-%d-%H%M%S')
        else:
            log_date = datetime.datetime.now().strftime('.%Y-%m-%d')

        # 是否有文件名, 有则输出到此文件
        if log_filename:

            if classify and ip:
                log_path = os.path.join(Log_path, self.ip+'_log_file', classify)
            elif classify and not ip:
                log_path = os.path.join(Log_path, 'log_file', classify)
            elif not classify and ip:
                log_path = os.path.join(Log_path, self.ip + '_log_file')
            else:
                log_path = os.path.join(Log_path, 'log_file')

            if not os.path.exists(log_path):
                os.makedirs(log_path)

            self.log_filename = os.path.join(log_path, log_filename+log_date)
            if time_split:
                fh = logging.handlers.TimedRotatingFileHandler(log_filename, when=when, interval=interval, backupCount=backupCount)
            elif chunk_split:
                # 按大小切分日志文件, 超过30M分割, 最多备份10个
                fh = logging.handlers.RotatingFileHandler(log_filename, maxBytes=maxchunk, backupCount=backupCount)
            else:
                fh = logging.FileHandler(self.log_filename)

            fh.setLevel(logging.DEBUG)

            if formatter:
                fh.setFormatter(formatter)
            self.logger.addHandler(fh)

        if stdout:  # 是否输出到屏幕
            ch = logging.StreamHandler()
            ch.setLevel(logging.DEBUG)
            ch.setFormatter(formatter)
            self.logger.addHandler(ch)

    def getLog(self):
        return self.logger


if __name__ == '__main__':
    log = Logger(log_filename='test_log', classify='test', log_objname='test1',
                 detial_time=True, stdout=True, format_type=1, ip=False).getLog()

    log.info('this is log test')