导入日志记录:使用 Python 文件恢复代码



我有一个愚蠢的代码,里面有一个名为logging的库。我想将调试模式的恢复放在特定路径 ./logs/内的文件中。 在代码的中间,我有 INFO 保存在.log文件中,但已经不起作用。 我认为我在一些非常基本的事情上错了,但我没有看到它。

# 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*CVEs+([^|]*)"
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()

可以看出,它是一个简单的代码,用于创建 cvs,其中输入从.txt文件中提取的数据。正则表达式已用于从文本文件中提取数据。

我已经调整了您的代码,使其只关注日志记录部分:

# 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()

请注意,在运行代码之前,您需要手动创建日志文件夹。 运行此程序后,我在 logs 文件夹中有一个包含以下内容的文件:

2018-08-17 10:13:09,304 - Main - INFO - This is a test writing in log file

最新更新