решено:
Эта проблема была решена путем переименования двух моих регистраторов.Моя проблема возникла, потому что я бы сделал log.getLogger
снова в основном файле.Это привело к созданию 2 экземпляров регистратора.Решение состоит в том, чтобы удалить второй вызов, ИЛИ переименовать один из двух.
Я пытаюсь настроить собственный регистратор для моего текущего проекта, и у меня возникают трудности с его правильной работой за пределами * 1006.* файл.Проблема в том, что все, что я регистрирую, регистрируется дважды.
Мой код:
__ Init __. Py :
import datetime as date
import os
import platform as plt
import logging as log
prefsDirectory = 'prefs/'
prefsName = 'preferences.txt'
prefsLocation = prefsDirectory + prefsName
now = date.datetime.now()
# SETUP
if not(os.path.exists(prefsLocation)):
if not(plt.system() == "Darwin"):
os.mknod(prefsLocation)
with(open(prefsLocation, 'w+')) as f:
f.write('Log increment:\n' + str(1) + "\n")
f.close()
pass
else:
if not(os.path.exists(prefsDirectory)):
os.mkdir(prefsDirectory)
with(open(prefsLocation, 'w+')) as f:
f.close()
pass
with(open(prefsLocation, 'w+')) as f:
f.write('Log increment:\n' + str(0) + "\n")
f.write('\nCurrent Date:\n' + str(now.day) + "\n")
f.close()
pass
with(open(prefsLocation, "r")) as f:
data = f.readlines()
if not(str(now.day) == data[4]):
data[4] = str(now.day)
data[1] = str(0) + '\n'
# print('This ran')
else:
inc = str(int(data[1]) + 1)
data[1] = inc + "\n"
with(open(prefsLocation, "w")) as f:
lines = (str(item) for item in data)
for item in lines:
f.write(item)
dateC = "[" + str(now.year) + "-" + str(now.month) + "-" + data[4] + "]"
logDirectory = "logs/"
inc = int(data[1])
logName2 = str(dateC) + "-" + str(inc)
logName = logName2 + ".log"
logLocation = logDirectory + logName
if not(os.path.exists(logLocation)):
if not(plt.system() == "Darwin"):
os.mknod(logLocation)
else:
if not(os.path.isdir(logDirectory)):
os.mkdir(logDirectory)
with (open(logLocation, 'w+')) as f:
f.close()
pass
formatter = log.Formatter("[%(asctime)s][%(levelname)s][%(module)s] : %(message)s \n", "%H:%M-%S" + "s")
handler = log.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel("DEBUG")
logger = log.getLogger("Main")
logger.addHandler(handler)
log.basicConfig(filename=logLocation, level=log.DEBUG, filemode="w",
format="[%(asctime)s][%(levelname)s][%(module)s] : %(message)s \n", datefmt="%H:%M-%S" + "s")
logger.info("[LOG NUMBER: " + str(inc) + "]")
logger.info("Found Settings file")
logger.info("Generated Log File")
__ main __. Py :
# IMPORTS
import logging as log
from main import variables as vrs
# VARIABLES
logg = vrs.logg
logg.addHandler(vrs.handlerMain)
log.basicConfig(filename=vrs.logLocation, level=log.DEBUG, filemode="w",
format="[%(asctime)s][%(levelname)s][%(module)s] : %(message)s \n", datefmt="%H:%M-%S" + "s")
with(open(vrs.prefsLocation, "r")) as f:
data = f.readlines()
# BODY
logg.info('Program Loading Completed.')
# Make a data holding file.
vrs.makefile('prefs/data.txt', 'prefs/', "Data File")
variables.py :
import datetime as date
import logging as log
import os
import platform as plt
prefsDirectory = 'prefs/'
prefsName = 'preferences.txt'
prefsLocation = prefsDirectory + prefsName
with(open(prefsLocation, "r")) as f:
data = f.readlines()
now = date.datetime.now()
dateC = "[" + str(now.year) + "-" + str(now.month) + "-" + data[4] + "]"
logDirectory = "logs/"
inc = int(data[1])
logName2 = str(dateC) + "-" + str(inc)
logName = logName2 + ".log"
logLocation = logDirectory + logName
formatter = log.Formatter("[%(asctime)s][%(levelname)s][%(module)s] : %(message)s \n", "%H:%M-%S" + "s")
handler = log.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel("DEBUG")
handler.set_name('Main')
handlerMain = log.StreamHandler()
handlerMain.setFormatter(formatter)
handlerMain.setLevel("DEBUG")
logg = log.getLogger("Main")
def makefile(filelocation, filedirectory, filename):
if not (os.path.exists(filelocation)):
if not (plt.system() == "Darwin"):
os.mknod(filelocation)
with(open(filelocation, 'w+')) as file:
file.write('File Created:\n' + dateC + "\n")
file.close()
pass
else:
if not (os.path.exists(filedirectory)):
os.mkdir(filedirectory)
with(open(filelocation, 'w+')) as file:
file.write('File Created:\n' + dateC + "\n")
file.close()
pass
logg.info('Created file: ' + filename)
Я не уверен, что именно вызывает проблему...Я думаю, что это что-то с определением регистратора в файле init и секундой в файле переменных.
Если это поможет, я предоставлю копию структуры моего файла ниже:
<a href="https://gyazo.com/5cb1221a65a9ad50adf2a355f92f90e4"><img src="https://i.gyazo.com/5cb1221a65a9ad50adf2a355f92f90e4.png" alt="Image from Gyazo" width="315"/></a>
<a href="https://gyazo.com/39f1b61ca09ed364080254a0f678db80"><img src="https://i.gyazo.com/39f1b61ca09ed364080254a0f678db80.png" alt="Image from Gyazo" width="1280"/></a>
[Я, кажется, не могу вводить изображения gyazo в пост, может ли один из вас, модератор сообщества, вставить их для меня?ТАКЖЕ, папка для просмотра под названием AoC2018]