-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom_log.py
More file actions
99 lines (79 loc) · 2.64 KB
/
custom_log.py
File metadata and controls
99 lines (79 loc) · 2.64 KB
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
# -*- coding: utf-8 -*-
import logging
import threading
import os
Lock = threading.Lock()
def make_dir(dir_path):
if os.path.exists(dir_path):
return False
else:
os.makedirs(dir_path)
return True
def make_file(filepath):
thedir, filename = os.path.split(filepath)
if not thedir or not filename:
return False
if not os.path.exists(filepath):
make_dir(thedir)
fh = open(filepath, 'w')
fh.close()
return True
else:
return False
class SettingLogSingleton(object):
'''
log只能初始化一次,否则写log会重复,所以才使用单例模式
'''
# 定义静态变量实例
__instance = None
__log_dir = ''
__log_file = ''
@classmethod
def add_message_to_setting_log(cls, msg):
logger = logging.getLogger(cls.__log_file)
logger.debug(msg)
@classmethod
def __init_logger(cls):
setting_log_filename = cls.__log_file
make_file(setting_log_filename)
logger = logging.getLogger(setting_log_filename)
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(setting_log_filename)
fh.setLevel(logging.DEBUG)
fm = 'setting_log: %(levelname)s: [%(filename)s line:%(lineno)d %(funcName)s] -- [%(asctime)s] %(message)s'
formatter = logging.Formatter(fm)
fh.setFormatter(formatter)
logger.addHandler(fh)
def __new__(cls, watch_dir, filename):
cls.__log_dir = watch_dir
cls.__log_file = os.path.join(watch_dir, filename)
if not cls.__instance:
try:
Lock.acquire()
# double check
if not cls.__instance:
# cls.__instance = super(SettingLogSingleton, cls).__new__(cls, *args, **kwargs)
cls.__instance = object.__new__(cls)
cls.__instance.__init_logger()
cls.__instance.__init_logger()
cls.__instance.__init_logger()
finally:
Lock.release()
return cls.__instance
def test_singleton():
import time
log_dir = './log'
log_file = 'demo.log'
obj2 = SettingLogSingleton(log_dir, log_file)
obj = SettingLogSingleton(log_dir, log_file)
print(id(obj))
obj.add_message_to_setting_log('*' * 80)
obj = SettingLogSingleton(log_dir, log_file)
print(id(obj))
obj.add_message_to_setting_log('for testing')
obj = SettingLogSingleton(log_dir, log_file)
obj.add_message_to_setting_log('for testing')
obj.add_message_to_setting_log('for testing')
print(id(obj))
if __name__ == "__main__":
test_singleton()