[英]Creating log-files
i'm new here and a beginner in programming. 我是这里的新手,还是编程的新手。
My problem is, my program should create a log-files during it runs. 我的问题是,我的程序应该在运行期间创建一个日志文件 。
It should look like this: 它看起来应该像这样:
Start Copy "Log X" |
开始复制“ Log X” | Date-today |
至今 Time
时间
Start Compress "Log X" |
开始压缩“ Log X” | Date-today |
至今 Time |
时间| File sice
文件服务
Ende Compress "Log X" |
Ende压缩“ Log X” | Date-today |
至今 Time |
时间| File sice
文件服务
Start Delete "Log X" |
开始删除“ Log X” | Date-today |
至今 Time
时间
End Delete "Log X" |
结束删除“ Log X” | Date-today |
至今 Time
时间
...
...
' "Log X" means the name of the File '“ Log X”表示文件名
When i run the program again the "new log-file" should attachment to the "old file" 当我再次运行程序时,“新日志文件”应附加到“旧文件”
This is my program-code till now: 到目前为止,这是我的程序代码:
import os, datetime, zipfile
def showProgramInformation():
print " "
print "#######################################################"
print "Python Log-Packer.py Ver. 1.4"
print "Search for files, separate the .log fils, compress them"
print "and delete the origin file"
print "log-File = Files with '.log' in name"
print "#######################################################"
print " "
def conversationWithUser(talk):
print talk
return raw_input()
def doesPathExists(path):
if os.path.exists(path):
return True
return False
def isFileALogFile(filePath):
if filePath.find(".log") != -1:
return True
return False
def formatSeveralDateTime(dateTime):
return datetime.datetime.fromtimestamp(dateTime).strftime('%Y-%m-%d')
def isFileInDateRange(filePath, startDate, endDate):
fileDate = formatSeveralDateTime(os.path.getmtime(filePath))
if fileDate >= startDate and fileDate <= endDate:
return True
return False
def zipLogFile(zipFilePath, zipArchivContent):
myzip = zipfile.ZipFile(zipFilePath + '.zip', 'w', zipfile.ZIP_DEFLATED)
myzip.write(zipArchivContent)
def isValidDate(dateToBeChecked):
if len(dateToBeChecked) != 10:
return False
try:
datetime.datetime.strptime(dateToBeChecked, '%Y-%m-%d')
return True
except ValueError:
return False
def repeatUserInputUntilValidInput(aString):
userInsert = False
while userInsert == False:
newString = aString.upper()
if newString == "Y":
userInsert = True
return True
elif newString == "N":
userInsert = True
return False
else:
print errorMessage
aString = conversationWithUser("Please insert 'Y' or 'N'!")
def pathNameLongerThan0(path):
if len(path) > 0:
print "Path does not exist. Please try it again!"
############## here starts main Program ##############
showProgramInformation()
checkIfInofsAreOk = "N"
errorMessage = "Your input is invalid. Please try again!"
while repeatUserInputUntilValidInput(checkIfInofsAreOk) == False:
logFolder = ""
logArchivFolder = ""
validLogFiles = []
while not doesPathExists(logFolder):
pathNameLongerThan0(logFolder)
logFolder = conversationWithUser("Please enter a valid path: ")
userWanntDateRange = conversationWithUser("Do you want to define a Date Range? (Y/N): ")
if repeatUserInputUntilValidInput(userWanntDateRange):
dateRangeIsOk = False
beginDateIsOk = False
endDateIsOK = False
while not dateRangeIsOk:
while not beginDateIsOk:
userStartDate = conversationWithUser("Please enter the beginning date (e.g. 2014-05-23): ")
beginDateIsOk = isValidDate(userStartDate)
if beginDateIsOk == False:
print errorMessage
while not endDateIsOK:
userEndDate = conversationWithUser("Please enter the ending date (e.g. 2014-11-03): ")
endDateIsOK = isValidDate(userEndDate)
if endDateIsOK == False:
print errorMessage
if userStartDate <= userEndDate:
dateRangeIsOk = True
else:
print errorMessage + " \nDate out of Range. Begin again!"
beginDateIsOk = False
endDateIsOK = False
else:
userStartDate = '1900-01-01' # set as default a wide date to make all files
userEndDate = '2090-01-01' # set as default a wide date to make all files
userWanntALogArchivFolder = conversationWithUser("Do you want create a new folder or archive the files in another folder? (Y/N): ")
if repeatUserInputUntilValidInput(userWanntALogArchivFolder):
userWanntToCreatANewFolder = conversationWithUser("Do you want to create a new folder? (Y/N): ")
if repeatUserInputUntilValidInput(userWanntToCreatANewFolder):
logArchivFolder = conversationWithUser("Enter a new fullpath folder please:")
pathIsAbsolut = os.path.isabs(logArchivFolder)
while pathIsAbsolut == False:
print errorMessage
logArchivFolder = conversationWithUser("Enter a new fullpath folder please:")
pathIsAbsolut = os.path.isabs(logArchivFolder)
else:
logArchivFolder = conversationWithUser("Enter the fullpath folder please:")
while not doesPathExists(logArchivFolder):
pathNameLongerThan0(logArchivFolder)
logArchivFolder = conversationWithUser("Please enter a valid path: ")
else:
logArchivFolder = logFolder + "/" + logArchivFolder
print "#######################################################"
print "Informations "
print "Logfolder: " + logFolder
print "Stardate: " + userStartDate
print "Enddate: " + userEndDate
print "Destination: " + logArchivFolder
print "#######################################################"
checkIfInofsAreOk = conversationWithUser("Are those informations correct? (Y/N): ")
print "#######################################################"
############ here starts compress process ############
for logFolder, subFolders, files in os.walk(logFolder):
print "#######################################################"
for file in files:
absoluteLogFilePath = logFolder + '/' + file
if isFileALogFile(file) and isFileInDateRange(filePath=absoluteLogFilePath, startDate=userStartDate, endDate=userEndDate):
validLogFiles.append(absoluteLogFilePath)
userFolderPath = logFolder
if len(validLogFiles) > 0:
if len(logArchivFolder) > 0:
if not doesPathExists(logArchivFolder):
os.mkdir(logArchivFolder)
userFolderPath = logArchivFolder
for logFile in validLogFiles:
zipFilePath = userFolderPath + '/' + os.path.basename(logFile)
zipLogFile(zipFilePath, logFile)
print logFile
os.remove(logFile)
print "#######################################################"
print "finish"
print "#######################################################"
quit()
It would be nice if they could help me. 如果他们能帮助我,那就太好了。 (Sorry if my english is no so good)
(对不起,如果我的英语不太好)
Yours truly 敬上
Johannes 约翰尼斯
The logging module defines functions and classes which implement a flexible event logging system for applications and libraries. 日志记录模块定义了实现用于应用程序和库的灵活事件日志记录系统的函数和类。
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)
would print something like 将打印类似
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
You can use a Formatter to define your own output format and use the different LogRecord attributes to merge data from the record into the format string. 您可以使用Formatter定义自己的输出格式,并使用不同的LogRecord属性将记录中的数据合并到格式字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.