Is there a way to grab the all text from the tool execution dialog at the end of a python tool? I want to write the content to a log (10.1).

697
2
08-24-2016 02:17 PM
Bartvan_der_Wolf
New Contributor II

I'm used to log everything I do in my python scripts to the tool-message window with

like this..

arcpy.AddMessage("!!!!ERROR!!!!, unable to delete existing folder: " +  fpth)

At the end of my script I'd like to grab all messages and write them to a textfile at once.

something like:

allmsgs = arcpy.getMessages()  #but this only gets the last geoprocessing message
logfile = open("c:\\temp\mytoollog1.txt", 'w')
logfile.write(allmsgs)
logfile.close‍‍‍‍‍‍‍‍

Maybe it can be grabbed from the results window? 

Thanks

0 Kudos
2 Replies
RebeccaStrauch__GISP
MVP Emeritus

You can right-click on the results and do a "Save As"

Freddie Gibson‌ helped me setup a logging file years ago that I still use (modified some since then).  In this case, I'm just capturing what I "print/AddMessage"  (myMsg function)  I have various time/date functions which can be modified to fit your needs.

import time
import arcpy
import os
from time import localtime

def timeStamp():   # returns time stamp.
     return time.strftime(' -  %B %d - %H:%M:%S')

def myMsgs(message):
     arcpy.AddMessage("{0} {1}".format(message, curTime()))
     print("{0} {1} {2}".format(message, curDate(), curTime()))

     global messageCount
     logFolder = r"C:\ESRITEST"
     if not arcpy.Exists(logFolder):
          arcpy.CreateFolder_management(os.sep.join(logFolder.split(os.sep)[:-1]), logFolder.split(os.sep)[-1])
     mdy = curDate()
     logName = "logfile_" + "_".join(mdy.split("/")) + ".log"
     logFile = open(os.path.join(logFolder, logName), "a")  #a=append, w=create new
     if message.lower() == "blank line":
          logFile.write("\n\n")
          print('\n\n')
     elif message.lower() == "close logfile":
          logFile.write("\n\n*****  finished  *****\n\n")
          logFile.close()
     else:
          messageCount += 1
          logFile.write("0" * (5 - len(str(messageCount))) + str(messageCount) + ".   ")
          logFile.write(message)
          logFile.write("\n")
          #print(message)
          #arcpy.AddMessage(message)

def curDate():          # mmddyyyy
     rawTime = localtime()
     yr = str(rawTime[0]) # Collect the year from the rawTime variable
     mo = str(rawTime[1]).zfill(2) # 2-char month from local rawTime
     dy = str(rawTime[2]).zfill(2) # 2-char day from local rawTime
     return "/".join([mo, dy, yr])

def curTime():          # hhmmss
     rawTime = localtime()
     hr   = str(rawTime[3]).zfill(2) # 2-char hour from local rawTime
     mins = str(rawTime[4]).zfill(2) # 2-char minute from local rawTime
     secs = str(rawTime[5]).zfill(2) # 2-char second from local rawTime
     return (":".join([hr, mins, secs]))

def curFileDateTime():   # yyyymmdd_hhmm
     rawTime = localtime()
     yr = str(rawTime[0]) # Collect year from local rawTime 
     mo = str(rawTime[1]).zfill(2) # 2-char month from local rawTime
     dy = str(rawTime[2]).zfill(2) # 2-char day from local rawTime 
     hr = str(rawTime[3]).zfill(2) # 2-char hour from local rawTime
     mn = str(rawTime[4]).zfill(2) # 2-char minute from local rawTime
     return (yr + mo + dy + "_" + hr + mn)
     #return "".join([mo, dy, yr, hr, mn])

def archiveDate():      # yyyymmddhhmm
     rawTime = localtime()
     yr = str(rawTime[0]) # Collect year from local rawTime
     mo = str(rawTime[1]).zfill(2) # 2-char month from local rawTime
     dy = str(rawTime[2]).zfill(2) # 2-char day from local rawTime 
     hr = str(rawTime[3]).zfill(2) # 2-char hour from local rawTime 
     mn = str(rawTime[4]).zfill(2) # 2-char minute from local rawTime 
     return "".join([yr, mo, dy, hr, mn])


messageCount = 0
Bartvan_der_Wolf
New Contributor II

Hi Rebecca,

I know I can save manualy from the results window. I also could use a logger. But since we have this nice results window. I was hoping to be able to write all message contents to a file at once.

I will have a close look at your logging code. It looks like what I used in 9.3. Back then I used a custom addmessage-function and used it for every addMessage in my scripts and write to a logfile simultaniously.

Thanks!

0 Kudos