Skip to content

Commit

Permalink
Make core library unopinionated wrt. logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer authored Nov 26, 2020
1 parent 29f8751 commit b46f720
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 56 deletions.
1 change: 0 additions & 1 deletion pyscal/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from .scalrecommendation import SCALrecommendation
from .pyscallist import PyscalList

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


Expand Down
1 change: 0 additions & 1 deletion pyscal/gasoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from pyscal.constants import EPSILON as epsilon
from pyscal.constants import SWINTEGERS, MAX_EXPONENT

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


Expand Down
1 change: 0 additions & 1 deletion pyscal/gaswater.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from .wateroil import WaterOil
from .gasoil import GasOil

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


Expand Down
15 changes: 2 additions & 13 deletions pyscal/pyscalcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .gaswater import GasWater
from .factory import PyscalFactory

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

EPILOG = """
Expand Down Expand Up @@ -200,20 +199,10 @@ def pyscal_main(
family2 (bool): Dump family 2 keywords
"""

def set_logger_levels(loglevel):
"""Set log levels for all modules imported by this script"""
logger.setLevel(loglevel)
logging.getLogger("pyscal.factory").setLevel(loglevel)
logging.getLogger("pyscal.wateroil").setLevel(loglevel)
logging.getLogger("pyscal.wateroilgas").setLevel(loglevel)
logging.getLogger("pyscal.gasoil").setLevel(loglevel)
logging.getLogger("pyscal.utils").setLevel(loglevel)
logging.getLogger("pyscal.pyscallist").setLevel(loglevel)

if verbose:
set_logger_levels(logging.INFO)
logging.basicConfig(level=logging.INFO)
if debug:
set_logger_levels(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)

scalinput_df = PyscalFactory.load_relperm_df(parametertable, sheet_name=sheet_name)

Expand Down
1 change: 0 additions & 1 deletion pyscal/pyscallist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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

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


Expand Down
1 change: 0 additions & 1 deletion pyscal/scalrecommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pyscal import WaterOilGas, GasWater
from pyscal.utils.interpolation import interpolate_wo, interpolate_go

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


Expand Down
1 change: 0 additions & 1 deletion pyscal/utils/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ..constants import SWINTEGERS


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


Expand Down
1 change: 0 additions & 1 deletion pyscal/utils/monotonocity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pyscal.constants import EPSILON as epsilon


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


Expand Down
1 change: 0 additions & 1 deletion pyscal/utils/relperm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ..constants import EPSILON as epsilon


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


Expand Down
1 change: 0 additions & 1 deletion pyscal/utils/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from .monotonocity import modify_dframe_monotonocity, remap_deprecated_monotonocity

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


Expand Down
1 change: 0 additions & 1 deletion pyscal/wateroil.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from pyscal.constants import SWINTEGERS, MAX_EXPONENT


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


Expand Down
1 change: 0 additions & 1 deletion pyscal/wateroilgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .wateroil import WaterOil
from .gasoil import GasOil

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


Expand Down
59 changes: 27 additions & 32 deletions tests/test_pyscalcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,38 @@ def test_installed():
assert subprocess.check_output(["pyscal", "--version"])


def test_log_levels(tmpdir, caplog):
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Requires Python 3.7 or higher")
@pytest.mark.parametrize("verbosity_flag", [None, "--verbose", "--debug"])
def test_log_levels(tmpdir, verbosity_flag):
"""Test that we can control the log level from the command line
client, and get log output from modules deep down"""
relperm_file = Path(__file__).absolute().parent / "data/relperm-input-example.xlsx"

tmpdir.chdir()

caplog.clear()
sys.argv = ["pyscal", str(relperm_file)]
pyscalcli.main()

# Ensure we have no INFO logging:
assert not any(record.levelno == logging.INFO for record in caplog.records)

# The following is an INFO statement from factory.py that we should not get:
assert all("Loaded input data" not in str(record) for record in caplog.records)
# And a debug statement from gasoil.py
assert all(
"Added Corey gas to GasOil object" not in str(record)
for record in caplog.records
relperm_file = str(
Path(__file__).absolute().parent / "data" / "relperm-input-example.xlsx"
)

caplog.clear()
sys.argv = ["pyscal", "--verbose", str(relperm_file)]
pyscalcli.main()
assert not any(record.levelno == logging.DEBUG for record in caplog.records)
assert any(record.levelno == logging.INFO for record in caplog.records)
assert not any(record.levelno == logging.ERROR for record in caplog.records)
assert any("Loaded input data" in str(record) for record in caplog.records)
assert any("Dumping" in str(record) for record in caplog.records)

caplog.clear()
sys.argv = ["pyscal", "--debug", str(relperm_file)]
pyscalcli.main()
assert any(record.levelno == logging.DEBUG for record in caplog.records)
assert any("Initialized GasOil with" in str(record) for record in caplog.records)
assert any("Initialized WaterOil with" in str(record) for record in caplog.records)
commands = ["pyscal", relperm_file]
if verbosity_flag is not None:
commands.append(verbosity_flag)

result = subprocess.run(commands, cwd=tmpdir, capture_output=True, check=True)
output = result.stdout.decode() + result.stderr.decode()

if verbosity_flag is None:
assert "INFO:" not in output
assert "DEBUG:" not in output
elif verbosity_flag == "--verbose":
assert "INFO:" in output
assert "DEBUG:" not in output
assert "Loaded input data" in output
assert "Dumping" in output
elif verbosity_flag == "--debug":
assert "INFO:" in output
assert "DEBUG:" in output
assert "Initialized GasOil with" in output
assert "Initialized WaterOil with" in output
else:
raise ValueError("Unknown value for 'verbosity_flag'")


def test_pyscal_client_static(tmpdir, caplog, default_loglevel):
Expand Down

0 comments on commit b46f720

Please sign in to comment.