Example #1
0
class Logger:
  
  #beyond these, email will be triggered.
  threshold_failure = 4
  threshold_latency = 4
  threshold_count = 4

  '''
    It :
      - Creates LogHandler instance to write metrics to log file.
      - Creates threading.local() instance to create thread specific variables.
  '''
  def __init__ (self, service, configFile):
    try:
      print "logger instantiated"
      self.logHandler = LogHandler(service, configFile)
    except Exception as error:
      monitorLog.logError("Cannot Instantiate Logger with configFile : " + configFile, `error`)
      raise IncorrectConfigException("Cannot Instantiate Logger with configFile : " + configFile)
    self.threadLocal = threading.local()
    self.counter = 0;

  '''
    If the given action is failed, then it will log the failure count uptil now.
    It will also return the updated counter value.
  '''
  def logIfFail (self, name, expectedReturn, counter, action, severity = 20, *args, **kwargs):
    count = self.reportCountNE(expectedReturn, counter, action, *args, **kwargs)
    if count > 0:
      try:
        print "logging failure"
        self.logHandler.appendFailCountLog(name, count, severity)	
      except Exception as error:
        monitorLog.logError("Failed to append log for metric: " + name, `error`)
        raise LoggingException("Failed to append log for metric: " + name)
    return count

  def logFailure (self, name, counter, severity = 20):
    if counter > 0:
      try:
        if counter >= Logger.threshold_failure:
          self.logHandler.appendFailCountLog(name, counter,  'CRITICAL')
        self.logHandler.appendFailCountLog(name, counter, severity)
        print "logging failure"
      except Exception as error:
        monitorLog.logError("Failed to append log for metric: " + name, `error`)
        raise LoggingException("Failed to append log for metric: " + name)
      return 1
    return 0

  def logCount (self, name, counter, severity = 20):
    if counter > 0:
      try:
        if counter >= Logger.threshold_count:
          self.logHandler.appendCountLog(name, counter,  'CRITICAL')
        self.logHandler.appendCountLog(name, counter, severity)
      except Exception as error:
        monitorLog.logError("Failed to append log for metric: " + name, `error`)
        raise LoggingException("Failed to append log for metric: " + name)
      return 1
    return 0


  '''
    Report the incremented counter if the action has failed to pass the expectation.

  '''
  def reportCountEqual(self, expectedReturn, counter, action, *args, **kwargs):
    try:
      actualReturn = action(*args, **kwargs)
    except:
      return counter + 1
    if actualReturn == expectedReturn:
      return counter + 1
    return counter 

  '''
    Report the incremented counter if the action has passed the expectation.
  '''  
  def reportCountNE(self, expectedReturn, counter, action, *args, **kwargs):
    try:
      actualReturn = action(*args, **kwargs)
    except:
      return counter + 1
    if actualReturn == expectedReturn:
      return counter
    return counter + 1

  '''
    Starts the thread local timer.
  '''
  def startTime (self):
    #using thread local storage for start time 
    self.threadLocal.startTime = time.time()

  '''
    Stops the thread local timer and logs the execution time. 
  '''
  def reportTime (self, name, severity = 20):
    endTime = time.time()
    runTime = endTime - self.threadLocal.startTime
    try:
      if runTime >= Logger.threshold_latency:
        self.logHandler.appendTimeLog(name, runTime, 'CRITICAL')
      self.logHandler.appendTimeLog(name, runTime, severity)
    except Exception as error:
      monitorLog.logError("Failed to append log for metric: " + name, `error`)
      raise LoggingException("Failed to append log for metric: " + name)

  '''
    Logs the execution time of the given action and returns the value of action.
  '''
  def reportLatency (self, name, action, severity = 20, *args, **kwargs):
    self.startTime()
    try:
      actualReturn = action(*args, **kwargs)
    except Exception as error:
      monitorLog.logError("Failed Action " + `action`, `error`)
      raise Exception("Failed Action :" + `action`)
    self.reportTime(name, severity)
    return actualReturn
class Logger:


  '''
    It :
      - Creates LogHandler instance to write metrics to log file.
      - Creates threading.local() instance to create thread specific variables.
  '''
  def __init__ (self, service):
    self.logHandler = LogHandler(service)
    self.threadLocal = threading.local()


  '''
    If the given action is failed, then it will log the failure count uptil now.
    It will also return the updated counter value.
  '''
  def logIfFail (self, name, metricType, expectedReturn, counter, action, *args, **kwargs):
    count = self.reportCountNE(expectedReturn, counter, action, *args, **kwargs)
    if count > 0:
      self.logHandler.appendCountLog(name, metricType, count)	
    return count


  '''
    Report the incremented counter if the action has failed to pass the expectation.
  '''
  def reportCountEqual(self, expectedReturn, counter, action, *args, **kwargs):
    try:
      actualReturn = action(*args, **kwargs)
    except:
      return counter + 1
    if actualReturn == expectedReturn:
      return counter + 1
    return counter 

  '''
    Report the incremented counter if the action has passed the expectation.
  '''  
  def reportCountNE(self, expectedReturn, counter, action, *args, **kwargs):
    try:
      actualReturn = action(*args, **kwargs)
    except:
      return counter + 1
    if actualReturn == expectedReturn:
      return counter
    return counter + 1

  '''
    Starts the thread local timer.
  '''
  def startTime (self):
#using thread local storage for start time 
    self.threadLocal.startTime = time.time()

  '''
    Stops the thread local timer and logs the execution time. 
  '''
  def reportTime (self, name, metricType):
    endTime = time.time()
    runTime = endTime - self.threadLocal.startTime
    self.logHandler.appendTimeLog(name, metricType, runTime)

  '''
    Logs the execution time of the given action and returns the value of action.
  '''
  def reportLatency (self, name, metricType, action, *args, **kwargs):
    self.startTime()
    try:
      print "Inside reportLatency try block"
      actualReturn = action(*args, **kwargs)
      print actualReturn
    except:
      print "Inside reportLatency except block"
      #monitorLogs.logError("Error")
    self.reportTime(name, metricType)
    return actualReturn