scope
scope
#
The verbs, and to(), which decides where they go.
to(*sinks)opens a freshLogrouted to the sinks and makes its root the ambient parent for the block; several sinks is the tee. Outside everyto()block the verbs capture their call site, read the context variable and return an inert entry: nothing is recorded.info,warning,errorandprogressmake an entry beneath the ambient parent;withon the result makes it the parent for its block. The caller's code location is the entry's slot inside an iteratedprogressloop (the loop rule inLog);key=names a slot by hand.- A
copy_context()thread — apmapworker — inherits the ambient parent; a bare thread starts with none, though an entry it already holds still acceptsupdateandadvance.
current()
#
current_entry()
#
The innermost open entry in this context, if any.
error(message, *, key=None, kind='text', **fields)
#
Log message at error; see info.
Source code in capturegraph-lib/capturegraph/logging/scope.py
info(message, *, key=None, kind='text', **fields)
#
Log message at info; with on the result nests a block beneath it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
What happened. |
required |
key
|
str | None
|
A slot of your own, so a later call with the same key revises this entry. |
None
|
kind
|
Kind
|
What |
'text'
|
fields
|
JSONValue
|
Named values the line carries. |
{}
|
Source code in capturegraph-lib/capturegraph/logging/scope.py
label_of(function)
#
The label a count takes from function: its name.
progress(iterable=None, *, total=None, label=None, key=None)
#
A counted entry: iterate it, or hold it and advance it.
Iterating numbers the iterations, and inside the loop an entry logged from the same line each time revises itself instead of piling up.
for batch in cg.logging.progress(batches, label="Training"):
loss = train(batch)
cg.logging.info(f"loss {loss:.3f}", loss=loss)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
Iterable[T] | None
|
What to iterate; |
None
|
total
|
int | None
|
How many units there are; the iterable's length when it has one. |
None
|
label
|
str | None
|
What the count shows; |
None
|
key
|
str | None
|
A slot of your own; see |
None
|
Source code in capturegraph-lib/capturegraph/logging/scope.py
to(*sinks)
#
Route everything logged in the block to sinks.
with cg.logging.to(cg.logging.Terminal()) as log:
thumbnails = journal.sessions.photo.pmap(session_thumbnail)
print(len(log.entries()), "entries")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sinks
|
Sink
|
Where the entries go; none collects them in the returned log only. |
()
|
Yields:
| Type | Description |
|---|---|
Log
|
The log being written. |
Source code in capturegraph-lib/capturegraph/logging/scope.py
warning(message, *, key=None, kind='text', **fields)
#
Log message at warning; see info.