diff --git a/mloggers/__init__.py b/mloggers/__init__.py index 8113fae..8c13d15 100644 --- a/mloggers/__init__.py +++ b/mloggers/__init__.py @@ -1,3 +1,7 @@ +""" +A collection of loggers well-suited for machine learning experiments. +""" + from ._log_levels import LogLevel, register_level from .console_logger import ConsoleLogger from .file_logger import FileLogger diff --git a/mloggers/console_logger.py b/mloggers/console_logger.py index 88dfd0a..21ad21a 100644 --- a/mloggers/console_logger.py +++ b/mloggers/console_logger.py @@ -1,4 +1,3 @@ -import json from datetime import datetime from typing import Any @@ -7,6 +6,7 @@ from mloggers._log_levels import LogLevel, _log_level_properties from mloggers.logger import Logger +from mloggers.utils import serialize class ConsoleLogger(Logger): @@ -42,15 +42,13 @@ def log( else: level_clr = colored(level_str, "green") - # The first level of the dictionary is printed as a multiline - # indented message. - # The rest of the levels are printed as a single line - # prettifyed depending on the type of the value. + # The first level of the dictionary is printed as a multiline indented message. + # The rest of the levels are printed as a single line prettifyed depending on the type of the value. # Handle multiple messages if len(messages) > 1: messages = list(messages) - # If the messages are strings, join them into a single string. + # If the messages are strings, join them into a single string if all( hasattr(message, "__str__") and callable(getattr(message, "__str__")) @@ -58,7 +56,7 @@ def log( for message in messages ): messages = " ".join([str(message) for message in messages]) - # If the messages are dictionaries, log them separately. + # If the messages are dictionaries, log them separately else: for message in messages: self.log(message, level=level) @@ -75,15 +73,28 @@ def log( if isinstance(value, float): print(f"{level_clr}{time} {key}: {value:.5f}") - elif isinstance(value, dict | list): - value = json.dumps(value, indent=4) - print(f"{level_clr}{time} {key}: {value}") elif value is None: # Used for headers, titles, etc. print(f"{level_clr}{time} {key}") else: - print(f"{level_clr}{time} {key}: {value}") + try: + value = serialize(value) + print(f"{level_clr}{time} {key}: {value}") + except TypeError as e: + print( + f'{colored("[ERROR]", "red")} [ConsoleLogger] Could not convert the message to a JSON serializable format: {e}' + ) + continue first = False elif hasattr(messages, "__str__") and callable(getattr(messages, "__str__")): print(f"{level_clr}{time} {str(messages)}") + + else: + try: + serialized = serialize(messages) + print(f"{level_clr}{time} {serialized}") + except TypeError as e: + print( + f'{colored("[ERROR]", "red")} [ConsoleLogger] Could not convert the message to a JSON serializable format: {e}' + ) diff --git a/pyproject.toml b/pyproject.toml index d79b291..0582e1b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "mloggers" -version = "1.3.3" +version = "1.3.4" authors = [ { name = "Sergio Hernandez Gutierrez", email = "contact.sergiohernandez@gmail.com" }, { name = "Matteo Merler", email = "matteo.merler@gmail.com" },