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:
if 'showMessages' in service.properties.properties:
infolevel = service.properties.properties.showMessages
return infolevel
gis = GIS(r'https://fully.qualified.domain.name/portal', 'portaladmin', 'Passw0rd', verify_cert=False)
federatedserver = gis.admin.servers.get(role="HOSTING_SERVER")[0]
servicesdirectory = federatedserver.content
serverloglevel = get_server_loglevel(servicename, servicesdirectory)
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]
else:
loglevel = 'DEBUG'
logger = logging.getLogger("GP Service")
level = logging.getLevelName(loglevel)
logger.setLevel(level)