diff --git a/ecologits/log.py b/ecologits/log.py index eea436a..edf05a3 100644 --- a/ecologits/log.py +++ b/ecologits/log.py @@ -1,3 +1,32 @@ import logging + +class EcoLogitsLogger(logging.Logger): + + def __init__(self, name, level=logging.NOTSET): + super().__init__(name, level) + self.__once_messages = set() + + def _log_once(self, level, msg, *args, **kwargs): + if msg not in self.__once_messages: + self.__once_messages.add(msg) + self.log(level, msg, *args, **kwargs) + + def debug_once(self, msg, *args, **kwargs): + self._log_once(logging.DEBUG, msg, *args, **kwargs) + + def info_once(self, msg, *args, **kwargs): + self._log_once(logging.INFO, msg, *args, **kwargs) + + def warning_once(self, msg, *args, **kwargs): + self._log_once(logging.WARNING, msg, *args, **kwargs) + + def error_once(self, msg, *args, **kwargs): + self._log_once(logging.ERROR, msg, *args, **kwargs) + + def critical_once(self, msg, *args, **kwargs): + self._log_once(logging.CRITICAL, msg, *args, **kwargs) + + +logging.setLoggerClass(EcoLogitsLogger) logger = logging.getLogger(__name__) diff --git a/ecologits/tracers/utils.py b/ecologits/tracers/utils.py index 494b0ee..06b77e6 100644 --- a/ecologits/tracers/utils.py +++ b/ecologits/tracers/utils.py @@ -75,8 +75,9 @@ def llm_impacts( model = models.find_model(provider=provider, model_name=model_name) if model is None: - logger.debug(f"Could not find model `{model_name}` for {provider} provider.") - return ImpactsOutput(errors=[ModelNotRegisteredError()]) + error = ModelNotRegisteredError(message=f"Could not find model `{model_name}` for {provider} provider.") + logger.warning_once(str(error)) + return ImpactsOutput(errors=[error]) if isinstance(model.architecture.parameters, ParametersMoE): model_total_params = model.architecture.parameters.total @@ -87,8 +88,11 @@ def llm_impacts( electricity_mix = electricity_mixes.find_electricity_mix(zone=electricity_mix_zone) if electricity_mix is None: - logger.debug(f"Could not find electricity mix `{electricity_mix_zone}` in the ADEME database") - return ImpactsOutput(errors=[ZoneNotRegisteredError()]) + error = ModelNotRegisteredError(message=f"Could not find electricity mix `{electricity_mix_zone}` in the " + f"ADEME database.") + logger.warning_once(str(error)) + return ImpactsOutput(errors=[error]) + if_electricity_mix_adpe=electricity_mix.adpe if_electricity_mix_pe=electricity_mix.pe if_electricity_mix_gwp=electricity_mix.gwp @@ -105,6 +109,7 @@ def llm_impacts( if model.has_warnings: for w in model.warnings: + logger.warning_once(str(w)) impacts.add_warning(w) return impacts