Nerotix
Nerotix

Reputation: 375

Always print multiple error lines

I'm busy working on a tool for tagging documents. I can't seem to solve an issue im having. See the code below, these are 2 functions that are a small part of my code:

def runSearch():
time.sleep(1)
#checkt of de lengte van de list keywords groter is dan 0, zo niet, moet je eerst keywords opgeven
if len(keywords) > 0:
    path_input = raw_input('Give path to check for documents(e.g. /Users/Frank/Desktop): ')
    errCounter = 0

    #hier word gekeken of de opgegeven directory bestaat
    if os.path.isdir(path_input):
        for root, dirs, files in os.walk(path_input):
            for file in files:
                fullFile = os.path.join(root, file)
                if os.path.splitext(fullFile)[1].lower() == ('.docx') or \
                                os.path.splitext(file)[1].lower() == ('.doc') or \
                                os.path.splitext(file)[1].lower() ==  ('.pptx') or \
                                os.path.splitext(file)[1].lower() == ('.txt') or \
                                os.path.splitext(file)[1].lower() == ('.xlsx') or \
                                os.path.splitext(file)[1].lower() == ('.xls') or \
                                os.path.splitext(file)[1].lower() == ('.odt') or \
                                os.path.splitext(file)[1].lower() == ('.rtf') or \
                                os.path.splitext(file)[1].lower() == ('.csv') or \
                                os.path.splitext(file)[1].lower() == ('.html') or \
                                os.path.splitext(file)[1].lower() == ('.htm') or \
                                os.path.splitext(file)[1].lower() == ('.pdf'):
                    tagDocs(fullFile)

        print ('\nSearch for keyword(s) completed.')
        time.sleep(1)
    else:
        print("\nPlease enter a valid path, for example: '/Users/Frank/Documents'")
else:
    print('\nNo keywords were defined, use menu option 2 first.')
raw_input("\nPress Enter to continue...")
menu()

def tagDocs(fullFile):
    try:
        content = textract.process(os.path.abspath(fullFile))
        # contentList.append(content)
        words = content.split()
        words = [words.lower() for words in words]

        tagCounter = 0

        for item in enumerate(words):
            for index, value in enumerate(keywords):
                if value in item:
                    tagDocuments.append(fullFile)
                    tagCounter += 1

        if tagCounter != 0:
            print ("\nIn the file '" + os.path.abspath(fullFile) + "' the tag(s)" +  " was found: " +
                   str(tagCounter) + " time(s).")

    except Exception, e:
        errorLog(e)
        pass

Every time there's an error passed to errorLog() it print the error 4 times in the error.log. I know what's causing this, it's because it loops 4 times, because there are 4 files, if I add a file into the folder im scanning, it will print the error 5 times. So my question, how can I make it only print the error 1 time? Now it prints the error 1 time for every file it finds.

update #2

def errorLogOnce(err):
    if not err in reported_errors:
        reported_errors.append(err)
        errorLog(err)

def errorLog(e):
    errCounter = 0
    errCounter += 1

    logging.basicConfig(filename='error.log',level=logging.DEBUG)
    logging.debug(e)

Upvotes: 1

Views: 332

Answers (2)

Adi Levin
Adi Levin

Reputation: 5243

If you want less lines of code, without classes, you can also do this:

reported_errors = []

def errorLogOnce(err):
  if not err in reported_errors:
    reported_errors.append(err)
    errorLog(err)        

And then call errorLogOnce instead of calling errorLog

Upvotes: 1

Adi Levin
Adi Levin

Reputation: 5243

So you want a logger that never repeats a log of the same error? If so, define a class

class MyLogger(object):
   def __init__(self):
      self.reported_errors = []
   def log(self,err):
      if not err in self.reported_errors:
         self.repoted_errors.add(err)
         errorLog(err)

And call this logger instead of directly calling errorLog(e) directly

def runSearch():
   logger = MyLogger()
      ....
      tagDocs(fullFile,logger)
   ....

def tagDocs(fullFile,logger):
  try:
     ...
  except Exception, e:
    logger.log(e)
    pass

Upvotes: 0

Related Questions