-
Notifications
You must be signed in to change notification settings - Fork 115
/
__init__.py
153 lines (110 loc) · 4.13 KB
/
__init__.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from logging.handlers import RotatingFileHandler
from os import path
import logging
import os
class LogDir(object):
'''
Locates the log dir for plugin logs.
'''
@staticmethod
def find():
return LogDir()._find_log_dir()
def _test(self, a, b):
if a == b:
return 'folder'
elif a == b + '.sublime-package':
return 'sublime-package'
def _find_path(self, start, package):
while True:
result = self._test(path.basename(start), package)
if result == 'folder':
if path.exists(path.join(path.dirname(start), 'User')):
return path.join(path.dirname(start), '.logs')
elif result == 'sublime-package':
parent = path.dirname(start)
if path.exists(path.join(path.dirname(parent), 'Packages')):
return path.join(path.dirname(parent), 'Packages', '.logs')
if path.dirname(start) == start:
return
start = path.dirname(start)
def _find_log_dir(self):
package = __name__.split('.')[0]
if package == '__main__':
return
start = path.dirname(__file__)
logs_path = self._find_path(start, package)
if not logs_path:
return
if not path.exists(logs_path):
os.mkdir(logs_path)
return logs_path
class NullPluginLogger(object):
'''
Supresses log records.
'''
def __init__(self, name):
pass
def debug(self, message, *args, **kwargs):
pass
def info(self, message, *args, **kwargs):
pass
def warn(self, message, *args, **kwargs):
pass
def warning(self, message, *args, **kwargs):
pass
def error(self, message, *args, **kwargs):
pass
def critical(self, message, *args, **kwargs):
pass
class PluginLogger(object):
'''
Logs events.
'''
log_dir = LogDir.find()
def __init__(self, name):
self.logger = logging.getLogger(name)
default_level = logging.ERROR
user_level = self._get_log_level_from_file()
self.logger.setLevel(user_level if user_level is not None else default_level)
f = logging.Formatter('%(asctime)s %(levelname)-5s %(name)s %(message)s')
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.WARNING)
consoleHandler.setFormatter(f)
self.logger.addHandler(consoleHandler)
file_name = self._file_name()
if file_name:
fileHandler = RotatingFileHandler(file_name, maxBytes=1<<10)
fileHandler.setFormatter(f)
self.logger.addHandler(fileHandler)
else:
print("Vintageous: cannot find log file path: %s" % file_name)
def warn_aboug_logging_level(self):
if self.logger.level <= logging.DEBUG:
package = __name__.split('.')[0]
self.warning("debug level set to DEBUG; check or delete %s", self._get_path_to_log())
def _get_path_to_log(self):
package = __name__.split('.')[0]
p = path.join(self.log_dir, package)
return p
def _get_log_level_from_file(self):
p = self._get_path_to_log()
if path.exists(p):
with open(p, 'rt') as f:
text = f.read().strip().upper()
return getattr(logging, text, None)
def _file_name(self):
p = __name__.split('.')[0]
return os.path.join(self.log_dir, '{}.log'.format(p))
def debug(self, message, *args, **kwargs):
self.logger.debug(message, *args, **kwargs)
def info(self, message, *args, **kwargs):
self.logger.info(message, *args, **kwargs)
def warn(self, message, *args, **kwargs):
self.logger.warning(message, *args, **kwargs)
def warning(self, message, *args, **kwargs):
self.logger.warning(message, *args, **kwargs)
def error(self, message, *args, **kwargs):
self.logger.error(message, *args, **kwargs)
def critical(self, message, *args, **kwargs):
self.logger.critical(message, *args, **kwargs)
PluginLogger(__name__).warn_aboug_logging_level()