Skip to content

Commit

Permalink
Logs split between stdout and stderr (#335)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomáš Buček <tomas.bucek@student.tuke.sk>
  • Loading branch information
ttomasbucek and Tomáš Buček authored Nov 19, 2021
1 parent f1fd9e1 commit d727f07
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 31 deletions.
84 changes: 77 additions & 7 deletions pyscal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
"""pyscal"""

import logging
import sys
from typing import Dict, List, Union

try:
from .version import version

__version__ = version
except ImportError:
__version__ = "0.0.0"


def getLogger_pyscal(
module_name: str = "pyscal", args_dict: Dict[str, Union[str, bool]] = None
) -> logging.Logger:
# pylint: disable=invalid-name
"""Provide a custom logger for pyscal
Logging output is by default split by logging levels (split between WARNING and
ERROR) to stdout and stderr, each log occurs in only one of the streams.
Args:
module_name: A suggested name for the logger, usually __name__ should be supplied
args_dict: Dictionary with contents from the argparse namespace object containing
only keys "output", "verbose" and "debug".
"""
logger = logging.getLogger(module_name)
if len(logger.handlers) != 0:
return logger

if args_dict is None:
args_dict = {}
formatter = logging.Formatter("%(levelname)s:%(name)s:%(message)s")

if args_dict.get("output", None) == "-":
# If main output is to stdout, we must send all logs to stderr:
default_handler = logging.StreamHandler(sys.stderr)
default_handler.setFormatter(formatter)
logger.addHandler(default_handler)
else:
# Split log messages to either stdout or stderr based on log level:
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.addFilter(lambda record: record.levelno < logging.ERROR)
stdout_handler.setFormatter(formatter)

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

logger.addHandler(stdout_handler)
logger.addHandler(stderr_handler)

# --debug overrides --verbose
if args_dict.get("debug", False):
logger.setLevel(logging.DEBUG)
elif args_dict.get("verbose", False):
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.WARNING)

if module_name == "pyscal.pyscalcli":
all_modules = [
"factory",
"gasoil",
"gaswater",
"pyscallist",
"scalrecommendation",
"wateroil",
"wateroilgas",
]
for module in all_modules:
module_logger = logging.getLogger("pyscal." + module)
module_logger.handlers = []
for handler in logger.handlers:
module_logger.addHandler(handler)
module_logger.setLevel(logger.level)

return logger


# The order of imports must be conserved to avoid circular imports:
from .wateroil import WaterOil # noqa
Expand All @@ -9,10 +86,3 @@
from .scalrecommendation import SCALrecommendation # noqa
from .pyscallist import PyscalList # noqa
from .factory import PyscalFactory # noqa

try:
from .version import version

__version__ = version
except ImportError:
__version__ = "0.0.0"
5 changes: 3 additions & 2 deletions pyscal/factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Factory functions for creating the pyscal objects"""

import logging
import zipfile
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Set, Union
Expand All @@ -10,6 +9,7 @@
import pandas as pd
import xlrd

from pyscal import getLogger_pyscal
from pyscal.utils import capillarypressure

from .gasoil import GasOil
Expand All @@ -19,7 +19,7 @@
from .wateroil import WaterOil
from .wateroilgas import WaterOilGas

logger = logging.getLogger(__name__)
logger = getLogger_pyscal(__name__)


def slicedict(dct: dict, keys: Iterable):
Expand Down Expand Up @@ -65,6 +65,7 @@ def slicedict(dct: dict, keys: Iterable):
"kroend",
"kromax",
]

GO_LET_GAS = ["lg", "eg", "tg"]
GO_LET_OIL = ["log", "eog", "tog"]

Expand Down
3 changes: 1 addition & 2 deletions pyscal/gasoil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Representing a GasOil object"""

import logging
from typing import Optional

import numpy as np
Expand All @@ -13,7 +12,7 @@
from pyscal.utils.relperm import crosspoint, estimate_diffjumppoint, truncate_zeroness
from pyscal.utils.string import comment_formatter, df2str

logger = logging.getLogger(__name__)
logger = pyscal.getLogger_pyscal(__name__)


