-
Notifications
You must be signed in to change notification settings - Fork 16
/
symLogging.py
80 lines (63 loc) · 2.05 KB
/
symLogging.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
import logging
import os
import tempfile
from logging.handlers import RotatingFileHandler
from symUtil import mkdir_p
gLog = None
# Whether to pause and launch debugger at CheckDebug breakpoints
gDebug = False
def SetLoggingOptions(logOptions):
global gLog
gLog = logging.getLogger("tornado.application")
fmt = logging.Formatter('%(asctime)s\t%(levelname)s\t%(message)s')
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(fmt)
gLog.addHandler(streamHandler)
filepath = logOptions.get('logPath', tempfile.gettempdir())
try:
mkdir_p(filepath)
rotatingHandler = \
RotatingFileHandler(
filename=os.path.join(filepath, 'snappy.log'),
mode='a',
maxBytes=int(logOptions.get("maxFileSize", 1024*1024)),
backupCount=int(logOptions.get("maxFiles", 10)))
rotatingHandler.setFormatter(fmt)
gLog.addHandler(rotatingHandler)
except OSError as exc:
gLog.error("Invalid path '%s': %s.", filepath, exc)
gLog.error("Logging on screen only.")
except IOError as exc:
gLog.error("Could not configure file logger: %s", exc)
gLog.error("Check the log path '%s'. Logging on screen only.", filepath)
logLevel = getattr(logging, logOptions.get("logLevel", "INFO"))
gLog.setLevel(logLevel)
def doLog(dbgLevel, string, remoteIp):
pid = os.getpid()
gLog.log(
dbgLevel,
"%d\t%s%s",
pid,
string,
" IP=" + remoteIp if remoteIp else '')
def LogDebug(string, remoteIp=None):
if gLog.isEnabledFor(logging.DEBUG):
doLog(logging.DEBUG, string, remoteIp)
def LogError(string, remoteIp=None):
if gLog.isEnabledFor(logging.ERROR):
doLog(logging.ERROR, string, remoteIp)
def LogMessage(string, remoteIp=None):
if gLog.isEnabledFor(logging.INFO):
doLog(logging.INFO, string, remoteIp)
def SetDebug(isEnabled):
global gDebug
gDebug = isEnabled
def CheckDebug():
global gDebug
if not gDebug:
return
# launch debug console
LogMessage("Stopping server, starting debug console!")
import pdb
pdb.set_trace()
LogMessage("Exited debug console, resuming server")