1 # -*- coding: utf-8 -*-
3 ## Copyright (C)2006 Ingeniweb
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or
8 ## (at your option) any later version.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; see the file COPYING. If not, write to the
17 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 One can override the following variables :
21 LOG_LEVEL : The log level, from 0 to 5.
22 A Log level n implies all logs from 0 to n.
23 LOG_LEVEL MUST BE OVERRIDEN !!!!!
26 LOG_NONE = 0 => No log output
27 LOG_CRITICAL = 1 => Critical problems (data consistency, module integrity, ...)
28 LOG_ERROR = 2 => Error (runtime exceptions, ...)
29 LOG_WARNING = 3 => Warning (non-blocking exceptions, ...)
30 LOG_NOTICE = 4 => Notices (Special conditions, ...)
31 LOG_DEBUG = 5 => Debug (Debugging information)
34 LOG_PROCESSOR : A dictionnary holding, for each key, the data processor.
35 A data processor is a function that takes only one parameter : the data to print.
36 Default : LogFile for all keys.
38 __version__
= "$Revision: $"
40 # $Id: Log.py 33389 2006-11-11 11:24:41Z shh42 $
41 __docformat__
= 'restructuredtext'
54 from sys
import stdout
, stderr
, exc_info
63 LOG_STACK_DEPTH
= [-2]
65 def Log(level
, *args
):
67 Log(level, *args) => Pretty-prints data on the console with additional information.
69 if LOG_LEVEL
and level
<= LOG_LEVEL
:
70 if not level
in LOG_PROCESSOR
.keys():
71 raise ValueError, "Invalid log level :", level
74 stackItems
= traceback
.extract_stack()
75 for depth
in LOG_STACK_DEPTH
:
76 stackItem
= stackItems
[depth
]
77 stack
= "%s%s:%s:" % (stack
, os
.path
.basename(stackItem
[0]), stackItem
[1],)
81 time
.ctime(time
.time()),
88 data
= pprint
.pformat(data
)
90 data
= pprint
.pformat(data
)
93 LOG_PROCESSOR
[level
](level
, LOG_LABEL
[level
], pr
, )
95 def LogCallStack(level
, *args
):
97 LogCallStack(level, *args) => View the whole call stack for the specified call
99 if LOG_LEVEL
and level
<= LOG_LEVEL
:
100 if not level
in LOG_PROCESSOR
.keys():
101 raise ValueError, "Invalid log level :", level
103 stack
= string
.join(traceback
.format_list(traceback
.extract_stack()[:-1]))
104 pr
= "%8s %s:\n%s\n" % (
106 time
.ctime(time
.time()),
114 data
= pprint
.pformat(data
)
116 data
= pprint
.pformat(data
)
119 LOG_PROCESSOR
[level
](level
, LOG_LABEL
[level
], pr
, )
123 def FormatStack(stack
):
125 FormatStack(stack) => string
127 Return a 'loggable' version of the stack trace
131 ret
= ret
+ "%s:%s:%s: %s\n" % (os
.path
.basename(s
[0]), s
[1], s
[2], s
[3])
137 LogException () => None
139 Print an exception information on the console
141 Log(LOG_NOTICE
, "EXCEPTION >>>")
142 traceback
.print_exc(file = LOG_OUTPUT
)
143 Log(LOG_NOTICE
, "<<< EXCEPTION")
147 def LogFile(level
, label
, data
, ):
149 LogFile : writes data to the LOG_OUTPUT file.
151 LOG_OUTPUT
.write(data
+'\n')
158 logging
.addLevelName('TRACE', CUSTOM_TRACE
)
160 zLogLevelConverter
= {
161 LOG_NONE
: CUSTOM_TRACE
,
162 LOG_CRITICAL
: logging
.CRITICAL
,
163 LOG_ERROR
: logging
.ERROR
,
164 LOG_WARNING
: logging
.WARNING
,
165 LOG_NOTICE
: logging
.INFO
,
166 LOG_DEBUG
: logging
.DEBUG
,
169 def LogzLog(level
, label
, data
, ):
171 LogzLog : writes data though Zope's logging facility
173 logger
= logging
.getLogger('GroupUserFolder')
174 logger
.log(zLogLevelConverter
[level
], data
+ "\n", )
180 LOG_CRITICAL
: LogzLog
,
182 LOG_WARNING
: LogzLog
,
190 LOG_CRITICAL
: "CRITICAL",
192 LOG_WARNING
: "WARNING ",
193 LOG_NOTICE
: "NOTICE ",