#12452 Python: Replace print() with logging API in rips module

This commit is contained in:
Kristian Bendiksen
2026-03-05 15:58:25 +01:00
parent c354819e5a
commit e4ea6ea44f
2 changed files with 26 additions and 22 deletions
+4
View File
@@ -1,6 +1,10 @@
import logging
import os
import sys
# Configure null handler to prevent "No handler found" warnings
logging.getLogger("rips").addHandler(logging.NullHandler())
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "generated"))
from .resinsight_classes import * # noqa: F403
+22 -22
View File
@@ -34,6 +34,8 @@ from .generated.generated_classes import CommandRouter
from typing import List, Optional, Tuple
from pathlib import Path
logger = logging.getLogger(__name__)
class Instance:
"""The ResInsight Instance class. Use to launch or find existing ResInsight instances
@@ -78,10 +80,10 @@ class Instance:
return int(value)
if retry_count == max_attempts:
print(
"Waiting for port number file timed out after {} seconds. File: {}".format(
max_attempts, file_path
)
logger.warning(
"Waiting for port number file timed out after %d seconds. File: %s",
max_attempts,
file_path,
)
return -1
@@ -138,16 +140,16 @@ class Instance:
data = json.load(f)
resinsight_executable = data["resinsight_executable"]
if resinsight_executable:
print(
"In './share/rips/rips_config.json', found resinsight_executable:",
logger.info(
"In './share/rips/rips_config.json', found resinsight_executable: %s",
resinsight_executable,
)
if not resinsight_executable:
resinsight_executable_from_env = os.environ.get("RESINSIGHT_EXECUTABLE")
if not resinsight_executable_from_env:
print(
"ERROR: Could not launch ResInsight because the environment variable"
logger.error(
"Could not launch ResInsight because the environment variable"
" RESINSIGHT_EXECUTABLE is not set"
)
return None
@@ -156,12 +158,12 @@ class Instance:
# Check if executable file exists
if not os.path.isfile(resinsight_executable):
print(
f"ERROR: ResInsight executable not found at path: {resinsight_executable}"
logger.error(
"ResInsight executable not found at path: %s", resinsight_executable
)
return None
print("Trying to launch", resinsight_executable)
logger.info("Trying to launch %s", resinsight_executable)
with tempfile.TemporaryDirectory() as tmp_dir_path:
port_number_file = tmp_dir_path + "/portnumber.txt"
parameters: List[str] = [
@@ -172,7 +174,7 @@ class Instance:
str(port_number_file),
] + command_line_parameters
if console:
print("Launching as console app")
logger.info("Launching as console app")
parameters.append("--console")
# Stringify all parameters
@@ -186,7 +188,7 @@ class Instance:
port_number_file, init_timeout
)
if port == -1:
print("Unable to read port number. Launch failed.")
logger.error("Unable to read port number. Launch failed.")
# Need to kill the process using PID since there is no GRPC connection to use.
Instance.__kill_process(pid)
else:
@@ -208,22 +210,21 @@ class Instance:
"""
port_env = os.environ.get("RESINSIGHT_GRPC_PORT")
if port_env:
print("Got port " + port_env + " from environment")
logger.info("Got port %s from environment", port_env)
start_port = int(port_env)
end_port = start_port + 20
for try_port in range(start_port, end_port):
print("Trying port " + str(try_port))
logger.debug("Trying port %d", try_port)
if Instance.__is_port_in_use(try_port) and Instance.__is_valid_port(
try_port
):
return Instance(port=try_port)
print(
"Error: Could not find any ResInsight instances responding between ports "
+ str(start_port)
+ " and "
+ str(end_port)
logger.error(
"Could not find any ResInsight instances responding between ports %d and %d",
start_port,
end_port,
)
return None
@@ -248,7 +249,6 @@ class Instance:
Args:
port(int): port number
"""
logging.basicConfig()
self.location: str = "localhost:" + str(port)
self.channel = grpc.insecure_channel(
@@ -432,7 +432,7 @@ class Instance:
def exit(self):
"""Tell ResInsight instance to quit"""
print("Telling ResInsight to Exit")
logger.info("Telling ResInsight to Exit")
return self.app.Exit(Empty())
def is_console(self) -> bool: