Skip to content

Commit 29aeed6

Browse files
committed
Add init_ers method in LHC
1 parent b5b2b97 commit 29aeed6

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

src/daqpytools/apps/logging_demonstrator.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ def test_handlertypes(main_logger):
143143

144144

145145
def test_handlerconf(main_logger):
146+
#* Test the routing to the Base and Opmon streams
147+
handlerconf = LogHandlerConf(init_ers=False) # False is the default
148+
main_logger.warning("Handlerconf Base", extra=handlerconf.Base)
149+
main_logger.warning("Handlerconf Opmon", extra=handlerconf.Opmon)
150+
146151
#* Interlude: Inject sample environment variables
147152
os.environ["DUNEDAQ_ERS_WARNING"] = "erstrace,throttle,lstdout"
148153
os.environ["DUNEDAQ_ERS_INFO"] = "erstrace,throttle,lstdout"
@@ -159,11 +164,13 @@ def test_handlerconf(main_logger):
159164
critical_out = f"{os.getenv('DUNEDAQ_ERS_CRITICAL')=}"
160165
main_logger.info(critical_out)
161166

162-
#* Test the routing to the Base and Opmon streams
163-
handlerconf = LogHandlerConf()
164-
main_logger.warning("Handlerconf Base", extra=handlerconf.Base)
165-
main_logger.warning("Handlerconf Opmon", extra=handlerconf.Opmon)
166-
167+
#* Init ERS stream
168+
# Note to developers:
169+
# HandlerConf will require that these variables are defined!
170+
# They come from the OKS, so whatever tools you have should have this up
171+
# You can also initialise via handlerconf = LogHandlerConf(init_ers=True)
172+
handlerconf.init_ERS()
173+
167174
#* Test ERS Streams
168175
main_logger.warning("ERS Warning erstrace,throttle,lstdout", extra=handlerconf.ERS)
169176
main_logger.info("ERS Info erstrace,throttle,lstdout", extra=handlerconf.ERS)

src/daqpytools/logging/handlers.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,21 @@ class LogHandlerConf:
196196
"""Dataclass that holds the various streams and relevant handlers.
197197
198198
Attributes:
199+
init_ers: If True, automatically initializes ERS configuration during construction.
199200
_BASE_HANDLERS: Private class variable for default base handlers
200201
_OPMON_HANDLERS: Private class variable for opmon handlers
201202
BASE_CONFIG: Class variable for base stream configuration
202203
OPMON_CONFIG: Class variable for opmon stream configuration
203204
ERS: Instance field for ERS configuration (loaded from environment)
204205
"""
206+
init_ers: bool = False
205207

206208
_BASE_HANDLERS: ClassVar[set] = {HandlerType.Stream, HandlerType.Rich,
207209
HandlerType.File
208210
}
209211
_OPMON_HANDLERS: ClassVar[set] = {HandlerType.Rich, HandlerType.Stream,
210212
HandlerType.File}
213+
_ERS: object = None
211214

212215
Base: ClassVar[dict] = {
213216
"handlers": _BASE_HANDLERS,
@@ -219,12 +222,43 @@ class LogHandlerConf:
219222
"stream": StreamType.OPMON
220223
}
221224

222-
ERS: dict=field(default_factory = lambda:
223-
{
224-
"ers_handlers": LogHandlerConf._get_oks_conf(),
225-
"stream": StreamType.ERS
226-
}
227-
)
225+
def __post_init__(self):
226+
"""Initialize ERS configuration if init_ers field is True.
227+
228+
This method is called automatically after dataclass initialization.
229+
If the init_ers attribute was set to True, it triggers the complete
230+
ERS initialization.
231+
"""
232+
if self.init_ers:
233+
self.init_ERS()
234+
235+
@property
236+
def ERS(self):
237+
"""Get the ERS configuration dictionary.
238+
239+
Returns:
240+
dict: Contains 'ers_handlers' and 'stream' configuration for ERS
241+
242+
Raises:
243+
AttributeError: If ERS has not been initialized (call init_ERS() first)
244+
"""
245+
if not self._ERS:
246+
raise AttributeError("ERS stream not initialised. Call init_ERS() first")
247+
return self._ERS
248+
249+
def init_ERS(self):
250+
"""Initialize ERS configuration from environment variables.
251+
252+
Loads ERS configuration from OKS environment variables and populates
253+
the _ERS dict with handlers and stream information.
254+
255+
Called automatically during construction if init_ers=True, or can be
256+
called manually afterwards.
257+
"""
258+
self._ERS = {
259+
"ers_handlers": LogHandlerConf._get_oks_conf(),
260+
"stream": StreamType.ERS
261+
}
228262

229263
@staticmethod
230264
def _convert_str_to_handlertype(handler_str: str) -> tuple[HandlerType,

0 commit comments

Comments
 (0)