HOW TO

Automate deleting an unwanted log message using Python

Last Published: July 28, 2023

Summary

Sometimes, users get a repeated false positive log message or an unwanted log message from Portal for ArcGIS, ArcGIS Server, or ArcGIS Data Store and they would like to erase it from both the logs directory and the ArcGIS Server Manager or Portaladmin endpoints. This is to avoid the consumption of disk space in the logs directory as well as to be able to clearly see and focus on the important log messages.

Procedure

This can be done by running a Python script that checks all the log files in a specified directory and deletes the lines that have a specified log message.

Let's say we want to automate the deletion of the following Portal for ArcGIS log message:

The web server was found to be stopped. Re-starting it.

Run the below Python script after you consider the following:

  • Make sure that you specify the "path" variable as the logs directory. In this example, it's the Portal for ArcGIS log directory.
  • Make sure that you specify the exact log message in the "string" variable. In this example, it's "The web server was found to be stopped. Re-starting it.".
  • The Operating System account that is running the Python script should have full permissions and access to the logs directory.
  • Running this script will erase the specified log message from all the log files in the specified directory, and therefore, it will be reflected when querying the logs from the endpoints.

Please note that the below script will work on any IDLE utilizing any Python 3.x environment, including but not limited to ArcGIS Pro Python environments.

import os
res = []

# SPECIFY PATH FOR LOGS

path = r"C:\arcgisportal\logs"

##SPECIFY THE LOG YOU WOULD LIKE TO DELETE

string = "The web server was found to be stopped. Re-starting it."

##CREATE A LIST OF ALL EXISTING .log FILE PATHS (DO NOT CHANGE)

for (path, dir_names, file_names) in os.walk(path):
    for name in file_names:
        res.append(os.path.join(path, name))

##FIND THE LINE THAT HAS THE LOG MESSAGE AND DELETE IT (DO NOT CHANGE)

for file in res:
    try:
        with open(file, 'r') as f:
            replaced_content = ""
            for line in f:
                if string not in line:
                    replaced_content += line
            f.close()
            f = open(file, "wt")
            f.write(replaced_content)
            f.close()

    except Exception as ex:
        print(ex)
        pass

The image below shows a typical log file before running the script that contains two instances of the message: "The web server was found to be stopped. Re-starting it.":

image.png

After running the script, instances this message are removed from the log file, as shown in the image below.

image.png

To schedule the Python script, run it as a task in Windows Task Scheduler.  For more information, please refer to Schedule a Python script or model to run at a prescribed time: 2019 update.

Article ID: 000030692

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options