1
Fork 0
mirror of https://github.com/Steffo99/unimore-bda-6.git synced 2024-11-21 15:34:18 +00:00

Configure file logging

This commit is contained in:
Steffo 2023-02-10 04:07:34 +01:00
parent 2bd97cbad2
commit 8907272002
Signed by: steffo
GPG key ID: 2A24051445686895
4 changed files with 49 additions and 19 deletions

1
.gitignore vendored
View file

@ -13,6 +13,7 @@ data/nltk/
data/training/
data/validation/
data/evaluation/
data/logs/
##################
# Python ignores #

View file

@ -10,6 +10,8 @@
<excludeFolder url="file://$MODULE_DIR$/.venv" />
<excludeFolder url="file://$MODULE_DIR$/data/evaluation" />
<excludeFolder url="file://$MODULE_DIR$/data/training" />
<excludeFolder url="file://$MODULE_DIR$/data/validation" />
<excludeFolder url="file://$MODULE_DIR$/data/logs" />
</content>
<orderEntry type="jdk" jdkName="Poetry (unimore-bda-6)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />

View file

@ -1,8 +1,8 @@
import logging
import pymongo.errors
from .log import install_log_handler
from .log import install_general_log_handlers
install_log_handler()
install_general_log_handlers()
from .config import config
from .database import mongo_client_from_config, reviews_collection, sample_reviews_polar, sample_reviews_varied
@ -36,7 +36,7 @@ def main():
for sample_func in [sample_reviews_varied, sample_reviews_polar]:
slog = logging.getLogger(f"{__name__}.{sample_func.__name__}")
slog.debug("Selected sample_func: %s", sample_func)
slog.debug("Selected sample_func: %s", sample_func.__name__)
for SentimentAnalyzer in [
TensorflowSentimentAnalyzer,
@ -44,7 +44,7 @@ def main():
]:
slog = logging.getLogger(f"{__name__}.{sample_func.__name__}.{SentimentAnalyzer.__name__}")
slog.debug("Selected SentimentAnalyzer: %s", SentimentAnalyzer)
slog.debug("Selected SentimentAnalyzer: %s", SentimentAnalyzer.__name__)
for Tokenizer in [
PlainTokenizer,
@ -55,7 +55,7 @@ def main():
]:
slog = logging.getLogger(f"{__name__}.{sample_func.__name__}.{SentimentAnalyzer.__name__}.{Tokenizer.__name__}")
slog.debug("Selected Tokenizer: %s", Tokenizer)
slog.debug("Selected Tokenizer: %s", Tokenizer.__name__)
run_counter = 0
@ -66,10 +66,10 @@ def main():
slog.debug("Run #%d", run_counter)
try:
slog.debug("Instantiating %s with %s...", SentimentAnalyzer, Tokenizer)
slog.debug("Instantiating %s with %s...", SentimentAnalyzer.__name__, Tokenizer.__name__)
sa = SentimentAnalyzer(tokenizer=Tokenizer())
except TypeError:
slog.warning("%s is not supported by %s, skipping run...", SentimentAnalyzer, Tokenizer)
slog.warning("%s is not supported by %s, skipping run...", SentimentAnalyzer.__name__, Tokenizer.__name__)
break
with Caches.from_database_samples(collection=reviews, sample_func=sample_func) as datasets:

View file

@ -1,22 +1,35 @@
import collections
import logging
import coloredlogs
import pathlib
this_log = logging.getLogger(__name__)
def install_log_handler(loggers: list[logging.Logger] = None):
if loggers is None:
loggers = [
logging.getLogger("__main__"),
logging.getLogger("unimore_bda_6"),
]
def install_general_log_handlers():
"""
Setup the `unimore_bda_6` and ``__main__`` loggers.
for logger in loggers:
With colors!
"""
main_logger: logging.Logger = logging.getLogger("__main__")
interesting_loggers: list[logging.Logger] = [
main_logger,
logging.getLogger("unimore_bda_6"),
]
chatty_loggers: list[logging.Logger] = [
logging.getLogger("unimore_bda_6.database.cache"),
logging.getLogger("unimore_bda_6.database.datatypes"),
]
this_log.debug("Installing console handlers...")
for logger in interesting_loggers:
coloredlogs.install(
logger=logger,
level="DEBUG" if __debug__ else "INFO",
fmt="{asctime} | {name:<80} | {levelname:>8} | {message}",
fmt="{asctime} | {name} | {levelname} | {message}",
style="{",
level_styles=dict(
debug=dict(color="white"),
@ -32,10 +45,24 @@ def install_log_handler(loggers: list[logging.Logger] = None):
),
isatty=True,
)
this_log.debug("Installed custom log handler on: %s", logger)
this_log.debug("Installed console log handler on: %s", logger)
logging.getLogger("unimore_bda_6.database.cache").setLevel("INFO")
logging.getLogger("unimore_bda_6.database.datatypes").setLevel("INFO")
this_log.debug("Silencing chatty loggers...")
for logger in chatty_loggers:
logger.setLevel("INFO")
this_log.debug("Silenced: %s", logger)
log_file_path = pathlib.Path("./data/logs/last_run.tsv")
log_directory_path = log_file_path.parent
this_log.debug("Ensuring %s exists...", log_directory_path)
log_directory_path.mkdir(parents=True, exist_ok=True)
this_log.debug("Ensuring %s exists...", log_file_path)
open(log_file_path, "w").close()
this_log.debug("Installing FileHandler for the __main__ logger at %s...", log_file_path)
file_handler = logging.FileHandler(log_file_path, mode="w")
file_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{")
main_logger.addHandler(file_handler)
this_log.debug("Installed FileHandler for the __main__ logger at %s!", log_file_path)
_passage_counts = collections.defaultdict(lambda: 0)
@ -48,6 +75,6 @@ def count_passage(log: logging.Logger, key: str, mod: int):
__all__ = (
"install_log_handler",
"install_general_log_handlers",
"count_passage",
)