Read the server 'message level' for geoprocessing logging

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

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 =
                if servicename == checkservicename:
                    # match the service name from the script to the one in the server
                    if 'showMessages' in
                        infolevel =

    return infolevel
# obtain the gis (portal) object 
gis = GIS(r'', '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'}


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

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