Skip to content

Commit

Permalink
Removed logger module to increase framework compatibility with other …
Browse files Browse the repository at this point in the history
…loggers
  • Loading branch information
JoseJVS committed Sep 6, 2024
1 parent e8b15e1 commit 23518ac
Show file tree
Hide file tree
Showing 19 changed files with 440 additions and 285 deletions.
42 changes: 39 additions & 3 deletions examples/minimal_example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"""

import sys
import logging

from pathlib import Path
from json import load, dumps
from argparse import ArgumentParser
Expand All @@ -18,24 +21,57 @@
from my_parsers import time_parser, yml_parser


stderr = logging.StreamHandler(stream=sys.stderr)

simple_format = logging.Formatter("%(levelname)s : %(message)s")
info_format = logging.Formatter("%(levelname)s : %(module)s : %(message)s")
full_format = logging.Formatter(
"\n%(name)s | %(asctime)s | %(levelname)s : %(levelno)s |"
+ " %(filename)s : %(funcName)s : %(lineno)s | %(processName)s : %(process)d | %(message)s\n"
)

LOG = logging.getLogger(__name__)

arg_parser = ArgumentParser()
arg_parser.add_argument("--verbosity", type=str, default="info")
args = arg_parser.parse_args()


def set_level(level: str) -> None:
"""
Function used to set LOG object logging level.
Arguments:
level: logging level as string, available levels: warning, info, debug.
Returns:
success boolean.
"""
if level == "warning":
stderr.setFormatter(simple_format)
logging.basicConfig(level=logging.WARNING, handlers=(stderr,))
elif level == "info":
stderr.setFormatter(info_format)
logging.basicConfig(level=logging.INFO, handlers=(stderr,))
elif level == "debug":
stderr.setFormatter(full_format)
logging.basicConfig(level=logging.DEBUG, handlers=(stderr,))
else:
raise ValueError("Incorrect logging level %s", level)


if __name__ == "__main__":
set_level(args.verbosity)
config_path = Path(__file__).parent.resolve() / "config.json"
if not config_path.is_file():
raise FileNotFoundError(f"No config file at: {config_path}")

with config_path.open("r") as f:
config = load(f)
config["verbosity"] = args.verbosity

arch = Archivist(path="metadata_archive.tar", parsers=[time_parser(), yml_parser()], **config)

arch.parse()
arch.export()

print("\nResulting metadata:")
print(dumps(arch.get_metadata(), indent=4))
LOG.info("Resulting metadata:\n%s", dumps(arch.get_metadata(), indent=4))
42 changes: 39 additions & 3 deletions examples/namelists_example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"""

import sys
import logging

from pathlib import Path
from json import load, dumps
from argparse import ArgumentParser
Expand All @@ -19,24 +22,57 @@
from my_parser import nml_parser


stderr = logging.StreamHandler(stream=sys.stderr)

simple_format = logging.Formatter("%(levelname)s : %(message)s")
info_format = logging.Formatter("%(levelname)s : %(module)s : %(message)s")
full_format = logging.Formatter(
"\n%(name)s | %(asctime)s | %(levelname)s : %(levelno)s |"
+ " %(filename)s : %(funcName)s : %(lineno)s | %(processName)s : %(process)d | %(message)s\n"
)

LOG = logging.getLogger(__name__)

arg_parser = ArgumentParser()
arg_parser.add_argument("--verbosity", type=str, default="info")
args = arg_parser.parse_args()


def set_level(level: str) -> None:
"""
Function used to set LOG object logging level.
Arguments:
level: logging level as string, available levels: warning, info, debug.
Returns:
success boolean.
"""
if level == "warning":
stderr.setFormatter(simple_format)
logging.basicConfig(level=logging.WARNING, handlers=(stderr,))
elif level == "info":
stderr.setFormatter(info_format)
logging.basicConfig(level=logging.INFO, handlers=(stderr,))
elif level == "debug":
stderr.setFormatter(full_format)
logging.basicConfig(level=logging.DEBUG, handlers=(stderr,))
else:
raise ValueError("Incorrect logging level %s", level)


