11import copy
2- from loguru import logger
2+ import logging
33from typing import Tuple , Union , Any , List , Optional
44import numpy as np
55
@@ -20,6 +20,7 @@ def __init__(
2020 parameters : Parameters ,
2121 thermal_limits : ThermalLimits ,
2222 is_dc : bool = False ,
23+ logger : Optional [logging .Logger ] = None ,
2324 ):
2425 """
2526 Initialise l'état du réseau avec des protections personnalisables.
@@ -42,6 +43,12 @@ def __init__(
4243 self ._timestep_overflow = np .zeros (self .thermal_limits .n_line , dtype = dt_int )
4344 self .infos : List [str ] = []
4445
46+ if logger is None :
47+ self .logger = logging .getLogger (__name__ )
48+ self .logger .disabled = True
49+ else :
50+ self .logger : logging .Logger = logger .getChild ("grid2op_BaseEnv" )
51+
4552 def _validate_input (self , backend : Backend , parameters : Optional [Parameters ]) -> None :
4653 if not isinstance (backend , Backend ):
4754 raise Grid2OpException (f"Argument 'backend' doit être de type 'Backend', reçu : { type (backend )} " )
@@ -55,12 +62,18 @@ def _run_power_flow(self) -> Optional[Exception]:
5562 try :
5663 return self .backend ._runpf_with_diverging_exception (self .is_dc )
5764 except Exception as e :
58- logger .error (f"Erreur flux de puissance : { e } " )
65+ if self .logger is not None :
66+ self .logger .error (
67+ f"Erreur flux de puissance : { e } "
68+ )
5969 return e
6070
6171 def _update_overflows (self , lines_flows : np .ndarray ) -> np .ndarray :
6272 if self ._thermal_limit_a is None :
63- logger .error ("Thermal limits must be provided for overflow calculations." )
73+ if self .logger is not None :
74+ self .logger .error (
75+ "Thermal limits must be provided for overflow calculations."
76+ )
6477 raise ValueError ("Thermal limits must be provided for overflow calculations." )
6578
6679 lines_status = self .backend .get_line_status () # self._thermal_limit_a reste fixe. self._soft_overflow_threshold = 1
@@ -77,7 +90,9 @@ def _disconnect_lines(self, lines_to_disconnect: np.ndarray, timestep: int) -> N
7790 for line_idx in np .where (lines_to_disconnect )[0 ]:
7891 self .backend ._disconnect_line (line_idx )
7992 self .disconnected_during_cf [line_idx ] = timestep
80- logger .warning (f"Ligne { line_idx } déconnectée au pas de temps { timestep } ." )
93+ if self .logger is not None :
94+ self .logger .warning (f"Ligne { line_idx } déconnectée au pas de temps { timestep } ." )
95+
8196
8297 def next_grid_state (self ) -> Tuple [np .ndarray , List [Any ], Union [None , Exception ]]:
8398 try :
@@ -99,7 +114,8 @@ def next_grid_state(self) -> Tuple[np.ndarray, List[Any], Union[None, Exception]
99114 return self .disconnected_during_cf , self .infos , None
100115
101116 except Exception as e :
102- logger .exception ("Erreur inattendue dans le calcul de l'état du réseau." )
117+ if self .logger is not None :
118+ self .exception ("Erreur inattendue dans le calcul de l'état du réseau." )
103119 return self .disconnected_during_cf , self .infos , e
104120
105121class NoProtection :
0 commit comments