1
1
"""
2
2
Logging
3
3
"""
4
+ from __future__ import annotations
4
5
import logging
5
6
import os
6
7
@@ -29,7 +30,7 @@ class Logging:
29
30
# lock the configuration of the Logging
30
31
_lockConfig = _lockRing .getLock ("config" )
31
32
32
- def __init__ (self , father = None , name = "" ):
33
+ def __init__ (self , father : Logging | None = None , name : str = "" ):
33
34
"""
34
35
Initialization of the Logging object. By default, 'name' is empty,
35
36
because getChild only accepts string and the first empty string corresponds to the root logger.
@@ -93,15 +94,15 @@ def __init__(self, father=None, name=""):
93
94
# lockObjectLoader to protect the ObjectLoader singleton
94
95
self ._lockObjectLoader = self ._lockRing .getLock ("objectLoader" )
95
96
96
- def showHeaders (self , yesno = True ):
97
+ def showHeaders (self , yesno : bool = True ):
97
98
"""
98
99
Depending on the value, display or not the prefix of the message.
99
100
100
101
:param bool yesno: determine the log record format
101
102
"""
102
103
self ._setOption ("headerIsShown" , yesno )
103
104
104
- def showThreadIDs (self , yesno = True ):
105
+ def showThreadIDs (self , yesno : bool = True ):
105
106
"""
106
107
Depending on the value, display or not the thread ID.
107
108
Make sure to enable the headers: showHeaders(True) before
@@ -110,7 +111,7 @@ def showThreadIDs(self, yesno=True):
110
111
"""
111
112
self ._setOption ("threadIDIsShown" , yesno )
112
113
113
- def showTimeStamps (self , yesno = True ):
114
+ def showTimeStamps (self , yesno : bool = True ):
114
115
"""
115
116
Depending on the value, display or not the timestamp of the message.
116
117
Make sure to enable the headers: showHeaders(True) before
@@ -119,7 +120,7 @@ def showTimeStamps(self, yesno=True):
119
120
"""
120
121
self ._setOption ("timeStampIsShown" , yesno )
121
122
122
- def showContexts (self , yesno = True ):
123
+ def showContexts (self , yesno : bool = True ):
123
124
"""
124
125
Depending on the value, display or not the context of the message.
125
126
Make sure to enable the headers: showHeaders(True) before
@@ -128,7 +129,7 @@ def showContexts(self, yesno=True):
128
129
"""
129
130
self ._setOption ("contextIsShown" , yesno )
130
131
131
- def _setOption (self , optionName , value , directCall = True ):
132
+ def _setOption (self , optionName : str , value : bool , directCall : bool = True ):
132
133
"""
133
134
Depending on the value, modify the value of the option and propagate the option to the children.
134
135
The options of the children will be updated if they were not modified before by a developer.
@@ -155,7 +156,9 @@ def _setOption(self, optionName, value, directCall=True):
155
156
finally :
156
157
self ._lockOptions .release ()
157
158
158
- def registerBackend (self , desiredBackend , backendOptions = None , backendFilters = None ):
159
+ def registerBackend (
160
+ self , desiredBackend : str , backendOptions : dict | None = None , backendFilters : dict | None = None
161
+ ) -> bool :
159
162
"""
160
163
Attach a backend to the Logging object.
161
164
Convert backend name to backend class name to a Backend object and add it to the Logging object
@@ -184,7 +187,7 @@ def registerBackend(self, desiredBackend, backendOptions=None, backendFilters=No
184
187
self ._addBackend (_class ["Value" ], backendOptions , filterInstances )
185
188
return True
186
189
187
- def _addBackend (self , backendType , backendOptions = None , backendFilters = None ):
190
+ def _addBackend (self , backendType , backendOptions : dict | None = None , backendFilters : list | None = None ):
188
191
"""
189
192
Attach a Backend object to the Logging object.
190
193
@@ -205,7 +208,7 @@ def _addBackend(self, backendType, backendOptions=None, backendFilters=None):
205
208
self ._lockLevel .release ()
206
209
self ._lockOptions .release ()
207
210
208
- def _generateFilter (self , filterType , filterOptions = None ):
211
+ def _generateFilter (self , filterType : str , filterOptions : dict | None = None ):
209
212
"""
210
213
Create a filter and add it to the handler of the backend.
211
214
@@ -218,7 +221,7 @@ def _generateFilter(self, filterType, filterOptions=None):
218
221
return None
219
222
return _class ["Value" ](filterOptions )
220
223
221
- def setLevel (self , levelName ) :
224
+ def setLevel (self , levelName : str ) -> bool :
222
225
"""
223
226
Check if the level name exists and set it.
224
227
@@ -234,13 +237,13 @@ def setLevel(self, levelName):
234
237
return True
235
238
return False
236
239
237
- def getLevel (self ):
240
+ def getLevel (self ) -> str :
238
241
"""
239
242
:return: the name of the level
240
243
"""
241
244
return LogLevels .getLevel (self ._logger .getEffectiveLevel ())
242
245
243
- def shown (self , levelName ) :
246
+ def shown (self , levelName : str ) -> bool :
244
247
"""
245
248
Determine whether messages with a certain level will be displayed.
246
249
@@ -259,19 +262,19 @@ def shown(self, levelName):
259
262
self ._lockLevel .release ()
260
263
261
264
@classmethod
262
- def getName (cls ):
265
+ def getName (cls ) -> str :
263
266
"""
264
267
:return: "system name/component name"
265
268
"""
266
269
return cls ._componentName
267
270
268
- def getSubName (self ):
271
+ def getSubName (self ) -> str :
269
272
"""
270
273
:return: the name of the logger
271
274
"""
272
275
return self .name
273
276
274
- def getDisplayOptions (self ):
277
+ def getDisplayOptions (self ) -> dict [ str , bool ] :
275
278
"""
276
279
:return: the dictionary of the display options and their values. Must not be redefined
277
280
"""
@@ -284,7 +287,7 @@ def getDisplayOptions(self):
284
287
finally :
285
288
self ._lockOptions .release ()
286
289
287
- def __loadLogClass (self , modulePath ):
290
+ def __loadLogClass (self , modulePath : str ):
288
291
"""Load class thread-safe."""
289
292
# import ObjectLoader here to avoid a dependancy loop
290
293
from DIRAC .Core .Utilities .ObjectLoader import ObjectLoader
@@ -300,69 +303,71 @@ def __loadLogClass(self, modulePath):
300
303
self ._lockObjectLoader .release ()
301
304
302
305
@staticmethod
303
- def getAllPossibleLevels ():
306
+ def getAllPossibleLevels () -> list [ str ] :
304
307
"""
305
308
:return: a list of all levels available
306
309
"""
307
310
return LogLevels .getLevelNames ()
308
311
309
- def always (self , sMsg , sVarMsg = "" ):
312
+ def always (self , sMsg : str , sVarMsg : str = "" ) -> bool :
310
313
"""
311
314
Always level
312
315
"""
313
316
return self ._createLogRecord (LogLevels .ALWAYS , sMsg , sVarMsg )
314
317
315
- def notice (self , sMsg , sVarMsg = "" ):
318
+ def notice (self , sMsg : str , sVarMsg : str = "" ) -> bool :
316
319
"""
317
320
Notice level
318
321
"""
319
322
return self ._createLogRecord (LogLevels .NOTICE , sMsg , sVarMsg )
320
323
321
- def info (self , sMsg , sVarMsg = "" ):
324
+ def info (self , sMsg : str , sVarMsg : str = "" ) -> bool :
322
325
"""
323
326
Info level
324
327
"""
325
328
return self ._createLogRecord (LogLevels .INFO , sMsg , sVarMsg )
326
329
327
- def verbose (self , sMsg , sVarMsg = "" ):
330
+ def verbose (self , sMsg : str , sVarMsg : str = "" ) -> bool :
328
331
"""
329
332
Verbose level
330
333
"""
331
334
return self ._createLogRecord (LogLevels .VERBOSE , sMsg , sVarMsg )
332
335
333
- def debug (self , sMsg , sVarMsg = "" ):
336
+ def debug (self , sMsg : str , sVarMsg : str = "" ) -> bool :
334
337
"""
335
338
Debug level
336
339
"""
337
340
return self ._createLogRecord (LogLevels .DEBUG , sMsg , sVarMsg )
338
341
339
- def warn (self , sMsg , sVarMsg = "" ):
342
+ def warn (self , sMsg : str , sVarMsg : str = "" ) -> bool :
340
343
"""
341
344
Warn
342
345
"""
343
346
return self ._createLogRecord (LogLevels .WARN , sMsg , sVarMsg )
344
347
345
- def error (self , sMsg , sVarMsg = "" ):
348
+ def error (self , sMsg : str , sVarMsg : str = "" ) -> bool :
346
349
"""
347
350
Error level
348
351
"""
349
352
return self ._createLogRecord (LogLevels .ERROR , sMsg , sVarMsg )
350
353
351
- def exception (self , sMsg = "" , sVarMsg = "" , lException = False , lExcInfo = False ):
354
+ def exception (self , sMsg : str = "" , sVarMsg : str = "" , lException : bool = False , lExcInfo : bool = False ) -> bool :
352
355
"""
353
356
Exception level
354
357
"""
355
358
_ = lException # Make pylint happy
356
359
_ = lExcInfo
357
360
return self ._createLogRecord (LogLevels .ERROR , sMsg , sVarMsg , exc_info = True )
358
361
359
- def fatal (self , sMsg , sVarMsg = "" ):
362
+ def fatal (self , sMsg : str , sVarMsg : str = "" ) -> bool :
360
363
"""
361
364
Fatal level
362
365
"""
363
366
return self ._createLogRecord (LogLevels .FATAL , sMsg , sVarMsg )
364
367
365
- def _createLogRecord (self , level , sMsg , sVarMsg , exc_info = False , local_context = None ):
368
+ def _createLogRecord (
369
+ self , level : int , sMsg : str , sVarMsg : str , exc_info : bool = False , local_context : dict | None = None
370
+ ) -> bool :
366
371
"""
367
372
Create a log record according to the level of the message.
368
373
@@ -410,15 +415,15 @@ def _createLogRecord(self, level, sMsg, sVarMsg, exc_info=False, local_context=N
410
415
finally :
411
416
self ._lockLevel .release ()
412
417
413
- def showStack (self ):
418
+ def showStack (self ) -> bool :
414
419
"""
415
420
Display a debug message without any content.
416
421
417
422
:return: boolean, True if the message is sent, else False
418
423
"""
419
424
return self .debug ("" )
420
425
421
- def getSubLogger (self , subName ) :
426
+ def getSubLogger (self , subName : str ) -> Logging :
422
427
"""
423
428
Create a new Logging object, child of this Logging, if it does not exists.
424
429
@@ -459,7 +464,7 @@ class LocalSubLogger:
459
464
(see https://github.com/DIRACGrid/DIRAC/issues/5280)
460
465
"""
461
466
462
- def __init__ (self , logger , extra ):
467
+ def __init__ (self , logger : Logging , extra : dict ):
463
468
"""
464
469
:param logger: :py:class:`Logging` object on which to be based
465
470
:param extra: dictionary of extra information to be passed
@@ -468,63 +473,65 @@ def __init__(self, logger, extra):
468
473
self .logger = logger
469
474
self .extra = extra
470
475
471
- def always (self , sMsg , sVarMsg = "" ):
476
+ def always (self , sMsg : str , sVarMsg : str = "" ) -> bool :
472
477
"""
473
478
Always level
474
479
"""
475
480
return self .logger ._createLogRecord ( # pylint: disable=protected-access
476
481
LogLevels .ALWAYS , sMsg , sVarMsg , local_context = self .extra
477
482
)
478
483
479
- def notice (self , sMsg , sVarMsg = "" ):
484
+ def notice (self , sMsg : str , sVarMsg : str = "" ) -> bool :
480
485
"""
481
486
Notice level
482
487
"""
483
488
return self .logger ._createLogRecord ( # pylint: disable=protected-access
484
489
LogLevels .NOTICE , sMsg , sVarMsg , local_context = self .extra
485
490
)
486
491
487
- def info (self , sMsg , sVarMsg = "" ):
492
+ def info (self , sMsg : str , sVarMsg : str = "" ) -> bool :
488
493
"""
489
494
Info level
490
495
"""
491
496
return self .logger ._createLogRecord ( # pylint: disable=protected-access
492
497
LogLevels .INFO , sMsg , sVarMsg , local_context = self .extra
493
498
)
494
499
495
- def verbose (self , sMsg , sVarMsg = "" ):
500
+ def verbose (self , sMsg : str , sVarMsg : str = "" ) -> bool :
496
501
"""
497
502
Verbose level
498
503
"""
499
504
return self .logger ._createLogRecord ( # pylint: disable=protected-access
500
505
LogLevels .VERBOSE , sMsg , sVarMsg , local_context = self .extra
501
506
)
502
507
503
- def debug (self , sMsg , sVarMsg = "" ):
508
+ def debug (self , sMsg : str , sVarMsg : str = "" ) -> bool :
504
509
"""
505
510
Debug level
506
511
"""
507
512
return self .logger ._createLogRecord ( # pylint: disable=protected-access
508
513
LogLevels .DEBUG , sMsg , sVarMsg , local_context = self .extra
509
514
)
510
515
511
- def warn (self , sMsg , sVarMsg = "" ):
516
+ def warn (self , sMsg : str , sVarMsg : str = "" ) -> bool :
512
517
"""
513
518
Warn
514
519
"""
515
520
return self .logger ._createLogRecord ( # pylint: disable=protected-access
516
521
LogLevels .WARN , sMsg , sVarMsg , local_context = self .extra
517
522
)
518
523
519
- def error (self , sMsg , sVarMsg = "" ):
524
+ def error (self , sMsg : str , sVarMsg : str = "" ) -> bool :
520
525
"""
521
526
Error level
522
527
"""
523
528
return self .logger ._createLogRecord ( # pylint: disable=protected-access
524
529
LogLevels .ERROR , sMsg , sVarMsg , local_context = self .extra
525
530
)
526
531
527
- def exception (self , sMsg = "" , sVarMsg = "" , lException = False , lExcInfo = False ):
532
+ def exception (
533
+ self , sMsg : str = "" , sVarMsg : str = "" , lException : bool = False , lExcInfo : bool = False
534
+ ) -> bool :
528
535
"""
529
536
Exception level
530
537
"""
@@ -534,15 +541,15 @@ def exception(self, sMsg="", sVarMsg="", lException=False, lExcInfo=False):
534
541
LogLevels .ERROR , sMsg , sVarMsg , exc_info = True , local_context = self .extra
535
542
)
536
543
537
- def fatal (self , sMsg , sVarMsg = "" ):
544
+ def fatal (self , sMsg : str , sVarMsg : str = "" ) -> bool :
538
545
"""
539
546
Fatal level
540
547
"""
541
548
return self .logger ._createLogRecord ( # pylint: disable=protected-access
542
549
LogLevels .FATAL , sMsg , sVarMsg , local_context = self .extra
543
550
)
544
551
545
- def getLocalSubLogger (self , subName ) :
552
+ def getLocalSubLogger (self , subName : str ) -> Logging . LocalSubLogger :
546
553
"""
547
554
Create a subLogger which is meant to have very short lifetime,
548
555
(e.g. when you want to add the jobID in the name)
0 commit comments