logger.py
上传用户:quxuerui
上传日期:2018-01-08
资源大小:41811k
文件大小:28k
源码类别:

网格计算

开发平台:

Java

  1. #Licensed to the Apache Software Foundation (ASF) under one
  2. #or more contributor license agreements.  See the NOTICE file
  3. #distributed with this work for additional information
  4. #regarding copyright ownership.  The ASF licenses this file
  5. #to you under the Apache License, Version 2.0 (the
  6. #"License"); you may not use this file except in compliance
  7. #with the License.  You may obtain a copy of the License at
  8. #     http://www.apache.org/licenses/LICENSE-2.0
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. """hodLogger provides a customized interface to Python's core logging package.
  15. """
  16. import sys, os, re, logging, logging.handlers, inspect, pprint, types
  17. from tcp import get_address_tuple
  18. fileFormatString    = '[%(asctime)s] %(levelname)s/%(levelno)s 
  19. %(module)s:%(lineno)s - %(message)s'
  20. streamFormatString  = '%(levelname)s - %(message)s'
  21. debugStreamFormatString = '[%(asctime)s] %(levelname)s/%(levelno)s 
  22. %(module)s:%(lineno)s - %(message)s'
  23. syslogFormatString = '(%(process)d) %(levelname)s/%(levelno)s 
  24. %(module)s:%(lineno)s - %(message)s'
  25. smtpFormatString    = '[%(asctime)s] %(levelname)s/%(levelno)s 
  26. %(module)s:%(lineno)snn%(message)s'
  27. fileFormater = logging.Formatter(fileFormatString)
  28. streamFormater = logging.Formatter(streamFormatString)
  29. debugStreamFormater = logging.Formatter(debugStreamFormatString)
  30. syslogFormater = logging.Formatter(syslogFormatString)
  31. smtpFormater = logging.Formatter(smtpFormatString)
  32. defaultFileLevel = 3
  33. defaultStreamLevel = 4
  34. defaultSyslogLevel = 3
  35. defaultSmtpLevel = 0
  36. hodLogLevelMap = { 0 : logging.CRITICAL,
  37.                    1 : logging.ERROR,
  38.                    2 : logging.WARNING,
  39.                    3 : logging.INFO,
  40.                    4 : logging.DEBUG    }
  41. hodStreamFormatMap = { 0 : streamFormater,
  42.                        1 : streamFormater,
  43.                        2 : streamFormater,
  44.                        3 : streamFormater,
  45.                        4 : debugStreamFormater }
  46. rehodLogLevelMap = {}
  47. for key in hodLogLevelMap.keys():
  48.     rehodLogLevelMap[hodLogLevelMap[key]] = key
  49. reModule = re.compile("^(.*)..*$")
  50. hodLogs = {}
  51. class hodRotatingFileHandler(logging.handlers.RotatingFileHandler):
  52.     """ This class needs to be used in place of RotatingFileHandler when
  53.         the 2.4.0 Python interpreter is used."""
  54.     def emit(self, record):
  55.         """
  56.         Emit a record.
  57.         If a formatter is specified, it is used to format the record.
  58.         The record is then written to the stream with a trailing newline
  59.         [N.B. this may be removed depending on feedback]. If exception
  60.         information is present, it is formatted using
  61.         traceback.print_exception and appended to the stream.
  62.         *****
  63.         THIS IS A HACK, when instances of hodLogger get passed to the child of
  64.         a child thread for some reason self.stream gets closed.  This version
  65.         of emit re-opens self.stream if it is closed.  After testing it appears
  66.         that self.stream is only closed once after the second thread is
  67.         initialized so there is not performance penalty to this hack.  This
  68.         problem only exists in python 2.4.
  69.         *****
  70.         """
  71.         try:
  72.             if self.shouldRollover(record):
  73.                 self.doRollover()
  74.             try:
  75.                 msg = self.format(record)
  76.                 fs = "%sn"
  77.                 if not hasattr(types, "UnicodeType"): #if no unicode support...
  78.                     self.stream.write(fs % msg)
  79.                 else:
  80.                     try:
  81.                         self.stream.write(fs % msg)
  82.                     except UnicodeError:
  83.                         self.stream.write(fs % msg.encode("UTF-8"))
  84.                     except ValueError:
  85.                         self.stream = open(self.baseFilename, self.mode)
  86.                         self.stream.write(fs % msg)
  87.                 self.flush()
  88.             except:
  89.                 self.handleError(record)
  90.         except:
  91.             self.handleError(record)
  92.     def shouldRollover(self, record):
  93.         """
  94.         Determine if rollover should occur.
  95.         Basically, see if the supplied record would cause the file to exceed
  96.         the size limit we have.
  97.         *****
  98.         THIS IS A HACK, when instances of hodLogger get passed to the child of
  99.         a child thread for some reason self.stream gets closed.  This version
  100.         of emit re-opens self.stream if it is closed.  After testing it appears
  101.         that self.stream is only closed once after the second thread is
  102.         initialized so there is not performance penalty to this hack. This
  103.         problem only exists in python 2.4.
  104.         *****
  105.         """
  106.         if self.maxBytes > 0:                   # are we rolling over?
  107.             msg = "%sn" % self.format(record)
  108.             try:
  109.                 #due to non-posix-compliant Windows feature
  110.                 self.stream.seek(0, 2)
  111.             except ValueError:
  112.                 self.stream = open(self.baseFilename, self.mode)
  113.                 self.stream.seek(0, 2)
  114.             if self.stream.tell() + len(msg) >= self.maxBytes:
  115.                 return 1
  116.         return 0
  117. class hodCustomLoggingLogger(logging.Logger):
  118.     """ Slight extension of the logging.Logger class used by the hodLog class.
  119.     """
  120.     def findCaller(self):
  121.         """ findCaller() is supposed to return the callers file name and line
  122.             number of the caller. This was broken when the logging package was
  123.             wrapped by hodLog.  We should return much more relevant info now.
  124.             """
  125.         callerModule = ''
  126.         callerLine = 0
  127.         currentModule = os.path.basename(__file__)
  128.         currentModule = reModule.sub("g<1>", currentModule)
  129.         frames = inspect.stack()
  130.         for i in range(len(frames)):
  131.             frameModule = os.path.basename(frames[i][1])
  132.             frameModule = reModule.sub("g<1>", frameModule)
  133.             if frameModule == currentModule:
  134.                 previousFrameModule = os.path.basename(frames[i+1][1])
  135.                 previousFrameModule = reModule.sub("g<1>",
  136.                     previousFrameModule)
  137.                 callerFile = frames[i+1][1]
  138.                 callerLine = frames[i+1][2]
  139.                 continue
  140.         returnValues = (callerFile, callerLine)
  141.         if sys.version.startswith('2.4.4') or sys.version.startswith('2.5'):
  142.             returnValues = (callerFile, callerLine, None)
  143.             
  144.         return returnValues
  145. class hodLog:
  146.     """ Cluster management logging class.
  147.         logging levels: 0 - log only critical messages
  148.                         1 - log critical and error messages
  149.                         2 - log critical, error, and warning messages
  150.                         3 - log critical, error, warning, and info messages
  151.                         4 - log critical, error, warning, info, and debug
  152.                             messages"""
  153.     def __init__(self, appName):
  154.         """Constructs a hodLogger object.
  155.         appName      - name of logging application, log filenames will be
  156.                        prepended with this name"""
  157.         self.__appName = appName
  158.         # initialize a dictionary to hold loggerNames
  159.         self.__loggerNames = {}
  160.         # initialize a dictionary to track log handlers and handler classes
  161.         self.__logObjs = { 'file' : {}, 'smtp' : {}, 
  162.                            'syslog' : {}, 'strm' : {} }
  163.         # use a custom logging.Logger class
  164.         logging.setLoggerClass(hodCustomLoggingLogger)
  165.         # get the root app logger
  166.         self.__logger = logging.getLogger(appName)
  167.         self.__logger.setLevel(logging.DEBUG)
  168.         
  169.         hodLogs[self.__appName] = self
  170.     def __attr__(self, attrname):
  171.         """loggerNames  - list of defined logger names"""
  172.         if attrname   == "loggerNames":  return self.__loggerNames.keys()
  173.         else: raise AttributeError, attrname
  174.     def __repr__(self):
  175.         """Returns a string representation of a hodLog object of the form:
  176.            LOG_NAME
  177.                 file: FILENAME (level LEVEL)
  178.                 smtp: SMTP_SERVER from FROM_ADDRESS (level LEVEL)
  179.                 strm: STRM_OBJECT (level LEVEL)
  180.                 ... """
  181.         hodLogString = "hodLog: %snn" % self.__appName
  182.         for loggerName in self.__loggerNames.keys():
  183.             hodLogString = "%s    logger: %sn" % (hodLogString, loggerName)
  184.             handlerClasses = self.__logObjs.keys()
  185.             handlerClasses.sort()
  186.             for handlerClass in handlerClasses:
  187.                 try:
  188.                     loggerLevelName = logging.getLevelName(
  189.                         self.__logObjs[handlerClass][loggerName]['level'])
  190.                     hodLogString = "%s        %s: %s (level %s)n" % (
  191.                         hodLogString, handlerClass,
  192.                         self.__logObjs[handlerClass][loggerName]['data'],
  193.                         loggerLevelName)
  194.                 except:
  195.                     hodLogString = "%s        %s: nonen" % (
  196.                         hodLogString, handlerClass)
  197.             hodLogString = "%sn" % hodLogString
  198.         return hodLogString
  199.     # 'private' method which adds handlers to self.__logObjs
  200.     def __add_to_handlers(self, handlerClass, loggerName, handler, data,
  201.         level):
  202.         self.__logObjs[handlerClass][loggerName] = {}
  203.         self.__logObjs[handlerClass][loggerName]['handler'] = handler
  204.         self.__logObjs[handlerClass][loggerName]['data'] = data
  205.         self.__logObjs[handlerClass][loggerName]['level'] = level
  206.     # 'private' method which determines whether a hod log level is valid and
  207.     #   returns a valid logging.Logger level
  208.     def __get_logging_level(self, level, defaultLevel):
  209.         loggingLevel = ''
  210.         try:
  211.             loggingLevel = hodLogLevelMap[int(level)]
  212.         except:
  213.             loggingLevel = hodLogLevelMap[defaultLevel]
  214.         return loggingLevel
  215.     # make a logging.logger name rootLogger.childLogger in our case the
  216.     #   appName.componentName
  217.     def __get_logging_logger_name(self, loggerName):
  218.         return "%s.%s" % (self.__appName, loggerName)
  219.     def add_logger(self, loggerName):
  220.         """Adds a logger of name loggerName.
  221.            loggerName    - name of component of a given application doing the
  222.                            logging
  223.            Returns a hodLogger object for the just added logger."""
  224.         try:
  225.             self.__loggerNames[loggerName]
  226.         except:
  227.             loggingLoggerName = self.__get_logging_logger_name(loggerName)
  228.             logging.getLogger(loggingLoggerName)
  229.             self.__loggerNames[loggerName] = 1
  230.             return hodLogger(self.__appName, loggingLoggerName)
  231.     def add_file(self, logDirectory, maxBytes=0, backupCount=0,
  232.         level=defaultFileLevel, addToLoggerNames=None):
  233.         """Adds a file handler to all defined loggers or a specified set of
  234.            loggers.  Each log file will be located in logDirectory and have a
  235.            name of the form appName-loggerName.log.
  236.            logDirectory     - logging directory
  237.            maxBytes         - maximum log size to write in bytes before rotate
  238.            backupCount      - number of rotated logs to keep
  239.            level            - cluster management log level
  240.            addToLoggerNames - list of logger names to which stream handling
  241.                               will be added"""
  242.         def add_file_handler(loggerName):
  243.             if not self.__logObjs['file'].has_key(loggerName):
  244.                 loggingLevel = self.__get_logging_level(level,
  245.                     defaultFileLevel)
  246.                 logFile = os.path.join(logDirectory, "%s-%s.log" % (
  247.                     self.__appName, loggerName))
  248.                 logFilePresent = False
  249.                 if(os.path.exists(logFile)):
  250.                   logFilePresent = True
  251.                 if sys.version.startswith('2.4'):
  252.                     fileHandler = hodRotatingFileHandler(logFile,
  253.                         maxBytes=maxBytes, backupCount=backupCount)
  254.                 else:
  255.                     fileHandler = logging.handlers.RotatingFileHandler(logFile,
  256.                         maxBytes=maxBytes, backupCount=backupCount)
  257.                 if logFilePresent and backupCount:
  258.                   fileHandler.doRollover()
  259.                 fileHandler.setLevel(loggingLevel)
  260.                 fileHandler.setFormatter(fileFormater)
  261.                 loggingLoggerName = self.__get_logging_logger_name(loggerName)
  262.                 aLogger = logging.getLogger(loggingLoggerName)
  263.                 aLogger.addHandler(fileHandler)
  264.                 fileData = "%s" % logFile
  265.                 self.__add_to_handlers('file', loggerName, fileHandler,
  266.                     fileData, loggingLevel)
  267.         if addToLoggerNames:
  268.             for loggerName in addToLoggerNames:
  269.                 add_file_handler(loggerName)
  270.         else:
  271.             for loggerName in self.__loggerNames:
  272.                 add_file_handler(loggerName)
  273.     def add_stream(self, stream=sys.stderr, level=defaultStreamLevel,
  274.         addToLoggerNames=None):
  275.         """Adds a stream handler to all defined loggers or a specified set of
  276.            loggers.
  277.            stream           - a stream such as sys.stderr or sys.stdout
  278.            level            - cluster management log level
  279.            addToLoggerNames - tupple of logger names to which stream handling
  280.                               will be added"""
  281.         def add_stream_handler(loggerName):
  282.             if not self.__logObjs['strm'].has_key(loggerName):
  283.                 loggingLevel = self.__get_logging_level(level,
  284.                     defaultStreamLevel)
  285.                 streamHandler = logging.StreamHandler(stream)
  286.                 
  287.                 streamHandler.setLevel(loggingLevel)
  288.                 
  289.                 streamHandler.setFormatter(hodStreamFormatMap[int(level)])
  290.                 loggingLoggerName = self.__get_logging_logger_name(loggerName)
  291.                 aLogger = logging.getLogger(loggingLoggerName)
  292.                 aLogger.addHandler(streamHandler)
  293.                 streamData = "%s" % stream
  294.                 self.__add_to_handlers('strm', loggerName, streamHandler,
  295.                     streamData, loggingLevel)
  296.         if addToLoggerNames:
  297.             for loggerName in addToLoggerNames:
  298.                 add_stream_handler(loggerName)
  299.         else:
  300.             for loggerName in self.__loggerNames:
  301.                 add_stream_handler(loggerName)
  302.     def add_syslog(self, address, level=defaultSyslogLevel, 
  303.                    addToLoggerNames=None):
  304.         def add_syslog_handler(loggerName):
  305.             if not self.__logObjs['syslog'].has_key(loggerName):
  306.                 loggingLevel = self.__get_logging_level(level,
  307.                     defaultSyslogLevel)
  308.                 address[1] = int(address[1])
  309.                 syslogHandler = logging.handlers.SysLogHandler(tuple(address),
  310.                                                                9)
  311.                 
  312.                 syslogHandler.setLevel(loggingLevel)
  313.                 
  314.                 syslogHandler.setFormatter(syslogFormater)
  315.                 loggingLoggerName = self.__get_logging_logger_name(loggerName)
  316.                 aLogger = logging.getLogger(loggingLoggerName)
  317.                 aLogger.addHandler(syslogHandler)
  318.                 syslogData = "%s:%s" % (address[0], address[1])
  319.                 self.__add_to_handlers('syslog', loggerName, syslogHandler,
  320.                     syslogData, loggingLevel)
  321.         if addToLoggerNames:
  322.             for loggerName in addToLoggerNames:
  323.                 add_syslog_handler(loggerName)
  324.         else:
  325.             for loggerName in self.__loggerNames:
  326.                 add_syslog_handler(loggerName)      
  327.       
  328.     def add_smtp(self, mailHost, fromAddress, toAddresses,
  329.         level=defaultSmtpLevel, addToLoggerNames=None):
  330.         """Adds an SMTP handler to all defined loggers or a specified set of
  331.            loggers.
  332.            mailHost         - SMTP server to used when sending mail
  333.            fromAddress      - email address to use as the from address when
  334.                               sending mail
  335.            toAdresses       - comma seperated list of email address to which
  336.                               mail will be sent
  337.            level            - cluster management log level
  338.            addToLoggerNames - tupple of logger names to which smtp handling
  339.                               will be added"""
  340.         def add_email_handler(loggerName):
  341.             if not self.__logObjs['smtp'].has_key(loggerName):
  342.                 loggingLevel = self.__get_logging_level(level,
  343.                     defaultSmtpLevel)
  344.                 subject = loggerName
  345.                 if   loggingLevel == 50:
  346.                     subject = "%s - a critical error has occured." % subject
  347.                 elif loggingLevel == 40:
  348.                     subject = "%s - an error has occured."         % subject
  349.                 elif loggingLevel == 30:
  350.                     subject = "%s - warning message."              % subject
  351.                 elif loggingLevel == 20:
  352.                     subject = "%s - information message."          % subject
  353.                 elif loggingLevel == 10:
  354.                     subject = "%s - debugging message."            % subject
  355.                 mailHostTuple = get_address_tuple(mailHost)
  356.                 emailHandler = logging.handlers.SMTPHandler(mailHostTuple,
  357.                     fromAddress, toAddresses, subject)
  358.                 emailHandler.setFormatter(smtpFormater)
  359.                 emailHandler.setLevel(loggingLevel)
  360.                 loggingLoggerName = self.__get_logging_logger_name(loggerName)
  361.                 aLogger = logging.getLogger(loggingLoggerName)
  362.                 aLogger.addHandler(emailHandler)
  363.                 emailData = "%s from %s" % (mailHost, fromAddress)
  364.                 self.__add_to_handlers('smtp', loggerName, emailHandler,
  365.                     emailData, loggingLevel)
  366.         if addToLoggerNames:
  367.             for loggerName in addToLoggerNames:
  368.                 add_email_handler(loggerName)
  369.         else:
  370.             for loggerName in self.__loggerNames:
  371.                 add_email_handler(loggerName)
  372.     def status(self):
  373.         statusStruct = {}
  374.         for loggerName in self.__loggerNames.keys():
  375.             statusStruct[loggerName] = []
  376.             for handlerClass in self.__logObjs.keys():
  377.                 loggerDict = {}
  378.                 try:
  379.                     level = self.__logObjs[handlerClass][loggerName]['level']
  380.                     level = rehodLogLevelMap[level]
  381.                     loggerDict['handler'] = handlerClass
  382.                     loggerDict['level'] = level
  383.                     loggerDict['data'] = 
  384.                         self.__logObjs[handlerClass][loggerName]['data']
  385.                 except:
  386.                     pass
  387.                 else:
  388.                     statusStruct[loggerName].append(loggerDict)
  389.         return statusStruct
  390.     def lock_handlers(self):
  391.         for handlerClass in self.__logObjs.keys():
  392.             for loggerName in self.__logObjs[handlerClass].keys():
  393.                 self.__logObjs[handlerClass][loggerName]['handler'].acquire()
  394.     def release_handlers(self):
  395.         for handlerClass in self.__logObjs.keys():
  396.             for loggerName in self.__logObjs[handlerClass].keys():
  397.                 self.__logObjs[handlerClass][loggerName]['handler'].release()
  398.     def get_level(self, handler, loggerName):
  399.         return rehodLogLevelMap[self.__logObjs[handler][loggerName]['level']]
  400.     def set_level(self, handler, loggerName, level):
  401.         """Sets the logging level of a particular logger and logger handler.
  402.            handler    - handler (smtp, file, or stream)
  403.            loggerName - logger to set level on
  404.            level      - level to set logger
  405.         """
  406.         level = self.__get_logging_level(level, defaultFileLevel)
  407.         self.__logObjs[handler][loggerName]['handler'].setLevel(level)
  408.         self.__logObjs[handler][loggerName]['level'] = level
  409.         
  410.         if handler == 'stream':
  411.             self.__logObjs[handler][loggerName]['handler'].setFormatter(
  412.                 hodStreamFormatMap[int(level)])
  413.     def set_logger_level(self, loggerName, level):
  414.         status = 0
  415.         for handlerClass in self.__logObjs.keys():
  416.             if self.__logObjs[handlerClass].has_key(loggerName):
  417.                 self.set_level(handlerClass, loggerName, level)
  418.             else:
  419.                 status = 1
  420.         
  421.         return status
  422.     def rollover(self, loggerName):
  423.         status = 0 
  424.         if self.__logObjs['file'].has_key(loggerName):
  425.             if self.__logObjs['file'][loggerName]['handler'].shouldRollover():
  426.                 self.__logObjs['file'][loggerName]['handler'].doRollover()
  427.         else:
  428.             status = 1
  429.             
  430.         return status
  431.         
  432.     def set_max_bytes(self, maxBytes):
  433.         status = 0
  434.         if self.__logObjs.has_key('file'):
  435.             for loggerName in self.__logObjs['file'].keys():
  436.                 self.__logObjs['file'][loggerName]['handler'].maxBytes = 0
  437.         else:
  438.             status = 1
  439.             
  440.         return status
  441.     def get_logger(self, loggerName):
  442.         """ Returns a hodLogger object for a logger by name. """
  443.         loggingLoggerName = self.__get_logging_logger_name(loggerName)
  444.         return hodLogger(self.__appName, loggingLoggerName)
  445.     def critical(self, loggerName, msg):
  446.         """Logs a critical message and flushes log buffers.  This method really
  447.            should only be called upon a catastrophic failure.
  448.            loggerName - logger to use
  449.            msg        - message to be logged"""
  450.         loggingLoggerName = self.__get_logging_logger_name(loggerName)
  451.         logger = logging.getLogger(loggingLoggerName)
  452.         logger.critical(msg)
  453.         self.flush()
  454.     def error(self, loggerName, msg):
  455.         """Logs an error message and flushes log buffers.
  456.            loggerName - logger to use
  457.            msg        - message to be logged"""
  458.         loggingLoggerName = self.__get_logging_logger_name(loggerName)
  459.         logger = logging.getLogger(loggingLoggerName)
  460.         logger.error(msg)
  461.         self.flush()
  462.     def warn(self, loggerName, msg):
  463.         """Logs a warning message.
  464.            loggerName - logger to use
  465.            msg        - message to be logged"""
  466.         loggingLoggerName = self.__get_logging_logger_name(loggerName)
  467.         logger = logging.getLogger(loggingLoggerName)
  468.         logger.warn(msg)
  469.     def info(self, loggerName, msg):
  470.         """Logs an information message.
  471.            loggerName - logger to use
  472.            msg        - message to be logged"""
  473.         loggingLoggerName = self.__get_logging_logger_name(loggerName)
  474.         logger = logging.getLogger(loggingLoggerName)
  475.         logger.info(msg)
  476.     def debug(self, loggerName, msg):
  477.         """Logs a debugging message.
  478.            loggerName - logger to use
  479.            msg        - message to be logged"""
  480.         loggingLoggerName = self.__get_logging_logger_name(loggerName)
  481.         logger = logging.getLogger(loggingLoggerName)
  482.         logger.debug(msg)
  483.     def flush(self):
  484.         """Flush all log handlers."""
  485.         for handlerClass in self.__logObjs.keys():
  486.             for loggerName in self.__logObjs[handlerClass].keys():
  487.                 self.__logObjs[handlerClass][loggerName]['handler'].flush()
  488.     def shutdown(self):
  489.         """Shutdown all logging, flushing all buffers."""
  490.         for handlerClass in self.__logObjs.keys():
  491.             for loggerName in self.__logObjs[handlerClass].keys():
  492.                 self.__logObjs[handlerClass][loggerName]['handler'].flush()
  493.                 # Causes famous 'ValueError: I/O operation on closed file'
  494.                 # self.__logObjs[handlerClass][loggerName]['handler'].close()
  495. class hodLogger:
  496.     """ Encapsulates a particular logger from a hodLog object. """
  497.     def __init__(self, appName, loggingLoggerName):
  498.         """Constructs a hodLogger object (a particular logger in a hodLog
  499.            object).
  500.            loggingLoggerName - name of a logger in hodLog object"""
  501.         self.__appName = appName
  502.         self.__loggerName = loggingLoggerName
  503.         self.__logger = logging.getLogger(self.__loggerName)
  504.     def __repr__(self):
  505.         """Returns a string representation of a hodComponentLog object."""
  506.         return "%s hodLog" % self.__loggerName
  507.     def __call__(self):
  508.         pass
  509.     def set_logger_level(self, loggerName, level):
  510.         
  511.         return hodLogs[self.__appName].set_logger_level(loggerName, level)
  512.         
  513.     def set_max_bytes(self, maxBytes):
  514.             
  515.         return hodLogs[self.__appName].set_max_bytes(maxBytes)
  516.     def rollover(self):
  517.         return hodLogs[self.__appName].rollover(self.__loggerName)
  518.     def get_level(self, handler, loggerName):
  519.     
  520.         return hodLogs[self.__appName].get_level(handler, loggerName)
  521.         
  522.     def critical(self, msg):
  523.         """Logs a critical message and calls sys.exit(1).
  524.            msg     - message to be logged"""
  525.         self.__logger.critical(msg)
  526.     def error(self, msg):
  527.         """Logs an error message.
  528.            msg     - message to be logged"""
  529.         self.__logger.error(msg)
  530.     def warn(self, msg):
  531.         """Logs a warning message.
  532.            msg     - message to be logged"""
  533.         self.__logger.warn(msg)
  534.     def info(self, msg):
  535.         """Logs an information message.
  536.            msg     - message to be logged"""
  537.         self.__logger.info(msg)
  538.     def debug(self, msg):
  539.         """Logs a debugging message.
  540.            msg     - message to be logged"""
  541.         self.__logger.debug(msg)
  542. class hodDummyLogger:
  543.     """ Dummy hodLogger class.  Other hod classes requiring a hodLogger default
  544.         to this hodLogger if no logger is passed."""
  545.     def __init__(self):
  546.         """pass"""
  547.         pass
  548.     def __repr__(self):
  549.         return "dummy hodLogger"
  550.     def __call__(self):
  551.         """pass"""
  552.         pass
  553.     def set_logger_level(self, loggerName, level):
  554.         
  555.         return 0
  556.         
  557.     def set_max_bytes(self, loggerName, maxBytes):
  558.             
  559.         return 0
  560.     def get_level(self, handler, loggerName):
  561.         
  562.         return 4
  563.     def rollover(self):
  564.         
  565.         return 0
  566.     def critical(self, msg):
  567.         """pass"""
  568.         pass
  569.     def error(self, msg):
  570.         """pass"""
  571.         pass
  572.     def warn(self, msg):
  573.         """pass"""
  574.         pass
  575.     def info(self, msg):
  576.         """pass"""
  577.         pass
  578.     def debug(self, msg):
  579.         """pass"""
  580.         pass
  581. def ensureLogDir(logDir):
  582.   """Verify that the passed in log directory exists, and if it doesn't
  583.   create it."""
  584.   if not os.path.exists(logDir):
  585.     try:
  586.       old_mask = os.umask(0)
  587.       os.makedirs(logDir, 01777)
  588.       os.umask(old_mask)
  589.     except Exception, e:
  590.       print >>sys.stderr, "Could not create log directories %s. Exception: %s. Stack Trace: %s" % (logDir, get_exception_error_string(), get_exception_string())
  591.       raise e
  592. def getLogger(cfg, logName):
  593.   if cfg['debug'] > 0:
  594.     user = cfg['userid']
  595.     baseLogger = hodLog(logName)
  596.     log = baseLogger.add_logger('main')
  597.     if cfg.has_key('log-dir'):
  598.       serviceId = os.getenv('PBS_JOBID')
  599.       if serviceId:
  600.         logDir = os.path.join(cfg['log-dir'], "%s.%s" % (user, serviceId))
  601.       else:
  602.         logDir = os.path.join(cfg['log-dir'], user) 
  603.       if not os.path.exists(logDir):
  604.         os.mkdir(logDir)
  605.       baseLogger.add_file(logDirectory=logDir, level=cfg['debug'], 
  606.                addToLoggerNames=('main',))
  607.     try:
  608.       if cfg.has_key('stream') and cfg['stream']:
  609.         baseLogger.add_stream(level=cfg['debug'], addToLoggerNames=('main',))
  610.       if cfg.has_key('syslog-address'):
  611.         baseLogger.add_syslog(cfg['syslog-address'], 
  612.           level=cfg['debug'], addToLoggerNames=('main',))
  613.     except Exception,e:
  614.       # Caught an exception while initialising logger
  615.       log.critical("%s Logger failed to initialise. Reason : %s" % (logName, e))
  616.       pass
  617.     return log