-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_logging.py
58 lines (43 loc) · 1.55 KB
/
_logging.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
import logging
import sys
import sublime
class ConsoleHandler(logging.StreamHandler):
"""
`StreamHandler` logging subclass which handles console panel opening when needed.
"""
def handle(self, record):
"""Open console panel if it is hidden before emitting record"""
record_name = record.name if record.name != "root" else None
if (
logging.getLogger(record_name).isEnabledFor(record.levelno)
and record.levelno >= logging.WARNING
):
window = sublime.active_window()
if window.active_panel() != "console":
window.run_command(
"show_panel",
{
"panel": "console",
"toggle": True,
},
)
if record.levelno >= logging.WARNING:
self.setStream(sys.stderr)
else:
self.setStream(sys.stdout)
super().handle(record)
def _settings():
return sublime.load_settings("SSHubl.sublime-settings")
logger = logging.getLogger(__package__)
handler = ConsoleHandler()
handler.setFormatter(logging.Formatter("[%(name)s] %(asctime)s:%(levelname)s: %(message)s"))
logger.addHandler(handler)
logger.propagate = False
def plugin_loaded():
def _on_change():
logger.setLevel(logging.DEBUG if _settings().get("debug") else logging.INFO)
_settings().add_on_change(__name__, _on_change)
_on_change()
def plugin_unloaded():
_settings().clear_on_change(__name__)
logger.removeHandler(handler)