<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: logging script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732638#M56850</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It's also possible to use the logging module (as previously mentioned) which could help to reduce boilerplate code. See this post for an example: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-without-arcgis?p=51794&amp;amp;viewfull=1#post51794"&gt;http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-without-arcgis?p=51794&amp;amp;viewfull=1#post51794&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 28 Sep 2011 17:45:02 GMT</pubDate>
    <dc:creator>LoganPugh</dc:creator>
    <dc:date>2011-09-28T17:45:02Z</dc:date>
    <item>
      <title>logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732633#M56845</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I need to see where my scripts are failing and would like to echo all commands and the results to a log file. I am a newbie to python and would apreciate any help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 12:48:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732633#M56845</guid>
      <dc:creator>PeterHoffman</dc:creator>
      <dc:date>2011-09-28T12:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732634#M56846</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Have you tried running the script in IDLE (the IDE included with your python distro) and see what kind of errors it reports?&amp;nbsp; If you have exception handling conditionals set up in your script, you could just write the exceptions to a file.&amp;nbsp; Or, you could use mile marker write statements to chart the progression through your script (i.e. have python write a note to the log file you define each time it successfully passes a certain point).&amp;nbsp; Read these for more info:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/release/2.6.6/tutorial/inputoutput.html#reading-and-writing-files"&gt;http://docs.python.org/release/2.6.6/tutorial/inputoutput.html#reading-and-writing-files&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/release/2.6.6/tutorial/errors.html"&gt;http://docs.python.org/release/2.6.6/tutorial/errors.html&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/release/2.6.6/tutorial/stdlib.html#error-output-redirection-and-program-termination"&gt;http://docs.python.org/release/2.6.6/tutorial/stdlib.html#error-output-redirection-and-program-termination&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/release/2.6.6/tutorial/stdlib2.html#logging"&gt;http://docs.python.org/release/2.6.6/tutorial/stdlib2.html#logging&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 14:54:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732634#M56846</guid>
      <dc:creator>PeterStratton</dc:creator>
      <dc:date>2011-09-28T14:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732635#M56847</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am running this from a .bat file using task manager at 4AM so that IDE method would not work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I was looking for something like the echo command used in dos .bat files.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 16:03:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732635#M56847</guid>
      <dc:creator>PeterHoffman</dc:creator>
      <dc:date>2011-09-28T16:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732636#M56848</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you can't test the python script in IDLE before running it from a batch file, or if the arguments are going to change each time it runs, i'd just write out a log file using try/except statements to record the exceptions before exiting and possibly mile markers to help chart the progress.&amp;nbsp; You can use string replacement formatting to capture the value of variables or arguments that change each time the script executes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/release/2.6.6/library/string.html#formatstrings"&gt;http://docs.python.org/release/2.6.6/library/string.html#formatstrings&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 17:01:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732636#M56848</guid>
      <dc:creator>PeterStratton</dc:creator>
      <dc:date>2011-09-28T17:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732637#M56849</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is the basic "logger" template I have been using for the last 6 years or so. Here's one for v93 and one for v10.0. By default the log file gets put in the "root" directory, so search for the "root" variable and edit it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 17:14:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732637#M56849</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2011-09-28T17:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732638#M56850</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It's also possible to use the logging module (as previously mentioned) which could help to reduce boilerplate code. See this post for an example: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-without-arcgis?p=51794&amp;amp;viewfull=1#post51794"&gt;http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-without-arcgis?p=51794&amp;amp;viewfull=1#post51794&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 17:45:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732638#M56850</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2011-09-28T17:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: logging script</title>
      <link>https://community.esri.com/t5/python-questions/logging-script/m-p/732639#M56851</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Full, 100% agreement with Logan.&lt;/STRONG&gt;&lt;SPAN&gt; Python has a built-in module for logging information, and it has been tried, tested and fine-tuned to respond to most people's needs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you are looking for a means of repeating everything to the screen and to a file, an intermediary function (like the relatively functional showGpMessage in Chris' attachment) would probably be your easiest means of doing so, along with the logger module. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If, for whatever reason, you find that this is not suitable to your needs, you can have a look at overwriting sys.stdout with an object of your choice, which has a method defined for write().&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;For example:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;class GetStream(object):
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self, pLog):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.terminal = sys.stdout
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.log = pLog

&amp;nbsp;&amp;nbsp;&amp;nbsp; def write(self, message):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.terminal.write(message)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.log.write(message)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except UnicodeEncodeError:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.log.write(message.encode('LATIN-1'))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(message)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Marc&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:16:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/logging-script/m-p/732639#M56851</guid>
      <dc:creator>MarcNakleh</dc:creator>
      <dc:date>2021-12-12T07:16:03Z</dc:date>
    </item>
  </channel>
</rss>

