Skip to content

Commit 9140b0d

Browse files
author
evanroman
committed
Add logging from user created threads documentation
1 parent fde773e commit 9140b0d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

articles/azure-functions/functions-reference-python.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,28 @@ More logging methods are available that let you write to the console at differen
517517

518518
To learn more about logging, see [Monitor Azure Functions](functions-monitoring.md).
519519

520+
### Logging from created threads
521+
522+
To see logs coming from your created threads, include the [`context`](/python/api/azure-functions/azure.functions.context) argument in the function's signature. This argument contains an attribute `thread_local_storage` which stores a local `invocation_id`. This can be set to the function's current `invocation_id` to ensure the context is changed.
523+
524+
```python
525+
import azure.functions as func
526+
import logging
527+
import threading
528+
529+
530+
def main(req, context):
531+
logging.info('Python HTTP trigger function processed a request.')
532+
t = threading.Thread(target=log_function, args=(context,))
533+
t.start()
534+
535+
536+
def log_function(context):
537+
context.thread_local_storage.invocation_id = context.invocation_id
538+
logging.info('Logging from thread.')
539+
```
540+
541+
520542
### Log custom telemetry
521543

522544
By default, the Functions runtime collects logs and other telemetry data that are generated by your functions. This telemetry ends up as traces in Application Insights. Request and dependency telemetry for certain Azure services are also collected by default by [triggers and bindings](functions-triggers-bindings.md#supported-bindings).
@@ -850,6 +872,7 @@ The [`Context`](/python/api/azure-functions/azure.functions.context) class has t
850872
| `function_directory` | The directory in which the function is running. |
851873
| `function_name` | The name of the function. |
852874
| `invocation_id` | The ID of the current function invocation. |
875+
| `thread_local_storage` | Thread local storage. Contains a local `invocation_id` for [logging from created threads](#logging-from-created-threads). |
853876
| `trace_context` | The context for distributed tracing. For more information, see [`Trace Context`](https://www.w3.org/TR/trace-context/). |
854877
| `retry_context` | The context for retries to the function. For more information, see [`retry-policies`](./functions-bindings-errors.md#retry-policies). |
855878

0 commit comments

Comments
 (0)