Skip to content

Commit

Permalink
Suppress everest logger output to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
verveerpj committed Nov 27, 2024
1 parent 7e0f793 commit 8b107b4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 76 deletions.
13 changes: 7 additions & 6 deletions src/everest/bin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from everest.bin.kill_script import kill_entry
from everest.bin.monitor_script import monitor_entry
from everest.bin.visualization_script import visualization_entry
from everest.util import configure_logger
from everest.util import get_azure_logging_handler


def _create_dump_action(dumps, extended=False):
Expand Down Expand Up @@ -86,11 +86,12 @@ def __init__(self, args):
if not hasattr(self, parsed_args.command):
parser.error("Unrecognized command")

# Somewhere some logging to the root is done, this leads to logging going to
# the console. Install a null handler to prevent this:
null_handler = logging.NullHandler()
logging.getLogger().addHandler(null_handler)
logger = configure_logger("everest_main", log_to_azure=True)
# Setup logging to azure:
logger = logging.getLogger("everest_main")
azure_handler = get_azure_logging_handler()
if azure_handler:
logger.addHandler(azure_handler)

logger.info(f"Started everest with {parsed_args}")
# Use dispatch pattern to invoke method with same name
getattr(self, parsed_args.command)(args[2:])
Expand Down
87 changes: 48 additions & 39 deletions src/everest/detached/jobs/everserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from base64 import b64encode
from datetime import datetime, timedelta
from functools import partial
from pathlib import Path
from typing import Any

import uvicorn
from cryptography import x509
Expand Down Expand Up @@ -39,13 +41,14 @@
from everest.export import check_for_errors
from everest.simulator import JOB_FAILURE
from everest.strings import (
DEFAULT_LOGGING_FORMAT,
EVEREST,
OPT_FAILURE_REALIZATIONS,
OPT_PROGRESS_ENDPOINT,
SIM_PROGRESS_ENDPOINT,
STOP_ENDPOINT,
)
from everest.util import configure_logger, makedirs_if_needed, version_info
from everest.util import get_azure_logging_handler, makedirs_if_needed, version_info


def _get_machine_name() -> str:
Expand Down Expand Up @@ -159,6 +162,7 @@ def get_opt_progress(
ssl_certfile=server_config["cert_path"],
ssl_version=ssl.PROTOCOL_SSLv23,
ssl_keyfile_password=server_config["key_passwd"],
log_level=logging.CRITICAL,
)


Expand Down Expand Up @@ -193,41 +197,51 @@ def _write_hostfile(host_file_path, host, port, cert, auth) -> None:
f.write(json_string)


def _configure_loggers(
detached_node_dir: str,
everest_logs_dir: str,
logging_level: int,
) -> None:
configure_logger(
name="res",
file_path=os.path.join(detached_node_dir, "simulations.log"),
log_level=logging.INFO,
)

configure_logger(
name="everserver",
file_path=os.path.join(detached_node_dir, "endpoint.log"),
log_level=logging.INFO,
)
def _configure_loggers(config: EverestConfig) -> None:
def _make_handler(path: Path, log_level: str | int = "INFO") -> dict[str, Any]:
makedirs_if_needed(path.parent)
return {
"class": "logging.FileHandler",
"formatter": "default",
"level": log_level,
"filename": path,
}

configure_logger(
name=EVEREST,
file_path=os.path.join(everest_logs_dir, "everest.log"),
log_level=logging_level,
log_to_azure=True,
)
detached_dir = Path(ServerConfig.get_detached_node_dir(config.output_dir))
log_dir = Path(config.log_dir)
logging_dict = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"root": {"level": "NOTSET", "class": "logging.NullHandler"},
"res": _make_handler(detached_dir / "simulations.log"),
"everserver": _make_handler(detached_dir / "endpoint.log"),
"everest": _make_handler(log_dir / "everest.log", config.logging_level),
"forward_models": _make_handler(
log_dir / "forward_models.log", config.logging_level
),
},
"loggers": {
"": {"handlers": ["root"], "level": "NOTSET"},
"res": {"handlers": ["res"]},
"everserver": {"handlers": ["everserver"]},
"everest": {"handlers": ["everest"]},
"forward_models": {"handlers": ["forward_models"]},
},
"formatters": {
"default": {"format": DEFAULT_LOGGING_FORMAT},
},
}

