forked from pagekite/PyPagekite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.py
executable file
·162 lines (136 loc) · 4.41 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
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
154
155
156
157
158
159
160
161
"""
Logging.
"""
from __future__ import absolute_import
##############################################################################
LICENSE = """\
This file is part of pagekite.py.
Copyright 2010-2020, the Beanstalks Project ehf. and Bjarni Runar Einarsson
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see: <http://www.gnu.org/licenses/>
"""
##############################################################################
import threading
import time
import sys
from . import compat, common
from .compat import *
from .common import *
syslog = compat.syslog
org_stdout = sys.stdout
DEBUG_IO = False
LOG = []
LOG_LINE = 0
LOG_LENGTH = 300
LOG_THRESHOLD = 256 * 1024
LOG_LOCK = threading.Lock()
LOG_LEVEL_NONE = 1
LOG_LEVEL_ERR = 2
LOG_LEVEL_WARN = 3
LOG_LEVEL_INFO = 4
LOG_LEVEL_MACH = 5
LOG_LEVEL_DEBUG = 6
LOG_LEVEL_DEFAULT = LOG_LEVEL_INFO
LOG_LEVELS = {
'none': LOG_LEVEL_NONE,
'err': LOG_LEVEL_ERR,
'errors': LOG_LEVEL_ERR,
'warn': LOG_LEVEL_WARN,
'warnings': LOG_LEVEL_WARN,
'info': LOG_LEVEL_INFO,
'mach': LOG_LEVEL_MACH,
'machine': LOG_LEVEL_MACH,
'debug': LOG_LEVEL_DEBUG,
'full': LOG_LEVEL_DEBUG,
'all': LOG_LEVEL_DEBUG,
0: 'none',
LOG_LEVEL_NONE: 'none',
LOG_LEVEL_ERR: 'err',
LOG_LEVEL_WARN: 'warn',
LOG_LEVEL_INFO: 'info',
LOG_LEVEL_MACH: 'mach',
LOG_LEVEL_DEBUG: 'debug'}
LOG_LEVEL_DEFNAME = LOG_LEVELS[LOG_LEVEL_DEFAULT]
LOG_LEVEL = LOG_LEVEL_DEFAULT
def LogValues(values, testtime=None):
global LOG, LOG_LINE, LOG_LAST_TIME
now = int(testtime or time.time())
words = [('ts', '%x' % now),
('t', '%s' % ts_to_iso(now)),
('ll', '%x' % LOG_LINE)]
words.extend([(kv[0], ('%s' % kv[1]).replace('\t', ' ')
.replace('\r', ' ')
.replace('\n', ' ')
.replace('; ', ', ')
.strip()) for kv in values])
wdict = dict(words)
LOG_LINE += 1
LOG.append(wdict)
while len(LOG) > LOG_LENGTH:
LOG[0:(LOG_LENGTH//10)] = []
return (words, wdict)
def LogSyslog(values, wdict=None, words=None, level=LOG_LEVEL_INFO):
global LOG_LEVEL
if level > LOG_LEVEL: return
if values:
words, wdict = LogValues(values)
if level <= LOG_LEVEL_ERR or ('err' in wdict):
syslog.syslog(syslog.LOG_ERR, '; '.join(['='.join(x) for x in words]))
elif level <= LOG_LEVEL_INFO:
syslog.syslog(syslog.LOG_INFO, '; '.join(['='.join(x) for x in words]))
else:
syslog.syslog(syslog.LOG_DEBUG, '; '.join(['='.join(x) for x in words]))
def LogToFile(values, wdict=None, words=None, level=LOG_LEVEL_INFO):
global LOG_LEVEL
if level > LOG_LEVEL: return
if values:
words, wdict = LogValues(values)
try:
global LogFile
with LOG_LOCK:
LogFile.write('; '.join(['='.join(x) for x in words]))
LogFile.write('\n')
LogFile.flush()
except (OSError, IOError):
# Avoid crashing if the disk fills up or something lame like that
pass
def LogToMemory(values, wdict=None, words=None, level=LOG_LEVEL_INFO):
global LOG_LEVEL
if values and (level <= LOG_LEVEL):
with LOG_LOCK:
LogValues(values)
def FlushLogMemory():
global LOG
for l in LOG:
Log(None, wdict=l, words=[(w, l[w]) for w in l], level=LOG_LEVEL)
def LogError(msg, parms=None):
emsg = [('err', msg)]
if parms: emsg.extend(parms)
Log(emsg, level=LOG_LEVEL_ERR)
if common.gYamon:
common.gYamon.vadd('errors', 1, wrap=1000000)
def LogWarning(msg, parms=None):
emsg = [('warn', msg)]
if parms: emsg.extend(parms)
Log(emsg, level=LOG_LEVEL_WARN)
def LogDebug(msg, parms=None):
emsg = [('debug', msg)]
if parms: emsg.extend(parms)
Log(emsg, level=LOG_LEVEL_DEBUG)
def LogInfo(msg, parms=None):
emsg = [('info', msg)]
if parms: emsg.extend(parms)
Log(emsg, level=LOG_LEVEL_INFO)
def ResetLog():
global LogFile, Log, org_stdout
LogFile = org_stdout
Log = LogToMemory
ResetLog()