if __name__ == "__main__":
set_level(args.verbosity)
config_path = Path(__file__).parent.resolve() / "config.json"
if not config_path.is_file():
raise FileNotFoundError(f"No config file at: {config_path}")

with config_path.open("r") as f:
config = load(f)
config["verbosity"] = args.verbosity

arch = Archivist(path="metadata_archive.tar", parsers=nml_parser(), **config)

arch.parse()
arch.export()

print("\nResulting metadata:")
print(dumps(arch.get_metadata(), indent=4))
LOG.info("Resulting metadata:\n%s", dumps(arch.get_metadata(), indent=4))
42 changes: 39 additions & 3 deletions examples/ncdump_example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"""

import sys
import logging

from pathlib import Path
from json import load, dumps
from argparse import ArgumentParser
Expand All @@ -16,24 +19,57 @@
from my_parser import ncdump_hs_parser


stderr = logging.StreamHandler(stream=sys.stderr)

simple_format = logging.Formatter("%(levelname)s : %(message)s")
info_format = logging.Formatter("%(levelname)s : %(module)s : %(message)s")
full_format = logging.Formatter(
"\n%(name)s | %(asctime)s | %(levelname)s : %(levelno)s |"
+ " %(filename)s : %(funcName)s : %(lineno)s | %(processName)s : %(process)d | %(message)s\n"
)

LOG = logging.getLogger(__name__)

arg_parser = ArgumentParser()
arg_parser.add_argument("--verbosity", type=str, default="info")
args = arg_parser.parse_args()


def set_level(level: str) -> None:
"""
Function used to set LOG object logging level.
Arguments:
level: logging level as string, available levels: warning, info, debug.
Returns:
success boolean.
"""
if level == "warning":
stderr.setFormatter(simple_format)
logging.basicConfig(level=logging.WARNING, handlers=(stderr,))
elif level == "info":
stderr.setFormatter(info_format)
logging.basicConfig(level=logging.INFO, handlers=(stderr,))
elif level == "debug":
stderr.setFormatter(full_format)
logging.basicConfig(level=logging.DEBUG, handlers=(stderr,))
else:
raise ValueError("Incorrect logging level %s", level)


if __name__ == "__main__":
set_level(args.verbosity)
config_path = Path(__file__).parent.resolve() / "config.json"
if not config_path.is_file():
raise FileNotFoundError(f"No config file at: {config_path}")

with config_path.open("r") as f:
config = load(f)
config["verbosity"] = args.verbosity

arch = Archivist(path="metadata_archive.tar", parsers=ncdump_hs_parser(), **config)

arch.parse()
arch.export()

print("\nResulting metadata:")
print(dumps(arch.get_metadata(), indent=4))
LOG.info("Resulting metadata:\n%s", dumps(arch.get_metadata(), indent=4))
46 changes: 40 additions & 6 deletions examples/schema_example1/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"""

import sys
import logging

from pathlib import Path
from json import dumps, dump
from argparse import ArgumentParser
Expand All @@ -19,11 +22,21 @@
from my_parsers import time_parser, yml_parser


stderr = logging.StreamHandler(stream=sys.stderr)

simple_format = logging.Formatter("%(levelname)s : %(message)s")
info_format = logging.Formatter("%(levelname)s : %(module)s : %(message)s")
full_format = logging.Formatter(
"\n%(name)s | %(asctime)s | %(levelname)s : %(levelno)s |"
+ " %(filename)s : %(funcName)s : %(lineno)s | %(processName)s : %(process)d | %(message)s\n"
)

LOG = logging.getLogger(__name__)

arg_parser = ArgumentParser()
arg_parser.add_argument("--verbosity", type=str, default="info")
args = arg_parser.parse_args()


my_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "my example schema",
Expand All @@ -45,7 +58,31 @@
}


