1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
# -*- test-case-name: twisted.logger.test.test_logger -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Logger class.
"""
from __future__ import annotations
from time import time
from types import TracebackType
from typing import Any, Callable, ContextManager, Optional, Protocol, cast
from twisted.python.compat import currentframe
from twisted.python.failure import Failure
from ._interfaces import ILogObserver, LogTrace
from ._levels import InvalidLogLevelError, LogLevel
class Operation(Protocol):
"""
An L{Operation} represents the success (or lack thereof) of code performed
within the body of the L{Logger.failureHandler} context manager.
"""
@property
def succeeded(self) -> bool:
"""
Did the operation succeed? C{True} iff the code completed without
raising an exception; C{False} while the code is running and C{False}
if it raises an exception.
"""
@property
def failure(self) -> Failure | None:
"""
Did the operation raise an exception? If so, this L{Failure} is that
exception.
"""
@property
def failed(self) -> bool:
"""
Did the operation fail? C{True} iff the code raised an exception;
C{False} while the code is running and C{False} if it completed without
error.
"""
class _FailCtxMgr:
succeeded: bool = False
failure: Failure | None = None
def __init__(self, fail: Callable[[Failure], None]) -> None:
self._fail = fail
@property
def failed(self) -> bool:
return self.failure is not None
def __enter__(self) -> Operation:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
/,
) -> bool:
if exc_type is not None:
failure = Failure()
self.failure = failure
self._fail(failure)
else:
self.succeeded = True
return True
class _FastFailCtxMgr:
def __init__(self, fail: Callable[[Failure], None]) -> None:
self._fail = fail
def __enter__(self) -> None:
pass
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
/,
) -> bool:
if exc_type is not None:
failure = Failure()
self.failure = failure
self._fail(failure)
return True
class Logger:
"""
A L{Logger} emits log messages to an observer. You should instantiate it
as a class or module attribute, as documented in L{this module's
documentation <twisted.logger>}.
@ivar namespace: the namespace for this logger
@ivar source: The object which is emitting events via this logger
@ivar observer: The observer that this logger will send events to.
"""
@staticmethod
def _namespaceFromCallingContext() -> str:
"""
Derive a namespace from the module containing the caller's caller.
@return: the fully qualified python name of a module.
"""
try:
return cast(str, currentframe(2).f_globals["__name__"])
except KeyError:
return "<unknown>"
def __init__(
self,
namespace: Optional[str] = None,
source: Optional[object] = None,
observer: Optional["ILogObserver"] = None,
) -> None:
"""
@param namespace: The namespace for this logger. Uses a dotted
notation, as used by python modules. If not L{None}, then the name
of the module of the caller is used.
@param source: The object which is emitting events via this
logger; this is automatically set on instances of a class
if this L{Logger} is an attribute of that class.
@param observer: The observer that this logger will send events to.
If L{None}, use the L{global log publisher <globalLogPublisher>}.
"""
if namespace is None:
namespace = self._namespaceFromCallingContext()
self.namespace = namespace
self.source = source
if observer is None:
from ._global import globalLogPublisher
self.observer: ILogObserver = globalLogPublisher
else:
self.observer = observer
def __get__(self, instance: object, owner: Optional[type] = None) -> "Logger":
"""
When used as a descriptor, i.e.::
# File: athing.py
class Something:
log = Logger()
def hello(self):
self.log.info("Hello")
a L{Logger}'s namespace will be set to the name of the class it is
declared on. In the above example, the namespace would be
C{athing.Something}.
Additionally, its source will be set to the actual object referring to
the L{Logger}. In the above example, C{Something.log.source} would be
C{Something}, and C{Something().log.source} would be an instance of
C{Something}.
"""
assert owner is not None
if instance is None:
source: Any = owner
else:
source = instance
return self.__class__(
".".join([owner.__module__, owner.__name__]),
source,
observer=self.observer,
)
def __repr__(self) -> str:
return f"<{self.__class__.__name__} {self.namespace!r}>"
def emit(
self, level: LogLevel, format: Optional[str] = None, **kwargs: object
) -> None:
"""
Emit a log event to all log observers at the given level.
@param level: a L{LogLevel}
@param format: a message format using new-style (PEP 3101)
formatting. The logging event (which is a L{dict}) is
used to render this format string.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
"""
if level not in LogLevel.iterconstants():
self.failure(
"Got invalid log level {invalidLevel!r} in {logger}.emit().",
Failure(InvalidLogLevelError(level)),
invalidLevel=level,
logger=self,
)
return
event = kwargs
event.update(
log_logger=self,
log_level=level,
log_namespace=self.namespace,
log_source=self.source,
log_format=format,
log_time=time(),
)
if "log_trace" in event:
cast(LogTrace, event["log_trace"]).append((self, self.observer))
self.observer(event)
def failure(
self,
format: str,
failure: Optional[Failure] = None,
level: LogLevel = LogLevel.critical,
**kwargs: object,
) -> None:
"""
Log a failure and emit a traceback.
For example::
try:
frob(knob)
except Exception:
log.failure("While frobbing {knob}", knob=knob)
or::
d = deferredFrob(knob)
d.addErrback(lambda f: log.failure("While frobbing {knob}",
f, knob=knob))
This method is meant to capture unexpected exceptions in code; an
exception that is caught and handled somehow should be logged, if
appropriate, via L{Logger.error} instead. If some unknown exception
occurs and your code doesn't know how to handle it, as in the above
example, then this method provides a means to describe the failure.
This is done at L{LogLevel.critical} by default, since no corrective
guidance can be offered to an user/administrator, and the impact of the
condition is unknown.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param failure: a L{Failure} to log. If L{None}, a L{Failure} is
created from the exception in flight.
@param level: a L{LogLevel} to use.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
@see: L{Logger.failureHandler}
@see: L{Logger.failuresHandled}
"""
if failure is None:
failure = Failure()
self.emit(level, format, log_failure=failure, **kwargs)
def debug(self, format: Optional[str] = None, **kwargs: object) -> None:
"""
Emit a log event at log level L{LogLevel.debug}.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
"""
self.emit(LogLevel.debug, format, **kwargs)
def info(self, format: Optional[str] = None, **kwargs: object) -> None:
"""
Emit a log event at log level L{LogLevel.info}.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
"""
self.emit(LogLevel.info, format, **kwargs)
def warn(self, format: Optional[str] = None, **kwargs: object) -> None:
"""
Emit a log event at log level L{LogLevel.warn}.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
"""
self.emit(LogLevel.warn, format, **kwargs)
def error(self, format: Optional[str] = None, **kwargs: object) -> None:
"""
Emit a log event at log level L{LogLevel.error}.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
"""
self.emit(LogLevel.error, format, **kwargs)
def critical(self, format: Optional[str] = None, **kwargs: object) -> None:
"""
Emit a log event at log level L{LogLevel.critical}.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param kwargs: additional key/value pairs to include in the event.
Note that values which are later mutated may result in
non-deterministic behavior from observers that schedule work for
later execution.
"""
self.emit(LogLevel.critical, format, **kwargs)
def failuresHandled(
self, format: str, level: LogLevel = LogLevel.critical, **kwargs: object
) -> ContextManager[Operation]:
"""
Run some application code, logging a failure and emitting a traceback
in the event that any of it fails, but continuing on. For example::
log = Logger(...)
def frameworkCode() -> None:
with log.failuresHandled("While frobbing {knob}:", knob=knob) as op:
frob(knob)
if op.succeeded:
log.info("frobbed {knob} successfully", knob=knob)
This method is meant to capture unexpected exceptions from application
code; an exception that is caught and handled somehow should be logged,
if appropriate, via L{Logger.error} instead. If some unknown exception
occurs and your code doesn't know how to handle it, as in the above
example, then this method provides a means to describe the failure.
This is done at L{LogLevel.critical} by default, since no corrective
guidance can be offered to an user/administrator, and the impact of the
condition is unknown.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param level: a L{LogLevel} to use.
@param kwargs: additional key/value pairs to include in the event, if
it is emitted. Note that values which are later mutated may result
in non-deterministic behavior from observers that schedule work for
later execution.
@see: L{Logger.failure}
@see: L{Logger.failureHandler}
@return: A context manager which yields an L{Operation} which will have
either its C{succeeded} or C{failed} attribute set to C{True} upon
completion of the code within the code within the C{with} block.
"""
return _FailCtxMgr(lambda f: self.failure(format, f, level, **kwargs))
def failureHandler(
self,
staticMessage: str,
level: LogLevel = LogLevel.critical,
) -> ContextManager[None]:
"""
For performance-sensitive frameworks that needs to handle potential
failures from frequently-called application code, and do not need to
include detailed structured information about the failure nor inspect
the result of the operation, this method returns a context manager that
will log exceptions and continue, that can be shared across multiple
invocations. It should be instantiated at module scope to avoid
additional object creations.
For example::
log = Logger(...)
ignoringFrobErrors = log.failureHandler("while frobbing:")
def hotLoop() -> None:
with ignoringFrobErrors:
frob()
This method is meant to capture unexpected exceptions from application
code; an exception that is caught and handled somehow should be logged,
if appropriate, via L{Logger.error} instead. If some unknown exception
occurs and your code doesn't know how to handle it, as in the above
example, then this method provides a means to describe the failure in
nerd-speak. This is done at L{LogLevel.critical} by default, since no
corrective guidance can be offered to an user/administrator, and the
impact of the condition is unknown.
@param format: a message format using new-style (PEP 3101) formatting.
The logging event (which is a L{dict}) is used to render this
format string.
@param level: a L{LogLevel} to use.
@see: L{Logger.failure}
@return: A context manager which does not return a value, but will
always exit from exceptions.
"""
return _FastFailCtxMgr(lambda f: self.failure(staticMessage, f, level))
_log = Logger()
def _loggerFor(obj: object) -> Logger:
"""
Get a L{Logger} instance attached to the given class.
"""
return _log.__get__(obj, obj.__class__)
|