Skip to content

Commit

Permalink
Log to stdout. Minimal logging for create case and workflow job, ordi…
Browse files Browse the repository at this point in the history
…nary logging in forward job. (#54)

* Log to stdout. Minimal logging for create case and workflow job

* Remove unused var
  • Loading branch information
roywilly authored Apr 15, 2024
1 parent 6302ddf commit 21e2aec
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/fmu/sumo/uploader/_fileondisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import yaml

from fmu.sumo.uploader._sumofile import SumoFile
from fmu.sumo.uploader._logger import get_uploader_logger


# pylint: disable=C0103 # allow non-snake case variable names

logger = logging.getLogger(__name__)
logger.setLevel(logging.CRITICAL)
logger = get_uploader_logger()


def path_to_yaml_path(path):
Expand Down Expand Up @@ -54,15 +55,14 @@ def file_to_byte_string(path):


class FileOnDisk(SumoFile):
def __init__(self, path: str, metadata_path=None, verbosity="INFO"):
def __init__(self, path: str, metadata_path=None, verbosity="WARNING"):
"""
path (str): Path to file
metadata_path (str): Path to metadata file. If not provided,
path will be derived from file path.
"""

self.verbosity = verbosity
logger.setLevel(level=self.verbosity)
logger.setLevel(level=verbosity)

self.metadata_path = (
metadata_path if metadata_path else path_to_yaml_path(path)
Expand Down
5 changes: 3 additions & 2 deletions src/fmu/sumo/uploader/_fileonjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import base64

from fmu.sumo.uploader._sumofile import SumoFile
from fmu.sumo.uploader._logger import get_uploader_logger


# pylint: disable=C0103 # allow non-snake case variable names

logger = logging.getLogger(__name__)
logger.setLevel(logging.CRITICAL)
logger = get_uploader_logger()

class FileOnJob(SumoFile):
def __init__(self, byte_string: str, metadata):
Expand Down
36 changes: 36 additions & 0 deletions src/fmu/sumo/uploader/_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging
import sys
import os
import time

# def getLogger(module_name="subscript"):
def get_uploader_logger():
# pylint: disable=invalid-name
"""Provides a unified logger for fmu-sumo-uploader.
Code is copied from
https://github.com/equinor/subscript/blob/main/src/subscript/__init__.py
Logging output is split by logging levels (split between WARNING and ERROR)
to stdout and stderr, each log occurs in only one of the streams.
Returns:
A logger object
"""
logger = logging.getLogger("fmu.sumo.uploader")
logger.propagate = False # Avoids duplicate logging

if not len(logger.handlers):
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")

stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.addFilter(lambda record: record.levelno < logging.ERROR)
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)

stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.addFilter(lambda record: record.levelno >= logging.ERROR)
stderr_handler.setFormatter(formatter)
logger.addHandler(stderr_handler)

return logger
7 changes: 4 additions & 3 deletions src/fmu/sumo/uploader/_sumocase.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@


from fmu.sumo.uploader._upload_files import upload_files
from fmu.sumo.uploader._logger import get_uploader_logger



# pylint: disable=C0103 # allow non-snake case variable names

logger = logging.getLogger(__name__)
logger.setLevel(logging.CRITICAL)
logger = get_uploader_logger()


class SumoCase:
def __init__(self, case_metadata: str, sumo_connection, verbosity):
def __init__(self, case_metadata: str, sumo_connection, verbosity="WARNING"):
logger.setLevel(verbosity)
self.sumo_connection = sumo_connection
self.case_metadata = _sanitize_datetimes(case_metadata)
Expand Down
5 changes: 3 additions & 2 deletions src/fmu/sumo/uploader/_sumofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import logging
import warnings
import httpx
from fmu.sumo.uploader._logger import get_uploader_logger


# pylint: disable=C0103 # allow non-snake case variable names

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger = get_uploader_logger()


def _get_segyimport_cmdstr(blob_url, object_id, file_path, sample_unit):
Expand Down
7 changes: 4 additions & 3 deletions src/fmu/sumo/uploader/caseondisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
from fmu.sumo.uploader._fileondisk import FileOnDisk
from fmu.dataio import ExportData
from fmu.dataio._utils import read_parameters_txt
from fmu.sumo.uploader._logger import get_uploader_logger


logger = logging.getLogger(__name__)
logger.setLevel(logging.CRITICAL)

logger = get_uploader_logger()

# pylint: disable=C0103 # allow non-snake case variable names

Expand Down Expand Up @@ -64,7 +65,7 @@ class CaseOnDisk(SumoCase):
"""

def __init__(
self, case_metadata_path: str, sumo_connection, verbosity=logging.INFO
self, case_metadata_path: str, sumo_connection, verbosity=logging.WARNING
):
"""Initialize CaseOnDisk.
Expand Down
5 changes: 3 additions & 2 deletions src/fmu/sumo/uploader/caseonjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from fmu.sumo.uploader._sumocase import SumoCase
from fmu.sumo.uploader._fileonjob import FileOnJob
from fmu.sumo.uploader._logger import get_uploader_logger

logger = logging.getLogger(__name__)
logger.setLevel(logging.CRITICAL)

logger = get_uploader_logger()

# pylint: disable=C0103 # allow non-snake case variable names

Expand Down
15 changes: 13 additions & 2 deletions src/fmu/sumo/uploader/scripts/sumo_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from res.job_queue import ErtScript # type: ignore

from fmu.sumo import uploader
from fmu.sumo.uploader._logger import get_uploader_logger

logger = get_uploader_logger()

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

# This documentation is for sumo_uploader as an ERT workflow
DESCRIPTION = """SUMO_UPLOAD will upload files to Sumo. The typical use case
Expand Down Expand Up @@ -74,6 +75,8 @@ def main() -> None:
parser = _get_parser()
args = parser.parse_args()

logger.setLevel(logging.INFO)

if args.verbose:
logger.setLevel(logging.INFO)
if args.debug:
Expand All @@ -92,6 +95,7 @@ def main() -> None:
metadata_path=args.metadata_path,
threads=args.threads,
config_path=args.config_path,
verbosity=logging.INFO
)


Expand All @@ -102,9 +106,11 @@ def sumo_upload_main(
metadata_path: str,
threads: int,
config_path: str = "fmuconfig/output/global_variables.yml",
verbosity: int = logging.INFO
) -> None:
"""A "main" function that can be used both from command line and from ERT workflow"""

logger.setLevel(verbosity)
logger.debug("Running fmu_uploader_main()")

# Catch-all to ensure FMU workflow keeps running even if something happens.
Expand All @@ -123,6 +129,7 @@ def sumo_upload_main(
e = uploader.CaseOnDisk(
case_metadata_path=case_metadata_path,
sumo_connection=sumo_connection,
verbosity=verbosity
)
# add files to the case on disk object
logger.info("Adding files. Search path is %s", searchpath)
Expand Down Expand Up @@ -163,6 +170,9 @@ def run(self, *args):
# pylint: disable=no-self-use
"""Parse with a simplified command line parser, for ERT only,
call sumo_upload_main()"""

logger.setLevel(logging.WARNING)

logger.debug("Calling run() on SumoUpload")
parser = _get_parser()
args = parser.parse_args(args)
Expand All @@ -174,6 +184,7 @@ def run(self, *args):
metadata_path=args.metadata_path,
threads=args.threads,
config_path=args.config_path,
verbosity=logging.WARNING
)


Expand Down

0 comments on commit 21e2aec

Please sign in to comment.