0

I have a silly code with a library called logging. I want to put a resume of debug mode in a file inside a specific path ./logs/. In the middle of the code I have INFO to save in the .log file, but not works already. I think I'm wrong in something very basic, but I do not see it.

# coding=utf-8
import re
import os
import csv
import datetime, timedelta
import logging
import logging.config


def configure_logging(logger):

    # Configure logger with custom formatter.
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    # Create file handler which logs DEBUG messages.
    now = datetime.now().strftime('%Y%m%d-%Hh%M')
    logname = './logs/' + now + '.log'
    fh = logging.FileHandler(logname)
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)

    # Create console handler with a higher log level.
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)

    # Add handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)

def between(value, a, b):
    pos_a = value.find(a)  # Find and validate before-part.
    if pos_a == -1: return ""  # Find and validate after part.
    pos_b = value.rfind(b)
    if pos_b == -1: return ""  # Return middle part.
    adjusted_pos_a = pos_a + len(a)
    if adjusted_pos_a >= pos_b: return ""
    return value[adjusted_pos_a:pos_b]

def main():
    logger = logging.getLogger('Main')
    configure_logging(logger)

    module_logger = logging.getLogger('Extract Information')

    def scan_folder():
        path = '/Users/anna/PycharmProjects/extractData/DiarioOficial'

        with open('All.csv', 'w') as csvFile:
            headers = ['COMPANY NAME', 'CVE']
            writer = csv.writer(csvFile, delimiter=';')
            writer.writerow(headers)
            for (path, dirnames, file_names) in os.walk(path):
                for file_name in file_names:
                    if file_name.endswith(".txt"):
                        file_path=os.path.join(path, file_name)
                        mensaje = open(file_path).read()

                        module_logger.info('File is opened correct')

                        # Company Name
                        keywords_cap = ['SpA', 'SPA', 'LIMITADA', 'LTDA', 'S.A.', 'E.I.R.L.', 'S.L.']
                        # re.escape to solve the problem with metacharacters in keyword_obj
                        keywords_cap = map(re.escape, keywords_cap)
                        # sorting the items by lengh in descending order
                        keywords_cap.sort(key=len, reverse=True)
                        obj = re.compile(r'[:,;.]\s*"?([^:,;.]*?(?<!\w)(?:{}))'.format('|'.join(keywords_cap)))
                        obj2 = obj.search(mensaje)
                        if obj2:
                            # To obtain the first match in group(1)
                            company_name = obj2.group(1)
                        else:
                            company_name = "None"

                        # CVE Number of the file
                        regex = r"\s*CVE\s+([^|]*)"
                        matches = re.search(regex, mensaje)
                        if matches:
                            company_cve = matches.group(1).strip()
                        else:

                            company_cve = "None"

                        csvData = [company_name, company_cve]
                        csvData = [str(data).replace('\n', '').replace('\r', '') for data in csvData]
                        writer = csv.writer(csvFile, delimiter=';')
                        writer.writerow(csvData)
    scan_folder()

if __name__ == '__main__':
    main()

As it can be seen, it is a simply code that create a cvs where data extracted from a .txt file is entered. Regex has been used to extract the data from the text file.

Anna Castan
  • 89
  • 1
  • 7
  • 1
    Can you confirm your issue is with the _logging_ module ? I think it's because you call `configure_logging` on `logger`, but then no longer use it and instead define a new logger `module_logger`. – Kévin Barré Aug 17 '18 at 08:07

1 Answers1

1

I've adapted your code to only focus on the logging part:

# coding=utf-8
import logging
from datetime import datetime  # You had a minor bug here, see https://stackoverflow.com/questions/415511/how-to-get-the-current-time-in-python


def configure_logging(logger):
    # Unchanged
    # ...

def main():
    logger = logging.getLogger('Main')
    configure_logging(logger)

    # Here, you need to use the configured logger, not another one
    logger.info("This is a test writing in log file")

if __name__ == '__main__':
    main()

Note that you need to create the logs folder manually before running the code. After running this, I have a file in the logs folder with the following content:

2018-08-17 10:13:09,304 - Main - INFO - This is a test writing in log file
Kévin Barré
  • 330
  • 1
  • 4
  • 11
  • Hi @AnnaCastan if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Kévin Barré Aug 17 '18 at 11:58
  • Ok, I didn’t know that the check is for this reason. I have already validated the answer... thank you very much. – Anna Castan Aug 18 '18 at 12:21