Select to view content in your preferred language

Read the server 'message level' for geoprocessing logging

1596
1
10-11-2020 11:12 PM
by Anonymous User
Not applicable
6 1 1,596

I have had a desire to set a switch/flag in geoprocessing scripts that log at a level corresponding to the setting in the server.

The 'Message Level' setting can be accessed using the python api. 

I accomplished this via a small function as follows.

from arcgis.gis import GIS

def get_server_loglevel(checkservicename):
    infolevel = None """set infolevel to None and handle None separately 
                        or set infolevel to a default - e.g. 'info' """
 
    for folder in servicesdirectory.folders:
        folder_services = servicesdirectory.list(folder=folder)
            for folderservice in folder_services:
            
                service = folderservice.service
                servicename = service.properties.serviceName
                
                if servicename == checkservicename:
                    # match the service name from the script to the one in the server
                    if 'showMessages' in service.properties.properties:
                        infolevel = service.properties.properties.showMessages

    return infolevel
 
# obtain the gis (portal) object 
gis = GIS(r'https://fully.qualified.domain.name/portal', 'portaladmin', 'Passw0rd', verify_cert=False)

# call the federated hosting server
federatedserver = gis.admin.servers.get(role="HOSTING_SERVER")[0]

# get the servicesdirectory object.
servicesdirectory = federatedserver.content

serverloglevel = get_server_loglevel(servicename, servicesdirectory)

# use serverloglevel in the script to log accordingly.‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The use for this may be a bit unconventional but it has helped to pass control of the 'log level' to the geoprocessing script so log files can also be written based on the server log level.

import logging

general_loglevels = {'Error':'ERROR', 'Warning': 'WARNING', 'Info': 'INFO'}

# https://docs.python.org/3/library/logging.html#logging-levels


if serverloglevel in general_loglevels:
    loglevel = general_loglevels[serverloglevel]
else:
    loglevel = 'DEBUG'

logger = logging.getLogger("GP Service")
level = logging.getLevelName(loglevel)
logger.setLevel(level)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 Comment