Write all Add Messages to text file

2754
5
Jump to solution
01-02-2021 06:49 AM
NealBanerjee
Occasional Contributor

Hello,

I have a script tool that I use 'Add Message' to indicate progress during tool execution.  I would like to also write everything that I am writing to a text/log file.  I know I can do this manually by use a separate standard Python logfile.write process, but wanted to see if anyway to either capture messages at one time

 

Thanks!

Neal

1 Solution

Accepted Solutions
NealBanerjee
Occasional Contributor

Thank you Dan and David for your help. What I ended up doing is I think along the lines of what you both mentioned, but maybe a little different.  I first created a very simple mini-function in my main script that takes a string parameter.  The function then uses 'AddMessage' to return the message to user running script tool and the logfile.write to write same message to a separate text log file.  Below is function I created and a snippet of code showing how I used it.  Maybe not the most elaborate, but better than having to write each message basically twice.

def WriteMsg(strMsg):
    arcpy.AddMessage(strMsg)
    logfile.write(strMsg + "\n")
    return
### Simple snippet below

lstdomains = arcpy.da.ListDomains(inFGDB)          #Get list of domains in inFGDB
addmsg="Processing domains in input workspace: {0} and writing as tables in: {1}".format(inFGDB, outFGDB)
WriteMsg(addmsg)

 

View solution in original post

5 Replies
DavidPike
MVP Frequent Contributor

GetMessages—ArcGIS Pro | Documentation

If it was wrapped up into a single tool execution I guess, otherwise it's a case of logging each add Message to a list.

NealBanerjee
Occasional Contributor

Thanks David, in my case it is a number of messages that want to capture. 

0 Kudos
DavidPike
MVP Frequent Contributor

Can you wrap the script into a function, then grab all the messages in a single call after it's been executed ?(unsure myself - the method could be better elaborated on in the help).

DanPatterson
MVP Esteemed Contributor
msg = "start\n"
stuff
msg += "did something\n"
stuff
msg += "did more something\n"
stuff
msg += "done\n"
with open(r"c:\somefolder\Output.txt", "w") as text_file:
    text_file.write(msg)

usually works


... sort of retired...
NealBanerjee
Occasional Contributor

Thank you Dan and David for your help. What I ended up doing is I think along the lines of what you both mentioned, but maybe a little different.  I first created a very simple mini-function in my main script that takes a string parameter.  The function then uses 'AddMessage' to return the message to user running script tool and the logfile.write to write same message to a separate text log file.  Below is function I created and a snippet of code showing how I used it.  Maybe not the most elaborate, but better than having to write each message basically twice.

def WriteMsg(strMsg):
    arcpy.AddMessage(strMsg)
    logfile.write(strMsg + "\n")
    return
### Simple snippet below

lstdomains = arcpy.da.ListDomains(inFGDB)          #Get list of domains in inFGDB
addmsg="Processing domains in input workspace: {0} and writing as tables in: {1}".format(inFGDB, outFGDB)
WriteMsg(addmsg)

 

View solution in original post