Skip to content

Commit

Permalink
feat: add logger methods to log a message only once
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelrince committed Dec 9, 2024
1 parent ae845aa commit 4ad263c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
29 changes: 29 additions & 0 deletions ecologits/log.py
Original file line number Diff line number Diff line change
@@ -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__)
13 changes: 9 additions & 4 deletions ecologits/tracers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 4ad263c

Please sign in to comment.