Skip to content

Commit

Permalink
Feature/logdict (#17)
Browse files Browse the repository at this point in the history
* doc: removed bitbucket pipeline [ci-skip]

* chore: moved pipeline and created reduced conda env spec

* feat: added logging dict functionnality to logger

* fix: polished log commit

---------

Co-authored-by: Julien Lerat <ler015@csiro.au>
  • Loading branch information
jlerat and Julien Lerat authored Feb 14, 2024
1 parent 337b454 commit af1cb71
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
31 changes: 21 additions & 10 deletions hydrodiy/io/iutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
import numpy as np
import pandas as pd

# Configuration of logging messages
LOGGER_NSEPARATORS = 50
LOGGER_SEPARATOR = "-"
LOGGER_NSEPARATORS_CONTEXTUAL = 3
LOGGER_SEPARATOR_CONTEXTUAL = "#"


def script_template(filename, comment,
type="simple",
Expand Down Expand Up @@ -129,6 +123,9 @@ def __init__(self, logger):
assert isinstance(logger, logging.Logger), errmess
self._logger = logger

def get_separator(self, nsep, sep):
return sep*nsep

def error(self, msg, *args, **kwargs):
return self._logger.error(msg, *args, **kwargs)

Expand All @@ -147,14 +144,29 @@ def handlers(self):

def started(self):
self.info("@@@ Process started @@@")
self.info(LOGGER_SEPARATOR*LOGGER_NSEPARATORS)
self.info(self.get_separator("-", 30))
self.info("")

def completed(self):
self.info("")
self.info(LOGGER_SEPARATOR*LOGGER_NSEPARATORS)
self.info(self.get_separator("-", 30))
self.info("@@@ Process completed @@@")

def log_dict(self, tolog, name="", level="info"):
""" Add log entry for dictionnary (e.g. created from argparse using vars)"""
assert level in ["info", "warning", "critical", "error"]
logfun = getattr(self, level)
sep = self.get_separator("+", 20)
logfun(sep)
if name!="":
logfun(f"{name}:")
for k, v in tolog.items():
logfun(" "*4+f"{k} = {v}")

logfun(sep)
logfun("")



class ContextualLogger(StartedCompletedLogger):
""" Add context to logging messages via the context attribute """
Expand All @@ -173,8 +185,7 @@ def context(self, value):
self.info("")
self._context = str(value)
if self._context != "":
sep = LOGGER_SEPARATOR_CONTEXTUAL\
*LOGGER_NSEPARATORS_CONTEXTUAL
sep = self.get_separator("#", 3)
mess = sep+" "+self._context+" "+sep
self.info(mess)

Expand Down
1 change: 1 addition & 0 deletions hydrodiy/io/script_template_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#------------------------------------------------------------
basename = source_file.stem
LOGGER = iutils.get_logger(basename)
LOGGER.log_dict(vars(args), "Command line arguments")

#------------------------------------------------------------
# @Get data
Expand Down
1 change: 1 addition & 0 deletions hydrodiy/io/script_template_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
flog = froot / "logs" / f"{basename}.log"
flog.parent.mkdir(exist_ok=True)
LOGGER = iutils.get_logger(basename, console=False, contextual=True)
LOGGER.log_dict(vars(args), "Command line arguments")

#----------------------------------------------------------------------
# @Get data
Expand Down
7 changes: 5 additions & 2 deletions hydrodiy/io/tests/test_hyio_iutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def test_get_logger():
logger1.error("error")
logger1.critical("critical")
logger1.warning("warning")
logger1.log_dict({"a": 1, "b": "asdasd"}, "test", "warning")

assert flog1.exists()

Expand All @@ -136,6 +137,8 @@ def test_get_logger():
ck = ck & txt[5].strip().endswith("ERROR | error")
ck = ck & txt[6].strip().endswith("CRITICAL | critical")
ck = ck & txt[7].strip().endswith("WARNING | warning")
ck = ck & txt[9].strip().endswith("WARNING | test:")
ck = ck & txt[10].strip().endswith("WARNING | a = 1")
assert ck

# Test logging with different format
Expand All @@ -152,8 +155,8 @@ def test_get_logger():

with open(flog2, "r") as fl:
txt = fl.readlines()
expected = ["@@@ Process started @@@", "-"*50, ""]+mess+\
["", "-"*50, "@@@ Process completed @@@"]
expected = ["@@@ Process started @@@", "-"*30, ""]+mess+\
["", "-"*30, "@@@ Process completed @@@"]
assert expected == [t.strip() for t in txt]

# Close log file handler and delete files
Expand Down

0 comments on commit af1cb71

Please sign in to comment.