我有一个愚蠢的代码,带有一个称为日志记录的库。我想将调试模式的简历放在特定路径./logs/中的文件中。 在代码的中间,我有要保存在.log文件中的INFO,但尚不能使用。 我认为我在一个非常基本的问题上错了,但我没有看到。
# 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()
可以看出,这是一个简单的代码,用于创建cvs,并在其中输入从.txt文件提取的数据。正则表达式已用于从文本文件中提取数据。
答案 0 :(得分:1)
我调整了您的代码,使其仅专注于 logging 部分:
# 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 文件夹。 运行此命令后,我在logs文件夹中有一个文件,内容如下:
2018-08-17 10:13:09,304 - Main - INFO - This is a test writing in log file