class GasOil(object):
Expand Down
4 changes: 2 additions & 2 deletions pyscal/gaswater.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Object to represent GasWater, implemented as a Container
object for one WaterOil and one GasOil object"""

import logging
from typing import Optional

import pandas as pd

from pyscal import getLogger_pyscal
from pyscal.utils.relperm import crosspoint

from .gasoil import GasOil
from .wateroil import WaterOil

logger = logging.getLogger(__name__)
logger = getLogger_pyscal(__name__)


def is_documented_by(original):
Expand Down
18 changes: 10 additions & 8 deletions pyscal/pyscalcli.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
"""Command line tool for pyscal"""

import argparse
import logging
import sys
import traceback
from pathlib import Path
from typing import Optional

import pandas as pd

from pyscal import GasWater, SCALrecommendation, WaterOilGas, __version__
from pyscal import (
GasWater,
SCALrecommendation,
WaterOilGas,
__version__,
getLogger_pyscal,
)

from .factory import PyscalFactory

logger = logging.getLogger(__name__)

EPILOG = """
The parameter file should contain a table with at least the column
SATNUM, containing only consecutive integers starting at 1. Each row
Expand Down Expand Up @@ -196,10 +199,9 @@ def pyscal_main(
family2: Dump family 2 keywords
"""

if verbose:
logging.basicConfig(level=logging.INFO)
if debug:
logging.basicConfig(level=logging.DEBUG)
logger = getLogger_pyscal(
__name__, {"debug": debug, "verbose": verbose, "output": output}
)

parametertable = PyscalFactory.load_relperm_df(
parametertable, sheet_name=sheet_name
Expand Down
12 changes: 9 additions & 3 deletions pyscal/pyscallist.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"""Container class for list of Pyscal objects"""

import logging
import warnings
from pathlib import Path
from typing import List, Optional, Type, Union

import pandas as pd

from pyscal import GasOil, GasWater, SCALrecommendation, WaterOil, WaterOilGas
from pyscal import (
GasOil,
GasWater,
SCALrecommendation,
WaterOil,
WaterOilGas,
getLogger_pyscal,
)

PYSCAL_OBJECTS = [WaterOil, GasOil, GasWater, WaterOilGas, SCALrecommendation]

PyscalObjects = Union[WaterOil, GasOil, GasWater, WaterOilGas, SCALrecommendation]

logger = logging.getLogger(__name__)
logger = getLogger_pyscal(__name__)

warnings.filterwarnings("default", category=DeprecationWarning, module="pyscal")

Expand Down
5 changes: 2 additions & 3 deletions pyscal/scalrecommendation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""SCALrecommendation, container for low, base and high WaterOilGas objects"""

import copy
import logging
from typing import Optional, Set, Type, Union

import numpy as np

from pyscal import GasWater, WaterOilGas
from pyscal import GasWater, WaterOilGas, getLogger_pyscal
from pyscal.utils.interpolation import interpolate_go, interpolate_wo

logger = logging.getLogger(__name__)
logger = getLogger_pyscal(__name__)


class SCALrecommendation(object):
Expand Down
3 changes: 1 addition & 2 deletions pyscal/wateroil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Wateroil module"""

import logging
import math
from typing import Optional

Expand All @@ -15,7 +14,7 @@
from pyscal.utils.relperm import crosspoint, estimate_diffjumppoint, truncate_zeroness
from pyscal.utils.string import comment_formatter, df2str

logger = logging.getLogger(__name__)
logger = pyscal.getLogger_pyscal(__name__)


class WaterOil(object):
Expand Down
3 changes: 1 addition & 2 deletions pyscal/wateroilgas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Container object for one WaterOil and one GasOil object"""

import logging
from typing import Optional

import numpy as np
Expand All @@ -13,7 +12,7 @@
from .gasoil import GasOil
from .wateroil import WaterOil

logger = logging.getLogger(__name__)
logger = pyscal.getLogger_pyscal(__name__)


class WaterOilGas(object):
Expand Down
Loading

0 comments on commit d727f07

Please sign in to comment.