From 93b5dcf1e6c76cd00240eea0859cb5894be40ea3 Mon Sep 17 00:00:00 2001 From: DEBREUVE Eric <eric.debreuve@cnrs.fr> Date: Tue, 13 Aug 2024 12:20:38 +0200 Subject: [PATCH] logging exception as error now a method, fixed strange (False,) as boolean default (automatic IDE "correction") --- package/logger_36/catalog/handler/generic.py | 2 +- package/logger_36/catalog/logger/exception.py | 74 ------------------- package/logger_36/logger.py | 1 - package/logger_36/type/logger.py | 22 +++++- package/logger_36/version.py | 2 +- 5 files changed, 21 insertions(+), 80 deletions(-) delete mode 100644 package/logger_36/catalog/logger/exception.py diff --git a/package/logger_36/catalog/handler/generic.py b/package/logger_36/catalog/handler/generic.py index eac8063..bb49dcb 100644 --- a/package/logger_36/catalog/handler/generic.py +++ b/package/logger_36/catalog/handler/generic.py @@ -57,7 +57,7 @@ class generic_handler_t(lggg.Handler): formatter: d.InitVar[lggg.Formatter | None] = None supports_html: d.InitVar[bool] = False - should_record: d.InitVar[bool] = (False,) + should_record: d.InitVar[bool] = False rich_kwargs: d.InitVar[dict[str, h.Any] | None] = None interface: d.InitVar[interface_h | None] = None # Cannot be None actually. diff --git a/package/logger_36/catalog/logger/exception.py b/package/logger_36/catalog/logger/exception.py deleted file mode 100644 index ac94259..0000000 --- a/package/logger_36/catalog/logger/exception.py +++ /dev/null @@ -1,74 +0,0 @@ -""" -Copyright CNRS/Inria/UniCA -Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 -SEE COPYRIGHT NOTICE BELOW -""" - -import traceback as tcbk - -from logger_36.instance.logger import LOGGER -from logger_36.type.logger import logger_t - - -def LogException( - exception: Exception, - /, - *, - logger: logger_t = LOGGER, - should_remove_caller: bool = False, -) -> None: - """""" - lines = tcbk.format_exception(exception) - if should_remove_caller: - message = "\n".join(lines[:1] + lines[2:]) - else: - formatted = "".join(lines) - message = f"{type(exception).__name__}:\n{formatted}" - logger.error(message) - - -""" -COPYRIGHT NOTICE - -This software is governed by the CeCILL license under French law and -abiding by the rules of distribution of free software. You can use, -modify and/ or redistribute the software under the terms of the CeCILL -license as circulated by CEA, CNRS and INRIA at the following URL -"http://www.cecill.info". - -As a counterpart to the access to the source code and rights to copy, -modify and redistribute granted by the license, users are provided only -with a limited warranty and the software's author, the holder of the -economic rights, and the successive licensors have only limited -liability. - -In this respect, the user's attention is drawn to the risks associated -with loading, using, modifying and/or developing or reproducing the -software by the user in light of its specific status of free software, -that may mean that it is complicated to manipulate, and that also -therefore means that it is reserved for developers and experienced -professionals having in-depth computer knowledge. Users are therefore -encouraged to load and test the software's suitability as regards their -requirements in conditions enabling the security of their systems and/or -data to be ensured and, more generally, to use and operate it in the -same conditions as regards security. - -The fact that you are presently reading this means that you have had -knowledge of the CeCILL license and that you accept its terms. - -SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. - -This software is being developed by Eric Debreuve, a CNRS employee and -member of team Morpheme. -Team Morpheme is a joint team between Inria, CNRS, and UniCA. -It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory -I3S, and Laboratory iBV. - -CNRS: https://www.cnrs.fr/index.php/en -Inria: https://www.inria.fr/en/ -UniCA: https://univ-cotedazur.eu/ -Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ -I3S: https://www.i3s.unice.fr/en/ -iBV: http://ibv.unice.fr/ -Team Morpheme: https://team.inria.fr/morpheme/ -""" diff --git a/package/logger_36/logger.py b/package/logger_36/logger.py index 5f69ead..41502a2 100644 --- a/package/logger_36/logger.py +++ b/package/logger_36/logger.py @@ -5,7 +5,6 @@ SEE COPYRIGHT NOTICE BELOW """ from logger_36.catalog.logger.chronos import LogElapsedTime -from logger_36.catalog.logger.exception import LogException from logger_36.catalog.logger.memory import LogMaximumMemoryUsage, LogMemoryUsages from logger_36.catalog.logger.system import LogSystemDetails diff --git a/package/logger_36/type/logger.py b/package/logger_36/type/logger.py index da00849..2abe775 100644 --- a/package/logger_36/type/logger.py +++ b/package/logger_36/type/logger.py @@ -7,7 +7,7 @@ SEE COPYRIGHT NOTICE BELOW import dataclasses as d import logging as lggg import sys as sstm -import traceback as tbck +import traceback as tcbk import types as t import typing as h from datetime import datetime as dttm @@ -221,6 +221,22 @@ class logger_t(lggg.Logger): # __post_init__ set self.exit_on_critical if self.exit_on_error. sstm.exit(1) + def error_for_exception( + self, + exception: Exception, + /, + *, + should_remove_caller: bool = False, + ) -> None: + """""" + lines = tcbk.format_exception(exception) + if should_remove_caller: + message = "\n".join(lines[:1] + lines[2:]) + else: + formatted = "".join(lines) + message = f"{type(exception).__name__}:\n{formatted}" + self.error(message) + def AddContextLevel(self, new_level: str, /) -> None: """""" self.context_levels.append(new_level) @@ -292,11 +308,11 @@ class logger_t(lggg.Logger): try: raise level("\n" + issues) except Exception as exception: - lines = ["Traceback (most recent call last):"] + tbck.format_stack()[ + lines = ["Traceback (most recent call last):"] + tcbk.format_stack()[ :-1 ] lines[-1] = lines[-1][:-1] - lines.extend(tbck.format_exception_only(exception)) + lines.extend(tcbk.format_exception_only(exception)) print("\n".join(lines), file=sstm.stderr) sstm.exit(1) diff --git a/package/logger_36/version.py b/package/logger_36/version.py index 5bc58c7..693c583 100644 --- a/package/logger_36/version.py +++ b/package/logger_36/version.py @@ -4,7 +4,7 @@ Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 SEE COPYRIGHT NOTICE BELOW """ -__version__ = "2024.19" +__version__ = "2024.20" """ COPYRIGHT NOTICE -- GitLab