<commit_before>import sys
import traceback
import datetime
import cStringIO
import threading
import collections

import logging
l = logging.getLogger('webapi')

#=================================================================================================================
class FakeLogger(object):
  loggingLevelNames = { logging.DEBUG: "DEBUG",
                        logging.INFO: "INFO",
                        logging.WARNING: "WARNING",
                        logging.ERROR: "ERROR",
                        logging.CRITICAL: "CRITICAL",
                        logging.FATAL: "FATAL"
                      }
  def createLogMessage(self,*x):
    try:
      loggingLevelString = FakeLogger.loggingLevelNames[x[0]]
    except KeyError:
      loggingLevelString = "Level[%s]" % str(x[0])
    message = x[1] % x[2:]
    return '%s %s' % (loggingLevelString, message)
  def log(self,*x):
    print >>sys.stderr, self.createLogMessage(*x)
  def debug(self,*x): self.log(logging.DEBUG, *x)
  def info(self,*x): self.log(logging.INFO, *x)
  def warning(self,*x): self.log(logging.WARNING, *x)
  warn = warning
  def error(self,*x): self.log(logging.ERROR, *x)
  def critical(self,*x): self.log(logging.CRITICAL, *x)
  fatal = critical


#=================================================================================================================
class SilentFakeLogger(object):
  def log(self,*x): pass
  def debug(self,*x): pass
  def info(self,*x): pass
  def warning(self,*x): pass
  def error(self,*x): pass
  def critical(self,*x): pass
  def fatal(self,*x):pass

#=================================================================================================================
class StringLogger(FakeLogger):
  def __init__(self):
    super(StringLogger, self).__init__()
    self.messages = []
  def log(self,*x):
    message = self.createLogMessage(*x)
    self.messages.append(message)
  def getMessages(self):
    log = '\n'.join(self.messages)
    self.messages = []
    return log


#=================================================================================================================
# logging routines
#-----------------------------------------------------------------------------------------------------------------

loggingReportLock = threading.RLock()

#-----------------------------------------------------------------------------------------------------------------
def reportExceptionAndContinue(logger=FakeLogger(), loggingLevel=logging.ERROR, ignoreFunction=None, showTraceback=True):
  try:
    exceptionType, exception, tracebackInfo = sys.exc_info()
    if ignoreFunction and ignoreFunction(exceptionType, exception, tracebackInfo):
      return
    if exceptionType in (KeyboardInterrupt, SystemExit):
      raise  exception
    loggingReportLock.acquire()   #make sure these multiple log entries stay together
    try:
      logger.log(loggingLevel, "%s Caught Error: %s", threading.currentThread().getName(), exceptionType)
      logger.log(loggingLevel, str(exception))
      if showTraceback:
        stringStream = cStringIO.StringIO()
        try:
          print >>stringStream,  "trace back follows:"
          traceback.print_tb(tracebackInfo, None, stringStream)
          tracebackString = stringStream.getvalue()
          logger.info(tracebackString)
        finally:<commit_msg>on logging exceptions, the traceback was always at the info logging level rather than at the logging level  requested by the input parameter.  This is corrected.<commit_after>import sys
import traceback
import datetime
import cStringIO
import threading
import collections

import logging
l = logging.getLogger('webapi')

#=================================================================================================================
class FakeLogger(object):
  loggingLevelNames = { logging.DEBUG: "DEBUG",
                        logging.INFO: "INFO",
                        logging.WARNING: "WARNING",
                        logging.ERROR: "ERROR",
                        logging.CRITICAL: "CRITICAL",
                        logging.FATAL: "FATAL"
                      }
  def createLogMessage(self,*x):
    try:
      loggingLevelString = FakeLogger.loggingLevelNames[x[0]]
    except KeyError:
      loggingLevelString = "Level[%s]" % str(x[0])
    message = x[1] % x[2:]
    return '%s %s' % (loggingLevelString, message)
  def log(self,*x):
    print >>sys.stderr, self.createLogMessage(*x)
  def debug(self,*x): self.log(logging.DEBUG, *x)
  def info(self,*x): self.log(logging.INFO, *x)
  def warning(self,*x): self.log(logging.WARNING, *x)
  warn = warning
  def error(self,*x): self.log(logging.ERROR, *x)
  def critical(self,*x): self.log(logging.CRITICAL, *x)
  fatal = critical


#=================================================================================================================
class SilentFakeLogger(object):
  def log(self,*x): pass
  def debug(self,*x): pass
  def info(self,*x): pass
  def warning(self,*x): pass
  def error(self,*x): pass
  def critical(self,*x): pass
  def fatal(self,*x):pass

#=================================================================================================================
class StringLogger(FakeLogger):
  def __init__(self):
    super(StringLogger, self).__init__()
    self.messages = []
  def log(self,*x):
    message = self.createLogMessage(*x)
    self.messages.append(message)
  def getMessages(self):
    log = '\n'.join(self.messages)
    self.messages = []
    return log


#=================================================================================================================
# logging routines
#-----------------------------------------------------------------------------------------------------------------

loggingReportLock = threading.RLock()

#-----------------------------------------------------------------------------------------------------------------
def reportExceptionAndContinue(logger=FakeLogger(), loggingLevel=logging.ERROR, ignoreFunction=None, showTraceback=True):
  try:
    exceptionType, exception, tracebackInfo = sys.exc_info()
    if ignoreFunction and ignoreFunction(exceptionType, exception, tracebackInfo):
      return
    if exceptionType in (KeyboardInterrupt, SystemExit):
      raise  exception
    loggingReportLock.acquire()   #make sure these multiple log entries stay together
    try:
      logger.log(loggingLevel, "%s Caught Error: %s", threading.currentThread().getName(), exceptionType)
      logger.log(loggingLevel, str(exception))
      if showTraceback:
        stringStream = cStringIO.StringIO()
        try:
          print >>stringStream,  "trace back follows:"
          traceback.print_tb(tracebackInfo, None, stringStream)
          tracebackString = stringStream.getvalue()
          logger.log(loggingLevel, tracebackString)
        finally:
{'fraction_non_alphanumeric': 0.29472761793486196, 'fraction_numerical': 0.0011683949174821088, 'mean_word_length': 2.960670908039329, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '766550', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1652, 'n_words': 391}