logxpy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- logxpy/__init__.py +126 -0
- logxpy/_action.py +958 -0
- logxpy/_async.py +186 -0
- logxpy/_base.py +80 -0
- logxpy/_compat.py +71 -0
- logxpy/_config.py +45 -0
- logxpy/_dest.py +88 -0
- logxpy/_errors.py +58 -0
- logxpy/_fmt.py +68 -0
- logxpy/_generators.py +136 -0
- logxpy/_mask.py +23 -0
- logxpy/_message.py +195 -0
- logxpy/_output.py +517 -0
- logxpy/_pool.py +93 -0
- logxpy/_traceback.py +126 -0
- logxpy/_types.py +71 -0
- logxpy/_util.py +56 -0
- logxpy/_validation.py +486 -0
- logxpy/_version.py +21 -0
- logxpy/cli.py +61 -0
- logxpy/dask.py +172 -0
- logxpy/decorators.py +268 -0
- logxpy/filter.py +124 -0
- logxpy/journald.py +88 -0
- logxpy/json.py +149 -0
- logxpy/loggerx.py +253 -0
- logxpy/logwriter.py +84 -0
- logxpy/parse.py +191 -0
- logxpy/prettyprint.py +173 -0
- logxpy/serializers.py +36 -0
- logxpy/stdlib.py +23 -0
- logxpy/tai64n.py +45 -0
- logxpy/testing.py +472 -0
- logxpy/tests/__init__.py +9 -0
- logxpy/tests/common.py +36 -0
- logxpy/tests/strategies.py +231 -0
- logxpy/tests/test_action.py +1751 -0
- logxpy/tests/test_api.py +86 -0
- logxpy/tests/test_async.py +67 -0
- logxpy/tests/test_compat.py +13 -0
- logxpy/tests/test_config.py +21 -0
- logxpy/tests/test_coroutines.py +105 -0
- logxpy/tests/test_dask.py +211 -0
- logxpy/tests/test_decorators.py +54 -0
- logxpy/tests/test_filter.py +122 -0
- logxpy/tests/test_fmt.py +42 -0
- logxpy/tests/test_generators.py +292 -0
- logxpy/tests/test_journald.py +246 -0
- logxpy/tests/test_json.py +208 -0
- logxpy/tests/test_loggerx.py +44 -0
- logxpy/tests/test_logwriter.py +262 -0
- logxpy/tests/test_message.py +334 -0
- logxpy/tests/test_output.py +921 -0
- logxpy/tests/test_parse.py +309 -0
- logxpy/tests/test_pool.py +55 -0
- logxpy/tests/test_prettyprint.py +303 -0
- logxpy/tests/test_pyinstaller.py +35 -0
- logxpy/tests/test_serializers.py +36 -0
- logxpy/tests/test_stdlib.py +73 -0
- logxpy/tests/test_tai64n.py +66 -0
- logxpy/tests/test_testing.py +1051 -0
- logxpy/tests/test_traceback.py +251 -0
- logxpy/tests/test_twisted.py +814 -0
- logxpy/tests/test_util.py +45 -0
- logxpy/tests/test_validation.py +989 -0
- logxpy/twisted.py +265 -0
- logxpy-0.1.0.dist-info/METADATA +100 -0
- logxpy-0.1.0.dist-info/RECORD +72 -0
- logxpy-0.1.0.dist-info/WHEEL +5 -0
- logxpy-0.1.0.dist-info/entry_points.txt +2 -0
- logxpy-0.1.0.dist-info/licenses/LICENSE +201 -0
- logxpy-0.1.0.dist-info/top_level.txt +1 -0
logxpy/_action.py
ADDED
|
@@ -0,0 +1,958 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Support for actions and tasks.
|
|
3
|
+
|
|
4
|
+
Actions have a beginning and an eventual end, and can be nested. Tasks are
|
|
5
|
+
top-level actions.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import threading
|
|
9
|
+
from uuid import uuid4
|
|
10
|
+
from contextlib import contextmanager
|
|
11
|
+
from functools import partial
|
|
12
|
+
from inspect import getcallargs
|
|
13
|
+
from contextvars import ContextVar
|
|
14
|
+
|
|
15
|
+
from pyrsistent import field, PClass, optional, pmap_field, pvector
|
|
16
|
+
from boltons.funcutils import wraps
|
|
17
|
+
|
|
18
|
+
from ._message import (
|
|
19
|
+
WrittenMessage,
|
|
20
|
+
EXCEPTION_FIELD,
|
|
21
|
+
REASON_FIELD,
|
|
22
|
+
TASK_UUID_FIELD,
|
|
23
|
+
MESSAGE_TYPE_FIELD,
|
|
24
|
+
)
|
|
25
|
+
from ._util import safeunicode
|
|
26
|
+
from ._errors import _error_extraction
|
|
27
|
+
|
|
28
|
+
ACTION_STATUS_FIELD = "action_status"
|
|
29
|
+
ACTION_TYPE_FIELD = "action_type"
|
|
30
|
+
|
|
31
|
+
STARTED_STATUS = "started"
|
|
32
|
+
SUCCEEDED_STATUS = "succeeded"
|
|
33
|
+
FAILED_STATUS = "failed"
|
|
34
|
+
|
|
35
|
+
VALID_STATUSES = (STARTED_STATUS, SUCCEEDED_STATUS, FAILED_STATUS)
|
|
36
|
+
|
|
37
|
+
_ACTION_CONTEXT = ContextVar("eliot.action")
|
|
38
|
+
|
|
39
|
+
from ._message import TIMESTAMP_FIELD, TASK_LEVEL_FIELD
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def current_action():
|
|
43
|
+
"""
|
|
44
|
+
@return: The current C{Action} in context, or C{None} if none were set.
|
|
45
|
+
"""
|
|
46
|
+
return _ACTION_CONTEXT.get(None)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class TaskLevel(object):
|
|
50
|
+
"""
|
|
51
|
+
The location of a message within the tree of actions of a task.
|
|
52
|
+
|
|
53
|
+
@ivar level: A pvector of integers. Each item indicates a child
|
|
54
|
+
relationship, and the value indicates message count. E.g. C{[2,
|
|
55
|
+
3]} indicates this is the third message within an action which is
|
|
56
|
+
the second item in the task.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
def __init__(self, level):
|
|
60
|
+
self._level = level
|
|
61
|
+
|
|
62
|
+
def as_list(self):
|
|
63
|
+
"""Return the current level.
|
|
64
|
+
|
|
65
|
+
@return: List of integers.
|
|
66
|
+
"""
|
|
67
|
+
return self._level[:]
|
|
68
|
+
|
|
69
|
+
# Backwards compatibility:
|
|
70
|
+
@property
|
|
71
|
+
def level(self):
|
|
72
|
+
return pvector(self._level)
|
|
73
|
+
|
|
74
|
+
def __lt__(self, other):
|
|
75
|
+
return self._level < other._level
|
|
76
|
+
|
|
77
|
+
def __le__(self, other):
|
|
78
|
+
return self._level <= other._level
|
|
79
|
+
|
|
80
|
+
def __gt__(self, other):
|
|
81
|
+
return self._level > other._level
|
|
82
|
+
|
|
83
|
+
def __ge__(self, other):
|
|
84
|
+
return self._level >= other._level
|
|
85
|
+
|
|
86
|
+
def __eq__(self, other):
|
|
87
|
+
if other.__class__ != TaskLevel:
|
|
88
|
+
return False
|
|
89
|
+
return self._level == other._level
|
|
90
|
+
|
|
91
|
+
def __ne__(self, other):
|
|
92
|
+
if other.__class__ != TaskLevel:
|
|
93
|
+
return True
|
|
94
|
+
return self._level != other._level
|
|
95
|
+
|
|
96
|
+
def __hash__(self):
|
|
97
|
+
return hash(tuple(self._level))
|
|
98
|
+
|
|
99
|
+
@classmethod
|
|
100
|
+
def fromString(cls, string):
|
|
101
|
+
"""
|
|
102
|
+
Convert a serialized Unicode string to a L{TaskLevel}.
|
|
103
|
+
|
|
104
|
+
@param string: Output of L{TaskLevel.toString}.
|
|
105
|
+
|
|
106
|
+
@return: L{TaskLevel} parsed from the string.
|
|
107
|
+
"""
|
|
108
|
+
return cls(level=[int(i) for i in string.split("/") if i])
|
|
109
|
+
|
|
110
|
+
def toString(self):
|
|
111
|
+
"""
|
|
112
|
+
Convert to a Unicode string, for serialization purposes.
|
|
113
|
+
|
|
114
|
+
@return: L{str} representation of the L{TaskLevel}.
|
|
115
|
+
"""
|
|
116
|
+
return "/" + "/".join(map(str, self._level))
|
|
117
|
+
|
|
118
|
+
def next_sibling(self):
|
|
119
|
+
"""
|
|
120
|
+
Return the next L{TaskLevel}, that is a task at the same level as this
|
|
121
|
+
one, but one after.
|
|
122
|
+
|
|
123
|
+
@return: L{TaskLevel} which follows this one.
|
|
124
|
+
"""
|
|
125
|
+
new_level = self._level[:]
|
|
126
|
+
new_level[-1] += 1
|
|
127
|
+
return TaskLevel(level=new_level)
|
|
128
|
+
|
|
129
|
+
def child(self):
|
|
130
|
+
"""
|
|
131
|
+
Return a child of this L{TaskLevel}.
|
|
132
|
+
|
|
133
|
+
@return: L{TaskLevel} which is the first child of this one.
|
|
134
|
+
"""
|
|
135
|
+
new_level = self._level[:]
|
|
136
|
+
new_level.append(1)
|
|
137
|
+
return TaskLevel(level=new_level)
|
|
138
|
+
|
|
139
|
+
def parent(self):
|
|
140
|
+
"""
|
|
141
|
+
Return the parent of this L{TaskLevel}, or C{None} if it doesn't have
|
|
142
|
+
one.
|
|
143
|
+
|
|
144
|
+
@return: L{TaskLevel} which is the parent of this one.
|
|
145
|
+
"""
|
|
146
|
+
if not self._level:
|
|
147
|
+
return None
|
|
148
|
+
return TaskLevel(level=self._level[:-1])
|
|
149
|
+
|
|
150
|
+
def is_sibling_of(self, task_level):
|
|
151
|
+
"""
|
|
152
|
+
Is this task a sibling of C{task_level}?
|
|
153
|
+
"""
|
|
154
|
+
return self.parent() == task_level.parent()
|
|
155
|
+
|
|
156
|
+
# PEP 8 compatibility:
|
|
157
|
+
from_string = fromString
|
|
158
|
+
to_string = toString
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
_TASK_ID_NOT_SUPPLIED = object()
|
|
162
|
+
|
|
163
|
+
import time
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class Action(object):
|
|
167
|
+
"""
|
|
168
|
+
Part of a nested heirarchy of ongoing actions.
|
|
169
|
+
|
|
170
|
+
An action has a start and an end; a message is logged for each.
|
|
171
|
+
|
|
172
|
+
Actions should only be used from a single thread, by implication the
|
|
173
|
+
thread where they were created.
|
|
174
|
+
|
|
175
|
+
@ivar _identification: Fields identifying this action.
|
|
176
|
+
|
|
177
|
+
@ivar _successFields: Fields to be included in successful finish message.
|
|
178
|
+
|
|
179
|
+
@ivar _finished: L{True} if the L{Action} has finished, otherwise L{False}.
|
|
180
|
+
"""
|
|
181
|
+
|
|
182
|
+
def __init__(self, logger, task_uuid, task_level, action_type, serializers=None):
|
|
183
|
+
"""
|
|
184
|
+
Initialize the L{Action} and log the start message.
|
|
185
|
+
|
|
186
|
+
You probably do not want to use this API directly: use L{start_action}
|
|
187
|
+
or L{startTask} instead.
|
|
188
|
+
|
|
189
|
+
@param logger: The L{eliot.ILogger} to which to write
|
|
190
|
+
messages.
|
|
191
|
+
|
|
192
|
+
@param task_uuid: The uuid of the top-level task, e.g. C{"123525"}.
|
|
193
|
+
|
|
194
|
+
@param task_level: The action's level in the task.
|
|
195
|
+
@type task_level: L{TaskLevel}
|
|
196
|
+
|
|
197
|
+
@param action_type: The type of the action,
|
|
198
|
+
e.g. C{"yourapp:subsystem:dosomething"}.
|
|
199
|
+
|
|
200
|
+
@param serializers: Either a L{eliot._validation._ActionSerializers}
|
|
201
|
+
instance or C{None}. In the latter case no validation or
|
|
202
|
+
serialization will be done for messages generated by the
|
|
203
|
+
L{Action}.
|
|
204
|
+
"""
|
|
205
|
+
self._successFields = {}
|
|
206
|
+
self._logger = _output._DEFAULT_LOGGER if (logger is None) else logger
|
|
207
|
+
self._task_level = task_level
|
|
208
|
+
self._last_child = None
|
|
209
|
+
self._identification = {
|
|
210
|
+
TASK_UUID_FIELD: task_uuid,
|
|
211
|
+
ACTION_TYPE_FIELD: action_type,
|
|
212
|
+
}
|
|
213
|
+
self._serializers = serializers
|
|
214
|
+
self._finished = False
|
|
215
|
+
|
|
216
|
+
@property
|
|
217
|
+
def task_uuid(self):
|
|
218
|
+
"""
|
|
219
|
+
@return str: the current action's task UUID.
|
|
220
|
+
"""
|
|
221
|
+
return self._identification[TASK_UUID_FIELD]
|
|
222
|
+
|
|
223
|
+
def serialize_task_id(self):
|
|
224
|
+
"""
|
|
225
|
+
Create a unique identifier for the current location within the task.
|
|
226
|
+
|
|
227
|
+
The format is C{b"<task_uuid>@<task_level>"}.
|
|
228
|
+
|
|
229
|
+
@return: L{bytes} encoding the current location within the task.
|
|
230
|
+
"""
|
|
231
|
+
return "{}@{}".format(
|
|
232
|
+
self._identification[TASK_UUID_FIELD], self._nextTaskLevel().toString()
|
|
233
|
+
).encode("ascii")
|
|
234
|
+
|
|
235
|
+
@classmethod
|
|
236
|
+
def continue_task(
|
|
237
|
+
cls,
|
|
238
|
+
logger=None,
|
|
239
|
+
task_id=_TASK_ID_NOT_SUPPLIED,
|
|
240
|
+
*,
|
|
241
|
+
action_type="eliot:remote_task",
|
|
242
|
+
_serializers=None,
|
|
243
|
+
**fields,
|
|
244
|
+
):
|
|
245
|
+
"""
|
|
246
|
+
Start a new action which is part of a serialized task.
|
|
247
|
+
|
|
248
|
+
@param logger: The L{eliot.ILogger} to which to write
|
|
249
|
+
messages, or C{None} if the default one should be used.
|
|
250
|
+
|
|
251
|
+
@param task_id: A serialized task identifier, the output of
|
|
252
|
+
L{Action.serialize_task_id}, either ASCII-encoded bytes or unicode
|
|
253
|
+
string. Required.
|
|
254
|
+
|
|
255
|
+
@param action_type: The type of this action,
|
|
256
|
+
e.g. C{"yourapp:subsystem:dosomething"}.
|
|
257
|
+
|
|
258
|
+
@param _serializers: Either a L{eliot._validation._ActionSerializers}
|
|
259
|
+
instance or C{None}. In the latter case no validation or serialization
|
|
260
|
+
will be done for messages generated by the L{Action}.
|
|
261
|
+
|
|
262
|
+
@param fields: Additional fields to add to the start message.
|
|
263
|
+
|
|
264
|
+
@return: The new L{Action} instance.
|
|
265
|
+
"""
|
|
266
|
+
if task_id is _TASK_ID_NOT_SUPPLIED:
|
|
267
|
+
raise RuntimeError("You must supply a task_id keyword argument.")
|
|
268
|
+
if isinstance(task_id, bytes):
|
|
269
|
+
task_id = task_id.decode("ascii")
|
|
270
|
+
uuid, task_level = task_id.split("@")
|
|
271
|
+
action = cls(
|
|
272
|
+
logger, uuid, TaskLevel.fromString(task_level), action_type, _serializers
|
|
273
|
+
)
|
|
274
|
+
action._start(fields)
|
|
275
|
+
return action
|
|
276
|
+
|
|
277
|
+
# Backwards-compat variants:
|
|
278
|
+
serializeTaskId = serialize_task_id
|
|
279
|
+
continueTask = continue_task
|
|
280
|
+
|
|
281
|
+
def _nextTaskLevel(self):
|
|
282
|
+
"""
|
|
283
|
+
Return the next C{task_level} for messages within this action.
|
|
284
|
+
|
|
285
|
+
Called whenever a message is logged within the context of an action.
|
|
286
|
+
|
|
287
|
+
@return: The message's C{task_level}.
|
|
288
|
+
"""
|
|
289
|
+
if not self._last_child:
|
|
290
|
+
self._last_child = self._task_level.child()
|
|
291
|
+
else:
|
|
292
|
+
self._last_child = self._last_child.next_sibling()
|
|
293
|
+
return self._last_child
|
|
294
|
+
|
|
295
|
+
def _start(self, fields):
|
|
296
|
+
"""
|
|
297
|
+
Log the start message.
|
|
298
|
+
|
|
299
|
+
The action identification fields, and any additional given fields,
|
|
300
|
+
will be logged.
|
|
301
|
+
|
|
302
|
+
In general you shouldn't call this yourself, instead using a C{with}
|
|
303
|
+
block or L{Action.finish}.
|
|
304
|
+
"""
|
|
305
|
+
fields[ACTION_STATUS_FIELD] = STARTED_STATUS
|
|
306
|
+
fields[TIMESTAMP_FIELD] = time.time()
|
|
307
|
+
fields.update(self._identification)
|
|
308
|
+
fields[TASK_LEVEL_FIELD] = self._nextTaskLevel().as_list()
|
|
309
|
+
if self._serializers is None:
|
|
310
|
+
serializer = None
|
|
311
|
+
else:
|
|
312
|
+
serializer = self._serializers.start
|
|
313
|
+
self._logger.write(fields, serializer)
|
|
314
|
+
|
|
315
|
+
def finish(self, exception=None):
|
|
316
|
+
"""
|
|
317
|
+
Log the finish message.
|
|
318
|
+
|
|
319
|
+
The action identification fields, and any additional given fields,
|
|
320
|
+
will be logged.
|
|
321
|
+
|
|
322
|
+
In general you shouldn't call this yourself, instead using a C{with}
|
|
323
|
+
block or L{Action.finish}.
|
|
324
|
+
|
|
325
|
+
@param exception: C{None}, in which case the fields added with
|
|
326
|
+
L{Action.addSuccessFields} are used. Or an L{Exception}, in
|
|
327
|
+
which case an C{"exception"} field is added with the given
|
|
328
|
+
L{Exception} type and C{"reason"} with its contents.
|
|
329
|
+
"""
|
|
330
|
+
if self._finished:
|
|
331
|
+
return
|
|
332
|
+
self._finished = True
|
|
333
|
+
serializer = None
|
|
334
|
+
if exception is None:
|
|
335
|
+
fields = self._successFields
|
|
336
|
+
fields[ACTION_STATUS_FIELD] = SUCCEEDED_STATUS
|
|
337
|
+
if self._serializers is not None:
|
|
338
|
+
serializer = self._serializers.success
|
|
339
|
+
else:
|
|
340
|
+
fields = _error_extraction.get_fields_for_exception(self._logger, exception)
|
|
341
|
+
fields[EXCEPTION_FIELD] = "%s.%s" % (
|
|
342
|
+
exception.__class__.__module__,
|
|
343
|
+
exception.__class__.__name__,
|
|
344
|
+
)
|
|
345
|
+
fields[REASON_FIELD] = safeunicode(exception)
|
|
346
|
+
fields[ACTION_STATUS_FIELD] = FAILED_STATUS
|
|
347
|
+
if self._serializers is not None:
|
|
348
|
+
serializer = self._serializers.failure
|
|
349
|
+
|
|
350
|
+
fields[TIMESTAMP_FIELD] = time.time()
|
|
351
|
+
fields.update(self._identification)
|
|
352
|
+
fields[TASK_LEVEL_FIELD] = self._nextTaskLevel().as_list()
|
|
353
|
+
self._logger.write(fields, serializer)
|
|
354
|
+
|
|
355
|
+
def child(self, logger, action_type, serializers=None):
|
|
356
|
+
"""
|
|
357
|
+
Create a child L{Action}.
|
|
358
|
+
|
|
359
|
+
Rather than calling this directly, you can use L{start_action} to
|
|
360
|
+
create child L{Action} using the execution context.
|
|
361
|
+
|
|
362
|
+
@param logger: The L{eliot.ILogger} to which to write
|
|
363
|
+
messages.
|
|
364
|
+
|
|
365
|
+
@param action_type: The type of this action,
|
|
366
|
+
e.g. C{"yourapp:subsystem:dosomething"}.
|
|
367
|
+
|
|
368
|
+
@param serializers: Either a L{eliot._validation._ActionSerializers}
|
|
369
|
+
instance or C{None}. In the latter case no validation or
|
|
370
|
+
serialization will be done for messages generated by the
|
|
371
|
+
L{Action}.
|
|
372
|
+
"""
|
|
373
|
+
newLevel = self._nextTaskLevel()
|
|
374
|
+
return self.__class__(
|
|
375
|
+
logger,
|
|
376
|
+
self._identification[TASK_UUID_FIELD],
|
|
377
|
+
newLevel,
|
|
378
|
+
action_type,
|
|
379
|
+
serializers,
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
def run(self, f, *args, **kwargs):
|
|
383
|
+
"""
|
|
384
|
+
Run the given function with this L{Action} as its execution context.
|
|
385
|
+
"""
|
|
386
|
+
parent = _ACTION_CONTEXT.set(self)
|
|
387
|
+
try:
|
|
388
|
+
return f(*args, **kwargs)
|
|
389
|
+
finally:
|
|
390
|
+
_ACTION_CONTEXT.reset(parent)
|
|
391
|
+
|
|
392
|
+
def addSuccessFields(self, **fields):
|
|
393
|
+
"""
|
|
394
|
+
Add fields to be included in the result message when the action
|
|
395
|
+
finishes successfully.
|
|
396
|
+
|
|
397
|
+
@param fields: Additional fields to add to the result message.
|
|
398
|
+
"""
|
|
399
|
+
self._successFields.update(fields)
|
|
400
|
+
|
|
401
|
+
# PEP 8 variant:
|
|
402
|
+
add_success_fields = addSuccessFields
|
|
403
|
+
|
|
404
|
+
@contextmanager
|
|
405
|
+
def context(self):
|
|
406
|
+
"""
|
|
407
|
+
Create a context manager that ensures code runs within action's context.
|
|
408
|
+
|
|
409
|
+
The action does NOT finish when the context is exited.
|
|
410
|
+
"""
|
|
411
|
+
parent = _ACTION_CONTEXT.set(self)
|
|
412
|
+
try:
|
|
413
|
+
yield self
|
|
414
|
+
finally:
|
|
415
|
+
_ACTION_CONTEXT.reset(parent)
|
|
416
|
+
|
|
417
|
+
# Python context manager implementation:
|
|
418
|
+
def __enter__(self):
|
|
419
|
+
"""
|
|
420
|
+
Push this action onto the execution context.
|
|
421
|
+
"""
|
|
422
|
+
self._parent_token = _ACTION_CONTEXT.set(self)
|
|
423
|
+
return self
|
|
424
|
+
|
|
425
|
+
def __exit__(self, type, exception, traceback):
|
|
426
|
+
"""
|
|
427
|
+
Pop this action off the execution context, log finish message.
|
|
428
|
+
"""
|
|
429
|
+
_ACTION_CONTEXT.reset(self._parent_token)
|
|
430
|
+
self._parent_token = None
|
|
431
|
+
self.finish(exception)
|
|
432
|
+
|
|
433
|
+
## Message logging
|
|
434
|
+
def log(self, message_type, **fields):
|
|
435
|
+
"""Log individual message."""
|
|
436
|
+
fields[TIMESTAMP_FIELD] = time.time()
|
|
437
|
+
fields[TASK_UUID_FIELD] = self._identification[TASK_UUID_FIELD]
|
|
438
|
+
fields[TASK_LEVEL_FIELD] = self._nextTaskLevel().as_list()
|
|
439
|
+
fields[MESSAGE_TYPE_FIELD] = message_type
|
|
440
|
+
# Loggers will hopefully go away...
|
|
441
|
+
logger = fields.pop("__eliot_logger__", self._logger)
|
|
442
|
+
logger.write(fields, fields.pop("__eliot_serializer__", None))
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
class WrongTask(Exception):
|
|
446
|
+
"""
|
|
447
|
+
Tried to add a message to an action, but the message was from another
|
|
448
|
+
task.
|
|
449
|
+
"""
|
|
450
|
+
|
|
451
|
+
def __init__(self, action, message):
|
|
452
|
+
Exception.__init__(
|
|
453
|
+
self,
|
|
454
|
+
"Tried to add {} to {}. Expected task_uuid = {}, got {}".format(
|
|
455
|
+
message, action, action.task_uuid, message.task_uuid
|
|
456
|
+
),
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
class WrongTaskLevel(Exception):
|
|
461
|
+
"""
|
|
462
|
+
Tried to add a message to an action, but the task level of the message
|
|
463
|
+
indicated that it was not a direct child.
|
|
464
|
+
"""
|
|
465
|
+
|
|
466
|
+
def __init__(self, action, message):
|
|
467
|
+
Exception.__init__(
|
|
468
|
+
self,
|
|
469
|
+
"Tried to add {} to {}, but {} is not a sibling of {}".format(
|
|
470
|
+
message, action, message.task_level, action.task_level
|
|
471
|
+
),
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
class WrongActionType(Exception):
|
|
476
|
+
"""
|
|
477
|
+
Tried to end a message with a different action_type than the beginning.
|
|
478
|
+
"""
|
|
479
|
+
|
|
480
|
+
def __init__(self, action, message):
|
|
481
|
+
error_msg = "Tried to end {} with {}. Expected action_type = {}, got {}"
|
|
482
|
+
Exception.__init__(
|
|
483
|
+
self,
|
|
484
|
+
error_msg.format(
|
|
485
|
+
action,
|
|
486
|
+
message,
|
|
487
|
+
action.action_type,
|
|
488
|
+
message.contents.get(ACTION_TYPE_FIELD, "<undefined>"),
|
|
489
|
+
),
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
class InvalidStatus(Exception):
|
|
494
|
+
"""
|
|
495
|
+
Tried to end a message with an invalid status.
|
|
496
|
+
"""
|
|
497
|
+
|
|
498
|
+
def __init__(self, action, message):
|
|
499
|
+
error_msg = "Tried to end {} with {}. Expected status {} or {}, got {}"
|
|
500
|
+
Exception.__init__(
|
|
501
|
+
self,
|
|
502
|
+
error_msg.format(
|
|
503
|
+
action,
|
|
504
|
+
message,
|
|
505
|
+
SUCCEEDED_STATUS,
|
|
506
|
+
FAILED_STATUS,
|
|
507
|
+
message.contents.get(ACTION_STATUS_FIELD, "<undefined>"),
|
|
508
|
+
),
|
|
509
|
+
)
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
class DuplicateChild(Exception):
|
|
513
|
+
"""
|
|
514
|
+
Tried to add a child to an action that already had a child at that task
|
|
515
|
+
level.
|
|
516
|
+
"""
|
|
517
|
+
|
|
518
|
+
def __init__(self, action, message):
|
|
519
|
+
Exception.__init__(
|
|
520
|
+
self,
|
|
521
|
+
"Tried to add {} to {}, but already had child at {}".format(
|
|
522
|
+
message, action, message.task_level
|
|
523
|
+
),
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
class InvalidStartMessage(Exception):
|
|
528
|
+
"""
|
|
529
|
+
Tried to start an action with an invalid message.
|
|
530
|
+
"""
|
|
531
|
+
|
|
532
|
+
def __init__(self, message, reason):
|
|
533
|
+
Exception.__init__(self, "Invalid start message {}: {}".format(message, reason))
|
|
534
|
+
|
|
535
|
+
@classmethod
|
|
536
|
+
def wrong_status(cls, message):
|
|
537
|
+
return cls(message, 'must have status "STARTED"')
|
|
538
|
+
|
|
539
|
+
@classmethod
|
|
540
|
+
def wrong_task_level(cls, message):
|
|
541
|
+
return cls(message, "first message must have task level ending in 1")
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
class WrittenAction(PClass):
|
|
545
|
+
"""
|
|
546
|
+
An Action that has been logged.
|
|
547
|
+
|
|
548
|
+
This class is intended to provide a definition within Eliot of what an
|
|
549
|
+
action actually is, and a means of constructing actions that are known to
|
|
550
|
+
be valid.
|
|
551
|
+
|
|
552
|
+
@ivar WrittenMessage start_message: A start message whose task UUID and
|
|
553
|
+
level match this action, or C{None} if it is not yet set on the
|
|
554
|
+
action.
|
|
555
|
+
@ivar WrittenMessage end_message: An end message hose task UUID and
|
|
556
|
+
level match this action. Can be C{None} if the action is
|
|
557
|
+
unfinished.
|
|
558
|
+
@ivar TaskLevel task_level: The action's task level, e.g. if start
|
|
559
|
+
message has level C{[2, 3, 1]} it will be
|
|
560
|
+
C{TaskLevel(level=[2, 3])}.
|
|
561
|
+
@ivar UUID task_uuid: The UUID of the task to which this action belongs.
|
|
562
|
+
@ivar _children: A L{pmap} from L{TaskLevel} to the L{WrittenAction} and
|
|
563
|
+
L{WrittenMessage} objects that make up this action.
|
|
564
|
+
"""
|
|
565
|
+
|
|
566
|
+
start_message = field(type=optional(WrittenMessage), mandatory=True, initial=None)
|
|
567
|
+
end_message = field(type=optional(WrittenMessage), mandatory=True, initial=None)
|
|
568
|
+
task_level = field(type=TaskLevel, mandatory=True)
|
|
569
|
+
task_uuid = field(type=str, mandatory=True, factory=str)
|
|
570
|
+
# Pyrsistent doesn't support pmap_field with recursive types.
|
|
571
|
+
_children = pmap_field(TaskLevel, object)
|
|
572
|
+
|
|
573
|
+
@classmethod
|
|
574
|
+
def from_messages(cls, start_message=None, children=pvector(), end_message=None):
|
|
575
|
+
"""
|
|
576
|
+
Create a C{WrittenAction} from C{WrittenMessage}s and other
|
|
577
|
+
C{WrittenAction}s.
|
|
578
|
+
|
|
579
|
+
@param WrittenMessage start_message: A message that has
|
|
580
|
+
C{ACTION_STATUS_FIELD}, C{ACTION_TYPE_FIELD}, and a C{task_level}
|
|
581
|
+
that ends in C{1}, or C{None} if unavailable.
|
|
582
|
+
@param children: An iterable of C{WrittenMessage} and C{WrittenAction}
|
|
583
|
+
@param WrittenMessage end_message: A message that has the same
|
|
584
|
+
C{action_type} as this action.
|
|
585
|
+
|
|
586
|
+
@raise WrongTask: If C{end_message} has a C{task_uuid} that differs
|
|
587
|
+
from C{start_message.task_uuid}.
|
|
588
|
+
@raise WrongTaskLevel: If any child message or C{end_message} has a
|
|
589
|
+
C{task_level} that means it is not a direct child.
|
|
590
|
+
@raise WrongActionType: If C{end_message} has an C{ACTION_TYPE_FIELD}
|
|
591
|
+
that differs from the C{ACTION_TYPE_FIELD} of C{start_message}.
|
|
592
|
+
@raise InvalidStatus: If C{end_message} doesn't have an
|
|
593
|
+
C{action_status}, or has one that is not C{SUCCEEDED_STATUS} or
|
|
594
|
+
C{FAILED_STATUS}.
|
|
595
|
+
@raise InvalidStartMessage: If C{start_message} does not have a
|
|
596
|
+
C{ACTION_STATUS_FIELD} of C{STARTED_STATUS}, or if it has a
|
|
597
|
+
C{task_level} indicating that it is not the first message of an
|
|
598
|
+
action.
|
|
599
|
+
|
|
600
|
+
@return: A new C{WrittenAction}.
|
|
601
|
+
"""
|
|
602
|
+
actual_message = [
|
|
603
|
+
message
|
|
604
|
+
for message in [start_message, end_message] + list(children)
|
|
605
|
+
if message
|
|
606
|
+
][0]
|
|
607
|
+
action = cls(
|
|
608
|
+
task_level=actual_message.task_level.parent(),
|
|
609
|
+
task_uuid=actual_message.task_uuid,
|
|
610
|
+
)
|
|
611
|
+
if start_message:
|
|
612
|
+
action = action._start(start_message)
|
|
613
|
+
for child in children:
|
|
614
|
+
if action._children.get(child.task_level, child) != child:
|
|
615
|
+
raise DuplicateChild(action, child)
|
|
616
|
+
action = action._add_child(child)
|
|
617
|
+
if end_message:
|
|
618
|
+
action = action._end(end_message)
|
|
619
|
+
return action
|
|
620
|
+
|
|
621
|
+
@property
|
|
622
|
+
def action_type(self):
|
|
623
|
+
"""
|
|
624
|
+
The type of this action, e.g. C{"yourapp:subsystem:dosomething"}.
|
|
625
|
+
"""
|
|
626
|
+
if self.start_message:
|
|
627
|
+
return self.start_message.contents[ACTION_TYPE_FIELD]
|
|
628
|
+
elif self.end_message:
|
|
629
|
+
return self.end_message.contents[ACTION_TYPE_FIELD]
|
|
630
|
+
else:
|
|
631
|
+
return None
|
|
632
|
+
|
|
633
|
+
@property
|
|
634
|
+
def status(self):
|
|
635
|
+
"""
|
|
636
|
+
One of C{STARTED_STATUS}, C{SUCCEEDED_STATUS}, C{FAILED_STATUS} or
|
|
637
|
+
C{None}.
|
|
638
|
+
"""
|
|
639
|
+
message = self.end_message if self.end_message else self.start_message
|
|
640
|
+
if message:
|
|
641
|
+
return message.contents[ACTION_STATUS_FIELD]
|
|
642
|
+
else:
|
|
643
|
+
return None
|
|
644
|
+
|
|
645
|
+
@property
|
|
646
|
+
def start_time(self):
|
|
647
|
+
"""
|
|
648
|
+
The Unix timestamp of when the action started, or C{None} if there has
|
|
649
|
+
been no start message added so far.
|
|
650
|
+
"""
|
|
651
|
+
if self.start_message:
|
|
652
|
+
return self.start_message.timestamp
|
|
653
|
+
|
|
654
|
+
@property
|
|
655
|
+
def end_time(self):
|
|
656
|
+
"""
|
|
657
|
+
The Unix timestamp of when the action ended, or C{None} if there has been
|
|
658
|
+
no end message.
|
|
659
|
+
"""
|
|
660
|
+
if self.end_message:
|
|
661
|
+
return self.end_message.timestamp
|
|
662
|
+
|
|
663
|
+
@property
|
|
664
|
+
def exception(self):
|
|
665
|
+
"""
|
|
666
|
+
If the action failed, the name of the exception that was raised to cause
|
|
667
|
+
it to fail. If the action succeeded, or hasn't finished yet, then
|
|
668
|
+
C{None}.
|
|
669
|
+
"""
|
|
670
|
+
if self.end_message:
|
|
671
|
+
return self.end_message.contents.get(EXCEPTION_FIELD, None)
|
|
672
|
+
|
|
673
|
+
@property
|
|
674
|
+
def reason(self):
|
|
675
|
+
"""
|
|
676
|
+
The reason the action failed. If the action succeeded, or hasn't finished
|
|
677
|
+
yet, then C{None}.
|
|
678
|
+
"""
|
|
679
|
+
if self.end_message:
|
|
680
|
+
return self.end_message.contents.get(REASON_FIELD, None)
|
|
681
|
+
|
|
682
|
+
@property
|
|
683
|
+
def children(self):
|
|
684
|
+
"""
|
|
685
|
+
The list of child messages and actions sorted by task level, excluding the
|
|
686
|
+
start and end messages.
|
|
687
|
+
"""
|
|
688
|
+
return pvector(sorted(self._children.values(), key=lambda m: m.task_level))
|
|
689
|
+
|
|
690
|
+
def _validate_message(self, message):
|
|
691
|
+
"""
|
|
692
|
+
Is C{message} a valid direct child of this action?
|
|
693
|
+
|
|
694
|
+
@param message: Either a C{WrittenAction} or a C{WrittenMessage}.
|
|
695
|
+
|
|
696
|
+
@raise WrongTask: If C{message} has a C{task_uuid} that differs from the
|
|
697
|
+
action's C{task_uuid}.
|
|
698
|
+
@raise WrongTaskLevel: If C{message} has a C{task_level} that means
|
|
699
|
+
it's not a direct child.
|
|
700
|
+
"""
|
|
701
|
+
if message.task_uuid != self.task_uuid:
|
|
702
|
+
raise WrongTask(self, message)
|
|
703
|
+
if not message.task_level.parent() == self.task_level:
|
|
704
|
+
raise WrongTaskLevel(self, message)
|
|
705
|
+
|
|
706
|
+
def _add_child(self, message):
|
|
707
|
+
"""
|
|
708
|
+
Return a new action with C{message} added as a child.
|
|
709
|
+
|
|
710
|
+
Assumes C{message} is not an end message.
|
|
711
|
+
|
|
712
|
+
@param message: Either a C{WrittenAction} or a C{WrittenMessage}.
|
|
713
|
+
|
|
714
|
+
@raise WrongTask: If C{message} has a C{task_uuid} that differs from the
|
|
715
|
+
action's C{task_uuid}.
|
|
716
|
+
@raise WrongTaskLevel: If C{message} has a C{task_level} that means
|
|
717
|
+
it's not a direct child.
|
|
718
|
+
|
|
719
|
+
@return: A new C{WrittenAction}.
|
|
720
|
+
"""
|
|
721
|
+
self._validate_message(message)
|
|
722
|
+
level = message.task_level
|
|
723
|
+
return self.transform(("_children", level), message)
|
|
724
|
+
|
|
725
|
+
def _start(self, start_message):
|
|
726
|
+
"""
|
|
727
|
+
Start this action given its start message.
|
|
728
|
+
|
|
729
|
+
@param WrittenMessage start_message: A start message that has the
|
|
730
|
+
same level as this action.
|
|
731
|
+
|
|
732
|
+
@raise InvalidStartMessage: If C{start_message} does not have a
|
|
733
|
+
C{ACTION_STATUS_FIELD} of C{STARTED_STATUS}, or if it has a
|
|
734
|
+
C{task_level} indicating that it is not the first message of an
|
|
735
|
+
action.
|
|
736
|
+
"""
|
|
737
|
+
if start_message.contents.get(ACTION_STATUS_FIELD, None) != STARTED_STATUS:
|
|
738
|
+
raise InvalidStartMessage.wrong_status(start_message)
|
|
739
|
+
if start_message.task_level.level[-1] != 1:
|
|
740
|
+
raise InvalidStartMessage.wrong_task_level(start_message)
|
|
741
|
+
return self.set(start_message=start_message)
|
|
742
|
+
|
|
743
|
+
def _end(self, end_message):
|
|
744
|
+
"""
|
|
745
|
+
End this action with C{end_message}.
|
|
746
|
+
|
|
747
|
+
Assumes that the action has not already been ended.
|
|
748
|
+
|
|
749
|
+
@param WrittenMessage end_message: An end message that has the
|
|
750
|
+
same level as this action.
|
|
751
|
+
|
|
752
|
+
@raise WrongTask: If C{end_message} has a C{task_uuid} that differs
|
|
753
|
+
from the action's C{task_uuid}.
|
|
754
|
+
@raise WrongTaskLevel: If C{end_message} has a C{task_level} that means
|
|
755
|
+
it's not a direct child.
|
|
756
|
+
@raise InvalidStatus: If C{end_message} doesn't have an
|
|
757
|
+
C{action_status}, or has one that is not C{SUCCEEDED_STATUS} or
|
|
758
|
+
C{FAILED_STATUS}.
|
|
759
|
+
|
|
760
|
+
@return: A new, completed C{WrittenAction}.
|
|
761
|
+
"""
|
|
762
|
+
action_type = end_message.contents.get(ACTION_TYPE_FIELD, None)
|
|
763
|
+
if self.action_type not in (None, action_type):
|
|
764
|
+
raise WrongActionType(self, end_message)
|
|
765
|
+
self._validate_message(end_message)
|
|
766
|
+
status = end_message.contents.get(ACTION_STATUS_FIELD, None)
|
|
767
|
+
if status not in (FAILED_STATUS, SUCCEEDED_STATUS):
|
|
768
|
+
raise InvalidStatus(self, end_message)
|
|
769
|
+
return self.set(end_message=end_message)
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
def start_action(logger=None, action_type="", _serializers=None, **fields):
|
|
773
|
+
"""
|
|
774
|
+
Create a child L{Action}, figuring out the parent L{Action} from execution
|
|
775
|
+
context, and log the start message.
|
|
776
|
+
|
|
777
|
+
You can use the result as a Python context manager, or use the
|
|
778
|
+
L{Action.finish} API to explicitly finish it.
|
|
779
|
+
|
|
780
|
+
with start_action(logger, "yourapp:subsystem:dosomething",
|
|
781
|
+
entry=x) as action:
|
|
782
|
+
do(x)
|
|
783
|
+
result = something(x * 2)
|
|
784
|
+
action.addSuccessFields(result=result)
|
|
785
|
+
|
|
786
|
+
Or alternatively:
|
|
787
|
+
|
|
788
|
+
action = start_action(logger, "yourapp:subsystem:dosomething",
|
|
789
|
+
entry=x)
|
|
790
|
+
with action.context():
|
|
791
|
+
do(x)
|
|
792
|
+
result = something(x * 2)
|
|
793
|
+
action.addSuccessFields(result=result)
|
|
794
|
+
action.finish()
|
|
795
|
+
|
|
796
|
+
@param logger: The L{eliot.ILogger} to which to write messages, or
|
|
797
|
+
C{None} to use the default one.
|
|
798
|
+
|
|
799
|
+
@param action_type: The type of this action,
|
|
800
|
+
e.g. C{"yourapp:subsystem:dosomething"}.
|
|
801
|
+
|
|
802
|
+
@param _serializers: Either a L{eliot._validation._ActionSerializers}
|
|
803
|
+
instance or C{None}. In the latter case no validation or serialization
|
|
804
|
+
will be done for messages generated by the L{Action}.
|
|
805
|
+
|
|
806
|
+
@param fields: Additional fields to add to the start message.
|
|
807
|
+
|
|
808
|
+
@return: A new L{Action}.
|
|
809
|
+
"""
|
|
810
|
+
parent = current_action()
|
|
811
|
+
if parent is None:
|
|
812
|
+
return startTask(logger, action_type, _serializers, **fields)
|
|
813
|
+
else:
|
|
814
|
+
action = parent.child(logger, action_type, _serializers)
|
|
815
|
+
action._start(fields)
|
|
816
|
+
return action
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
def startTask(logger=None, action_type="", _serializers=None, **fields):
|
|
820
|
+
"""
|
|
821
|
+
Like L{action}, but creates a new top-level L{Action} with no parent.
|
|
822
|
+
|
|
823
|
+
@param logger: The L{eliot.ILogger} to which to write messages, or
|
|
824
|
+
C{None} to use the default one.
|
|
825
|
+
|
|
826
|
+
@param action_type: The type of this action,
|
|
827
|
+
e.g. C{"yourapp:subsystem:dosomething"}.
|
|
828
|
+
|
|
829
|
+
@param _serializers: Either a L{eliot._validation._ActionSerializers}
|
|
830
|
+
instance or C{None}. In the latter case no validation or serialization
|
|
831
|
+
will be done for messages generated by the L{Action}.
|
|
832
|
+
|
|
833
|
+
@param fields: Additional fields to add to the start message.
|
|
834
|
+
|
|
835
|
+
@return: A new L{Action}.
|
|
836
|
+
"""
|
|
837
|
+
action = Action(
|
|
838
|
+
logger, str(uuid4()), TaskLevel(level=[]), action_type, _serializers
|
|
839
|
+
)
|
|
840
|
+
action._start(fields)
|
|
841
|
+
return action
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
class TooManyCalls(Exception):
|
|
845
|
+
"""
|
|
846
|
+
The callable was called more than once.
|
|
847
|
+
|
|
848
|
+
This typically indicates a coding bug: the result of
|
|
849
|
+
C{preserve_context} should only be called once, and
|
|
850
|
+
C{preserve_context} should therefore be called each time you want to
|
|
851
|
+
pass the callable to a thread.
|
|
852
|
+
"""
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
def preserve_context(f):
|
|
856
|
+
"""
|
|
857
|
+
Package up the given function with the current Eliot context, and then
|
|
858
|
+
restore context and call given function when the resulting callable is
|
|
859
|
+
run. This allows continuing the action context within a different thread.
|
|
860
|
+
|
|
861
|
+
The result should only be used once, since it relies on
|
|
862
|
+
L{Action.serialize_task_id} whose results should only be deserialized
|
|
863
|
+
once.
|
|
864
|
+
|
|
865
|
+
@param f: A callable.
|
|
866
|
+
|
|
867
|
+
@return: One-time use callable that calls given function in context of
|
|
868
|
+
a child of current Eliot action.
|
|
869
|
+
"""
|
|
870
|
+
action = current_action()
|
|
871
|
+
if action is None:
|
|
872
|
+
return f
|
|
873
|
+
task_id = action.serialize_task_id()
|
|
874
|
+
called = threading.Lock()
|
|
875
|
+
|
|
876
|
+
def restore_eliot_context(*args, **kwargs):
|
|
877
|
+
# Make sure the function has not already been called:
|
|
878
|
+
if not called.acquire(False):
|
|
879
|
+
raise TooManyCalls(f)
|
|
880
|
+
|
|
881
|
+
with Action.continue_task(task_id=task_id):
|
|
882
|
+
return f(*args, **kwargs)
|
|
883
|
+
|
|
884
|
+
return restore_eliot_context
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
def log_call(
|
|
888
|
+
wrapped_function=None, action_type=None, include_args=None, include_result=True
|
|
889
|
+
):
|
|
890
|
+
"""Decorator/decorator factory that logs inputs and the return result.
|
|
891
|
+
|
|
892
|
+
If used with inputs (i.e. as a decorator factory), it accepts the following
|
|
893
|
+
parameters:
|
|
894
|
+
|
|
895
|
+
@param action_type: The action type to use. If not given the function name
|
|
896
|
+
will be used.
|
|
897
|
+
@param include_args: If given, should be a list of strings, the arguments to log.
|
|
898
|
+
@param include_result: True by default. If False, the return result isn't logged.
|
|
899
|
+
"""
|
|
900
|
+
if wrapped_function is None:
|
|
901
|
+
return partial(
|
|
902
|
+
log_call,
|
|
903
|
+
action_type=action_type,
|
|
904
|
+
include_args=include_args,
|
|
905
|
+
include_result=include_result,
|
|
906
|
+
)
|
|
907
|
+
|
|
908
|
+
if action_type is None:
|
|
909
|
+
action_type = "{}.{}".format(
|
|
910
|
+
wrapped_function.__module__, wrapped_function.__qualname__
|
|
911
|
+
)
|
|
912
|
+
|
|
913
|
+
if include_args is not None:
|
|
914
|
+
from inspect import signature
|
|
915
|
+
|
|
916
|
+
sig = signature(wrapped_function)
|
|
917
|
+
if set(include_args) - set(sig.parameters):
|
|
918
|
+
raise ValueError(
|
|
919
|
+
(
|
|
920
|
+
"include_args ({}) lists arguments not in the " "wrapped function"
|
|
921
|
+
).format(include_args)
|
|
922
|
+
)
|
|
923
|
+
|
|
924
|
+
@wraps(wrapped_function)
|
|
925
|
+
def logging_wrapper(*args, **kwargs):
|
|
926
|
+
callargs = getcallargs(wrapped_function, *args, **kwargs)
|
|
927
|
+
|
|
928
|
+
# Remove self is it's included:
|
|
929
|
+
if "self" in callargs:
|
|
930
|
+
callargs.pop("self")
|
|
931
|
+
|
|
932
|
+
# Filter arguments to log, if necessary:
|
|
933
|
+
if include_args is not None:
|
|
934
|
+
callargs = {k: callargs[k] for k in include_args}
|
|
935
|
+
|
|
936
|
+
with start_action(action_type=action_type, **callargs) as ctx:
|
|
937
|
+
result = wrapped_function(*args, **kwargs)
|
|
938
|
+
if include_result:
|
|
939
|
+
ctx.add_success_fields(result=result)
|
|
940
|
+
return result
|
|
941
|
+
|
|
942
|
+
return logging_wrapper
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
def log_message(message_type, **fields):
|
|
946
|
+
"""Log a message in the context of the current action.
|
|
947
|
+
|
|
948
|
+
If there is no current action, a new UUID will be generated.
|
|
949
|
+
"""
|
|
950
|
+
action = current_action()
|
|
951
|
+
if action is None:
|
|
952
|
+
# Loggers will hopefully go away...
|
|
953
|
+
logger = fields.pop("__eliot_logger__", None)
|
|
954
|
+
action = Action(logger, str(uuid4()), TaskLevel(level=[]), "")
|
|
955
|
+
action.log(message_type, **fields)
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
from . import _output
|