Skip to content

Commit

Permalink
ENH: replace verbosity with standard logging patterns
Browse files Browse the repository at this point in the history
Using verbosity and setLevel within the library is an antipattern.

The approach shall be that the end client script
sets the logging level, format, etc. instead.
  • Loading branch information
jcrivenaes committed Jan 19, 2024
1 parent c1f8ae6 commit 382393b
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 109 deletions.
5 changes: 3 additions & 2 deletions src/fmu/dataio/_design_kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

from __future__ import annotations

import logging
import re
import shlex
from typing import Any, Final, Iterable

from ._logging import null_logger

_STATUS_FILE_NAME: Final = "DESIGN_KW.OK"

_logger: Final = logging.getLogger(__name__)
_logger: Final = null_logger(__name__)


def run(
Expand Down
8 changes: 3 additions & 5 deletions src/fmu/dataio/_filedata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"""
from __future__ import annotations

import logging
from copy import deepcopy
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Final, Optional
from warnings import warn

logger: Final = logging.getLogger(__name__)
from ._logging import null_logger

logger: Final = null_logger(__name__)


@dataclass
Expand All @@ -33,7 +34,6 @@ class _FileDataProvider:
rootpath: Path = field(default_factory=Path)
itername: str = ""
realname: str = ""
verbosity: str = "CRITICAL"

# storing results in these variables
relative_path: Optional[str] = field(default="", init=False)
Expand All @@ -43,8 +43,6 @@ class _FileDataProvider:
checksum_md5: Optional[str] = field(default="", init=False)

def __post_init__(self) -> None:
logger.setLevel(level=self.verbosity)

if self.dataio.name:
self.name = self.dataio.name
else:
Expand Down
7 changes: 2 additions & 5 deletions src/fmu/dataio/_fmu_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from __future__ import annotations

import json
import logging
import pathlib
import re
from copy import deepcopy
Expand All @@ -22,12 +21,13 @@
from fmu.config import utilities as ut

from . import _utils
from ._logging import null_logger

# case metadata relative to rootpath
ERT2_RELATIVE_CASE_METADATA_FILE: Final = "share/metadata/fmu_case.yml"
RESTART_PATH_ENVNAME: Final = "RESTART_FROM_PATH"

logger: Final = logging.getLogger(__name__)
logger: Final = null_logger(__name__)


def _get_folderlist(current: Path) -> list:
Expand All @@ -49,7 +49,6 @@ class _FmuProvider:
"""Class for detecting the run environment (e.g. an ERT2) and provide metadata."""

dataio: Any
verbosity: str = "CRITICAL"

provider: Optional[str] = field(default=None, init=False)
is_fmurun: Optional[bool] = field(default=False, init=False)
Expand All @@ -68,8 +67,6 @@ class _FmuProvider:
rootpath: Optional[Path] = field(default=None, init=False)

def __post_init__(self) -> None:
logger.setLevel(level=self.verbosity)

self.rootpath = Path(self.dataio._rootpath.absolute())

self.rootpath_initial = self.rootpath
Expand Down
32 changes: 32 additions & 0 deletions src/fmu/dataio/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from __future__ import annotations

import logging


def null_logger(name: str) -> logging.Logger:
"""
Create and return a logger with a NullHandler.
This function creates a logger for the specified name and attaches a
NullHandler to it. The NullHandler prevents logging messages from being
automatically output to the console or other default handlers. This is
particularly useful in library modules where you want to provide the
users of the library the flexibility to configure their own logging behavior.
Args:
name (str): The name of the logger to be created. This is typically
the name of the module in which the logger is
created (e.g., using __name__).
Returns:
logging.Logger: A logger object configured with a NullHandler.
Example:
# In a library module
logger = null_logger(__name__)
logger.info("This info won't be logged to the console by default.")
"""

logger = logging.getLogger(name)
logger.addHandler(logging.NullHandler())
return logger
12 changes: 5 additions & 7 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import datetime
import getpass
import logging
from dataclasses import dataclass, field
from datetime import timezone
from pathlib import Path
Expand All @@ -29,7 +28,9 @@
read_named_envvar,
)

logger: Final = logging.getLogger(__name__)
from ._logging import null_logger

logger: Final = null_logger(__name__)


class ConfigurationError(ValueError):
Expand Down Expand Up @@ -214,7 +215,6 @@ class _MetaData:
# input variables
obj: Any
dataio: Any
verbosity: str = "CRITICAL"
compute_md5: bool = True

# storage state variables
Expand All @@ -238,7 +238,6 @@ class _MetaData:
meta_existing: dict = field(default_factory=dict, init=False)

def __post_init__(self) -> None:
logger.setLevel(level=self.verbosity)
logger.info("Initialize _MetaData instance.")

# one special case is that obj is a file path, and dataio.reuse_metadata_rule is
Expand Down Expand Up @@ -268,7 +267,7 @@ def _get_case_metadata(self) -> object:
The _FmuDataProvider is ran first -> self.fmudata
"""
self.fmudata = _FmuProvider(self.dataio, verbosity=self.verbosity)
self.fmudata = _FmuProvider(self.dataio)
self.fmudata.detect_provider()
logger.info("FMU provider is %s", self.fmudata.provider)
return self.fmudata.case_metadata
Expand All @@ -281,7 +280,7 @@ def _populate_meta_fmu(self) -> None:
The _FmuDataProvider is ran first -> self.fmudata
"""
self.fmudata = _FmuProvider(self.dataio, verbosity=self.verbosity)
self.fmudata = _FmuProvider(self.dataio)
self.fmudata.detect_provider()
logger.info("FMU provider is %s", self.fmudata.provider)
self.meta_fmu = self.fmudata.metadata
Expand Down Expand Up @@ -309,7 +308,6 @@ def _populate_meta_file(self) -> None:
Path(self.rootpath),
self.fmudata.iter_name,
self.fmudata.real_name,
self.verbosity,
)
fdata.derive_filedata()

Expand Down
4 changes: 2 additions & 2 deletions src/fmu/dataio/_objectdata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"""
from __future__ import annotations

import logging
from dataclasses import dataclass, field
from datetime import datetime as dt
from pathlib import Path
Expand All @@ -97,9 +96,10 @@
import xtgeo

from ._definitions import ALLOWED_CONTENTS, STANDARD_TABLE_INDEX_COLUMNS, _ValidFormats
from ._logging import null_logger
from ._utils import generate_description, parse_timedata

logger: Final = logging.getLogger(__name__)
logger: Final = null_logger(__name__)


class ConfigurationError(ValueError):
Expand Down
6 changes: 2 additions & 4 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import contextlib
import hashlib
import json
import logging
import os
import shutil
import uuid
Expand All @@ -21,8 +20,9 @@
from fmu.config import utilities as ut

from . import _design_kw
from ._logging import null_logger

logger: Final = logging.getLogger(__name__)
logger: Final = null_logger(__name__)


def detect_inside_rms() -> bool:
Expand Down Expand Up @@ -75,10 +75,8 @@ def export_metadata_file(
file: Path,
metadata: dict,
savefmt: Literal["yaml", "json"] = "yaml",
verbosity: str = "WARNING",
) -> None:
"""Export genericly and ordered to the complementary metadata file."""
logger.setLevel(level=verbosity)
if not metadata:
raise RuntimeError(
"Export of metadata was requested, but no metadata are present."
Expand Down
Loading

0 comments on commit 382393b

Please sign in to comment.