Back to index

plone3  3.1.7
Log.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## GroupUserFolder
00003 ## Copyright (C)2006 Ingeniweb
00004 
00005 ## This program is free software; you can redistribute it and/or modify
00006 ## it under the terms of the GNU General Public License as published by
00007 ## the Free Software Foundation; either version 2 of the License, or
00008 ## (at your option) any later version.
00009 
00010 ## This program is distributed in the hope that it will be useful,
00011 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ## GNU General Public License for more details.
00014 
00015 ## You should have received a copy of the GNU General Public License
00016 ## along with this program; see the file COPYING. If not, write to the
00017 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 """
00019 One can override the following variables :
00020 
00021 LOG_LEVEL : The log level, from 0 to 5.
00022 A Log level n implies all logs from 0 to n.
00023 LOG_LEVEL MUST BE OVERRIDEN !!!!!
00024 
00025 
00026 LOG_NONE = 0            => No log output
00027 LOG_CRITICAL = 1        => Critical problems (data consistency, module integrity, ...)
00028 LOG_ERROR = 2           => Error (runtime exceptions, ...)
00029 LOG_WARNING = 3         => Warning (non-blocking exceptions, ...)
00030 LOG_NOTICE = 4          => Notices (Special conditions, ...)
00031 LOG_DEBUG = 5           => Debug (Debugging information)
00032 
00033 
00034 LOG_PROCESSOR : A dictionnary holding, for each key, the data processor.
00035 A data processor is a function that takes only one parameter : the data to print.
00036 Default : LogFile for all keys.
00037 """
00038 __version__ = "$Revision:  $"
00039 # $Source:  $
00040 # $Id: Log.py 33402 2006-11-11 12:26:18Z shh42 $
00041 __docformat__ = 'restructuredtext'
00042 
00043 
00044 
00045 LOG_LEVEL = -1
00046 
00047 LOG_NONE = 0
00048 LOG_CRITICAL = 1
00049 LOG_ERROR = 2
00050 LOG_WARNING = 3
00051 LOG_NOTICE = 4
00052 LOG_DEBUG = 5
00053 
00054 from sys import stdout, stderr, exc_info
00055 import time
00056 import thread
00057 import threading
00058 import traceback
00059 import os
00060 import pprint
00061 import string
00062 
00063 LOG_STACK_DEPTH = [-2]
00064 
00065 def Log(level, *args):
00066     """
00067     Log(level, *args) => Pretty-prints data on the console with additional information.
00068     """
00069     if LOG_LEVEL and level <= LOG_LEVEL:
00070         if not level in LOG_PROCESSOR.keys():
00071             raise ValueError, "Invalid log level :", level
00072 
00073         stack = ""
00074         stackItems = traceback.extract_stack()
00075         for depth in LOG_STACK_DEPTH:
00076             stackItem = stackItems[depth]
00077             stack = "%s%s:%s:" % (stack, os.path.basename(stackItem[0]), stackItem[1],)
00078         pr = "%8s %s%s: " % (
00079             LOG_LABEL[level],
00080             stack,
00081             time.ctime(time.time()),
00082             )
00083         for data in args:
00084             try:
00085                 if "\n" in data:
00086                     data = data
00087                 else:
00088                     data = pprint.pformat(data)
00089             except:
00090                 data = pprint.pformat(data)
00091             pr = pr + data + " "
00092 
00093         LOG_PROCESSOR[level](level, LOG_LABEL[level], pr, )
00094 
00095 def LogCallStack(level, *args):
00096     """
00097     LogCallStack(level, *args) => View the whole call stack for the specified call
00098     """
00099     if LOG_LEVEL and level <= LOG_LEVEL:
00100         if not level in LOG_PROCESSOR.keys():
00101             raise ValueError, "Invalid log level :", level
00102 
00103         stack = string.join(traceback.format_list(traceback.extract_stack()[:-1]))
00104         pr = "%8s %s:\n%s\n" % (
00105             LOG_LABEL[level],
00106             time.ctime(time.time()),
00107             stack
00108             )
00109         for data in args:
00110             try:
00111                 if "\n" in data:
00112                     data = data
00113                 else:
00114                     data = pprint.pformat(data)
00115             except:
00116                 data = pprint.pformat(data)
00117             pr = pr + data + " "
00118 
00119         LOG_PROCESSOR[level](level, LOG_LABEL[level], pr, )
00120 
00121 
00122 
00123 def FormatStack(stack):
00124     """
00125     FormatStack(stack) => string
00126 
00127     Return a 'loggable' version of the stack trace
00128     """
00129     ret = ""
00130     for s in stack:
00131         ret = ret + "%s:%s:%s: %s\n" % (os.path.basename(s[0]), s[1], s[2], s[3])
00132     return ret
00133 
00134 
00135 def LogException():
00136     """
00137     LogException () => None
00138 
00139     Print an exception information on the console
00140     """
00141     Log(LOG_NOTICE, "EXCEPTION >>>")
00142     traceback.print_exc(file = LOG_OUTPUT)
00143     Log(LOG_NOTICE, "<<< EXCEPTION")
00144 
00145 
00146 LOG_OUTPUT = stderr
00147 def LogFile(level, label, data, ):
00148     """
00149     LogFile : writes data to the LOG_OUTPUT file.
00150     """
00151     LOG_OUTPUT.write(data+'\n')
00152     LOG_OUTPUT.flush()
00153 
00154 logFile = LogFile
00155 
00156 import logging
00157 
00158 CUSTOM_TRACE = 5
00159 logging.addLevelName('TRACE', CUSTOM_TRACE)
00160 
00161 zLogLevelConverter = {
00162     LOG_NONE: CUSTOM_TRACE,
00163     LOG_CRITICAL: logging.CRITICAL,
00164     LOG_ERROR: logging.ERROR,
00165     LOG_WARNING: logging.WARNING,
00166     LOG_NOTICE: logging.INFO,
00167     LOG_DEBUG: logging.DEBUG,
00168     }
00169 
00170 def LogzLog(level, label, data, ):
00171     """
00172     LogzLog : writes data though Zope's logging facility
00173     """
00174     logger = logging.getLogger('GroupUserFolder')
00175     logger.log(zLogLevelConverter[level], data + "\n", )
00176 
00177 
00178 
00179 LOG_PROCESSOR = {
00180     LOG_NONE: LogzLog,
00181     LOG_CRITICAL: LogzLog,
00182     LOG_ERROR: LogzLog,
00183     LOG_WARNING: LogzLog,
00184     LOG_NOTICE: LogzLog,
00185     LOG_DEBUG: LogFile,
00186     }
00187 
00188 
00189 LOG_LABEL = {
00190     LOG_NONE: "",
00191     LOG_CRITICAL: "CRITICAL",
00192     LOG_ERROR:    "ERROR   ",
00193     LOG_WARNING:  "WARNING ",
00194     LOG_NOTICE:   "NOTICE  ",
00195     LOG_DEBUG:    "DEBUG   ",
00196     }