Python - how to create a success/failure log for each process

4621
3
11-05-2015 03:10 AM
robinburchfield1
New Contributor

Hi everyone, a Python and ESRI newbie here.

I wish to use a Python script to register a number of Oracle Materialised View with the geodatabase - please see my sample script below.  How can I instruct the script communicate to the user whether each MView has successfully been registered or not, ideally as a log file, or at least an open text file that the user can manually save at the end if they wish?

Any guidance would be greatly appreciated

Thanks,

Robin

----------START-------------

# -*- coding: utf-8 -*-

# ---------------------------------------------------------------------------

# Reg_MView2.py

# Created on: 2015-11-02 17:30:22.00000

#   (generated by ArcGIS/ModelBuilder)

# Description:

# ---------------------------------------------------------------------------

# Set the necessary product code

import arceditor

# Import arcpy module

import arcpy

# Local variables:

Var1 = "Database Connections\\Connection to GISREF.sde\\GIS.MVIEW1"

Var2 = "Database Connections\\Connection to GISREF.sde\\GIS.MVIEW2"

Var3 = "Database Connections\\Connection to GISREF.sde\\GIS.MVIEW3"

# ....etc

# Process: Register with Geodatabase

arcpy.RegisterWithGeodatabase_management(Var1)

arcpy.RegisterWithGeodatabase_management(Var2)

arcpy.RegisterWithGeodatabase_management(Var3)

# ....etc

print "Script completed"

-----------END-------

Tags (1)
0 Kudos
3 Replies
RebeccaStrauch__GISP
MVP Emeritus

I'm not familiar with Oracle and if you are expecting to capture some output from that, but as to what step in the script it has complete, you can add a "print" or an arcpy.AddMessage after each "RegisterWithGeodatabase" message.  Simplistic, but since you said you are a newbie with python, don't want to overlook it.

...edit:

oh...and opening up the GeoProseccing -> Results tab will show the output messages from your script.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Beyond Rebecca Strauch, GISP​'s suggestion for prints and messages, the general Python methods for opening and writing files (e.g. text files) are found here.

0 Kudos
BlakeTerhune
MVP Regular Contributor

For our scheduled task python scripts, we have it send an email with all the printed debug information, including arcpy.GetMessages(). The script also creates an entry in a scheduled task log table we have in SDE (which is just a fancy way of using a file like Darren mentioned).

This is kind of how we set up our scripts (minus the email part).

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Reg_MView2.py
# Created on: 2015-11-02 17:30:22.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

def main():
    import arceditor  # Set the necessary product code
    import arcpy  # Import arcpy module
    import os

    try:
        # Local variables:
        sdeConn = "Database Connections\\Connection to GISREF.sde"
        Var1 = os.path.join(sdeConn, "GIS.MVIEW1")
        Var2 = os.path.join(sdeConn, "GIS.MVIEW2")
        Var3 = os.path.join(sdeConn, "GIS.MVIEW3")
        # ....etc

        # Process: Register with Geodatabase
        for var in [Var1, Var2, Var3]:
            print "Registering {}".format(var)
            arcpy.RegisterWithGeodatabase_management(var)
            print "Done\n"

        print "Script completed"

    except Exception as err:
        # Write error message information
        if err.message in arcpy.GetMessages(2):
            ## Write all of the messages returned by the last ArcPy tool
            print arcpy.GetMessages()
        else:
            ## Write non-ArcPy error message
            print unicode(err).encode("utf-8")

    finally:
        # Cleanup
        arcpy.ClearWorkspaceCache_management()


if __name__ == '__main__':
    main()
0 Kudos