This repository was archived by the owner on Dec 26, 2023. It is now read-only.
forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging.py
More file actions
131 lines (103 loc) · 3.67 KB
/
Copy pathlogging.py
File metadata and controls
131 lines (103 loc) · 3.67 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
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
# ------------------------------------------------------------------------
# coding=utf-8
# ------------------------------------------------------------------------
#
# logging.py
#
# Created by Martin J. Laubach on 07.10.09.
#
# ------------------------------------------------------------------------
from feincms import settings
from feincms.utils import get_object
# ------------------------------------------------------------------------
class LogBase(object):
"""
Base logging class. This just defines the infrastructure but does
not log anything. To implement something more usefule, subclass it
and implement the do_log() method.
"""
# Log categories
ANY = 0
DB = 1
CACHE = 2
AUTH = 3
# Log levels
TRACE = 9
DEBUG = 7
INFO = 5
WARN = 3
ERR = 1
_name_dict = { }
_k = 0
for _k in locals():
if _k[0] != '_':
_name_dict[_k] = eval(_k)
# Default log levels
levels = { ANY: 10, DB: 10, CACHE: 10, AUTH: 10 }
def log(self, *args, **kwargs):
level = kwargs.get('level', self.ERR)
subsys = kwargs.get('subsys', self.ANY)
if level > self.levels[subsys]:
return
self.do_log(subsys, level, *args)
def set_levels(self, **kwargs):
"""
Set log levels.
logger.set_levels(CACHE=logger.ERR, AUTH=logger.INFO)
"""
for k in kwargs.keys():
self.levels[self._name_dict[k]] = kwargs[k]
def trace(self, *args, **kwargs):
self.log(level=self.TRACE, *args, **kwargs)
def debug(self, *args, **kwargs):
self.log(level=self.DEBUG, *args, **kwargs)
def info(self, *args, **kwargs):
self.log(level=self.INFO, *args, **kwargs)
def warn(self, *args, **kwargs):
self.log(level=self.WARN, *args, **kwargs)
def err(self, *args, **kwargs):
self.log(level=self.ERR, *args, **kwargs)
def do_log(self, subsys, level, *args):
# Don't actually log anything. Override this in subclasses
pass
# ------------------------------------------------------------------------
class LogFile(LogBase):
"""
Example logging class, logs to a file.
"""
from sys import stderr
def __init__(self, file = stderr):
self.log_file = file
subsys = { LogBase.ANY: '*', LogBase.DB: '#', LogBase.CACHE: '@', LogBase.AUTH: '!' }
def make_log_string(self, subsys, *args):
s = u'%s %s' % (self.subsys[subsys], u', '.join(args))
return s
def do_log(self, subsys, level, *args):
print >>self.log_file, self.make_log_string(subsys, *args).encode('utf-8')
# ------------------------------------------------------------------------
class LogStderr(LogFile):
"""
Compat class
"""
pass
# ------------------------------------------------------------------------
class LogDatedStderr(LogFile):
"""
Another simple logging class, prefixes log output with current time.
"""
def make_log_string(self, subsys, *args):
from datetime import datetime
now = datetime.now().isoformat()
return u"%s %s" % ( now, super(LogDatedStderr, self).make_log_string(subsys, *args) )
# ------------------------------------------------------------------------
class LogStdout(LogDatedStderr):
"""
Compat class.
"""
def __init__(self):
from sys import stdout
super(LogStdout, self).__init__(file=stdout)
# ------------------------------------------------------------------------
# Finally instanciate the logger, yeah!
logger = get_object(settings.FEINCMS_LOGGING_CLASS)()
# ------------------------------------------------------------------------