configure_logger(
name="forward_models",
file_path=os.path.join(everest_logs_dir, "forward_models.log"),
log_level=logging_level,
)
azure_handler = get_azure_logging_handler()
if azure_handler:
logging_dict["handlers"]["azure"] = {
"class": "azure_handler",
"level": config.logging_level,
}
logging_dict["loggers"]["everest"]["handlers"].append("azure")

configure_logger(
name="ropt",
file_path=os.path.join(everest_logs_dir, "ropt.log"),
log_level=logging_level,
)
logging.config.dictConfig(logging_dict)


def main():
Expand All @@ -238,16 +252,11 @@ def main():
config = EverestConfig.load_file(options.config_file)
if options.debug:
config.logging_level = "debug"
detached_dir = ServerConfig.get_detached_node_dir(config.output_dir)
status_path = ServerConfig.get_everserver_status_path(config.output_dir)
host_file = ServerConfig.get_hostfile_path(config.output_dir)

try:
_configure_loggers(
detached_node_dir=detached_dir,
everest_logs_dir=config.log_dir,
logging_level=config.logging_level,
)
_configure_loggers(config)
update_everserver_status(status_path, ServerStatus.starting)
logging.getLogger(EVEREST).info(version_info())
logging.getLogger(EVEREST).info(
Expand Down
27 changes: 1 addition & 26 deletions src/everest/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from ert.shared.version import version as ert_version
from everest.plugins.everest_plugin_manager import EverestPluginManager
from everest.strings import DATE_FORMAT, DEFAULT_LOGGING_FORMAT, EVEREST
from everest.util.async_run import async_run # noqa
from everest.strings import DATE_FORMAT, EVEREST

try:
import opm.io
Expand All @@ -30,30 +29,6 @@ def get_azure_logging_handler():
return handles[0]


def configure_logger(
name=None,
file_path=None,
log_level=None,
formatter=DEFAULT_LOGGING_FORMAT,
log_to_azure=False,
) -> logging.Logger:
logger = logging.getLogger(name)

logger.setLevel(log_level or logging.INFO)
if file_path is not None:
makedirs_if_needed(os.path.dirname(file_path))
handler = logging.FileHandler(file_path)
handler.setFormatter(logging.Formatter(formatter))
logger.addHandler(handler)

# Setup azure logging if needed
azure_handler = get_azure_logging_handler()
if log_to_azure and azure_handler:
logger.addHandler(azure_handler)

return logger


def version_info():
return ("everest:'{}'\nropt:'{}'\nert:'{}'").format(
ert_version, ropt_version, ert_version
Expand Down
10 changes: 5 additions & 5 deletions tests/everest/test_everserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_hostfile_storage(tmp_path, monkeypatch):

@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch(
"everest.detached.jobs.everserver.configure_logger",
"everest.detached.jobs.everserver._configure_loggers",
side_effect=configure_everserver_logger,
)
def test_everserver_status_failure(_1, copy_math_func_test_data_to_tmp):
Expand All @@ -102,7 +102,7 @@ def test_everserver_status_failure(_1, copy_math_func_test_data_to_tmp):


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver.configure_logger")
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_everserver_status_running_complete(


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver.configure_logger")
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_everserver_status_failed_job(


@patch("sys.argv", ["name", "--config-file", "config_minimal.yml"])
@patch("everest.detached.jobs.everserver.configure_logger")
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
Expand Down Expand Up @@ -237,7 +237,7 @@ def test_everserver_status_exception(


@patch("sys.argv", ["name", "--config-file", "config_one_batch.yml"])
@patch("everest.detached.jobs.everserver.configure_logger")
@patch("everest.detached.jobs.everserver._configure_loggers")
@patch("everest.detached.jobs.everserver._generate_authentication")
@patch(
"everest.detached.jobs.everserver._generate_certificate",
Expand Down

0 comments on commit 8b107b4

Please sign in to comment.