def set_level(level: str) -> None:
"""
Function used to set LOG object logging level.
Arguments:
level: logging level as string, available levels: warning, info, debug.
Returns:
success boolean.
"""
if level == "warning":
stderr.setFormatter(simple_format)
logging.basicConfig(level=logging.WARNING, handlers=(stderr,))
elif level == "info":
stderr.setFormatter(info_format)
logging.basicConfig(level=logging.INFO, handlers=(stderr,))
elif level == "debug":
stderr.setFormatter(full_format)
logging.basicConfig(level=logging.DEBUG, handlers=(stderr,))
else:
raise ValueError("Incorrect logging level %s", level)


if __name__ == "__main__":
set_level(args.verbosity)
arch = Archivist(
path="metadata_archive.tar",
parsers=[time_parser(), yml_parser()],
Expand All @@ -55,17 +92,14 @@
output_file="metadata.json",
overwrite=True,
auto_cleanup=True,
verbosity=args.verbosity,
)

arch.parse()
arch.export()

print("\nResulting schema:")
formatted_schema = arch.get_formatted_schema()
print(dumps(formatted_schema, indent=4))
LOG.info("Resulting schema:\n%s", dumps(formatted_schema, indent=4))
with Path("schema.json").open("w") as f:
dump(formatted_schema, f, indent=4)

print("\nResulting metadata:")
print(dumps(arch.get_metadata(), indent=4))
LOG.info("Resulting metadata:\n%s", dumps(arch.get_metadata(), indent=4))
46 changes: 40 additions & 6 deletions examples/schema_example2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"""

import sys
import logging

from pathlib import Path
from json import dumps, dump
from argparse import ArgumentParser
Expand All @@ -24,11 +27,21 @@
)


stderr = logging.StreamHandler(stream=sys.stderr)

simple_format = logging.Formatter("%(levelname)s : %(message)s")
info_format = logging.Formatter("%(levelname)s : %(module)s : %(message)s")
full_format = logging.Formatter(
"\n%(name)s | %(asctime)s | %(levelname)s : %(levelno)s |"
+ " %(filename)s : %(funcName)s : %(lineno)s | %(processName)s : %(process)d | %(message)s\n"
)

LOG = logging.getLogger(__name__)

arg_parser = ArgumentParser()
arg_parser.add_argument("--verbosity", type=str, default="info")
args = arg_parser.parse_args()


my_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "my example schema 2",
Expand Down Expand Up @@ -70,7 +83,31 @@
}


def set_level(level: str) -> None:
"""
Function used to set LOG object logging level.
Arguments:
level: logging level as string, available levels: warning, info, debug.
Returns:
success boolean.
"""
if level == "warning":
stderr.setFormatter(simple_format)
logging.basicConfig(level=logging.WARNING, handlers=(stderr,))
elif level == "info":
stderr.setFormatter(info_format)
logging.basicConfig(level=logging.INFO, handlers=(stderr,))
elif level == "debug":
stderr.setFormatter(full_format)
logging.basicConfig(level=logging.DEBUG, handlers=(stderr,))
else:
raise ValueError("Incorrect logging level %s", level)


if __name__ == "__main__":
set_level(args.verbosity)
arch = Archivist(
path="metadata_archive.tar",
parsers=[
Expand All @@ -85,19 +122,16 @@
output_file="metadata.json",
overwrite=True,
auto_cleanup=True,
verbosity=args.verbosity,
add_description=True,
add_type=True,
)

arch.parse()
arch.export()

print("\nResulting schema:")
formatted_schema = arch.get_formatted_schema()
print(dumps(formatted_schema, indent=4))
LOG.info("Resulting schema:\n%s", dumps(formatted_schema, indent=4))
with Path("schema.json").open("w") as f:
dump(formatted_schema, f, indent=4)

print("\nResulting metadata:")
print(dumps(arch.get_metadata(), indent=4))
LOG.info("Resulting metadata:\n%s", dumps(arch.get_metadata(), indent=4))
Loading

0 comments on commit 23518ac

Please sign in to comment.