Skip to content

Commit d75bb32

Browse files
authored
Document how to use cortex logging for structured logging (#1804)
1 parent 3a8afa2 commit d75bb32

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

docs/clusters/aws/logs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ fields @timestamp, log
3535
```
3636

3737
Please make sure to select the log group for your cluster and adjust the time range accordingly before running the queries.
38+
39+
## Structured logging
40+
41+
You can use Cortex's logger in your Python code to log in JSON, which will enrich your logs with Cortex's metadata, and enable you to add custom metadata to the logs. See the structured logging docs for [Realtime](../../workloads/realtime/predictors.md#structured-logging), [Batch](../../workloads/batch/predictors.md#structured-logging), and [Task](../../workloads/task/definitions.md#structured-logging) APIs.

docs/clusters/gcp/logs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ jsonPayload.labels.jobID="<INSERT JOB ID>"
2222
```
2323

2424
Please make sure to navigate to the project containing your cluster and adjust the time range accordingly before running queries.
25+
26+
## Structured logging
27+
28+
You can use Cortex's logger in your Python code to log in JSON, which will enrich your logs with Cortex's metadata, and enable you to add custom metadata to the logs. See the structured logging docs for [Realtime](../../workloads/realtime/predictors.md#structured-logging) and [Task](../../workloads/task/definitions.md#structured-logging) APIs.

docs/workloads/batch/predictors.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,24 @@ class ONNXPredictor:
207207
Cortex provides an `onnx_client` to your Predictor's constructor. `onnx_client` is an instance of [ONNXClient](https://github.com/cortexlabs/cortex/tree/master/pkg/cortex/serve/cortex_internal/lib/client/onnx.py) that manages an ONNX Runtime session to make predictions using your model. It should be saved as an instance variable in your Predictor, and your `predict()` function should call `onnx_client.predict()` to make an inference with your exported ONNX model. Preprocessing of the JSON payload and postprocessing of predictions can be implemented in your `predict()` function as well.
208208

209209
When multiple models are defined using the Predictor's `models` field, the `onnx_client.predict()` method expects a second argument `model_name` which must hold the name of the model that you want to use for inference (for example: `self.client.predict(model_input, "text-generator")`).
210+
211+
## Structured logging
212+
213+
You can use Cortex's logger in your predictor implemention to log in JSON. This will enrich your logs with Cortex's metadata, and you can add custom metadata to the logs by adding key value pairs to the `extra` key when using the logger. For example:
214+
215+
```python
216+
...
217+
from cortex_internal.lib.log import logger as cortex_logger
218+
219+
class PythonPredictor:
220+
def predict(self, payload, batch_id):
221+
cortex_logger.info("completed processing batch", extra={"batch_id": batch_id, "confidence": confidence})
222+
```
223+
224+
The dictionary passed in via the `extra` will be flattened by one level. e.g.
225+
226+
```text
227+
{"asctime": "2021-01-19 15:14:05,291", "levelname": "INFO", "message": "completed processing batch", "process": 235, "batch_id": "iuasyd8f7", "confidence": 0.97}
228+
```
229+
230+
To avoid overriding essential Cortex metadata, please refrain from specifying the following extra keys: `asctime`, `levelname`, `message`, `labels`, and `process`. Log lines greater than 5 MB in size will be ignored.

docs/workloads/realtime/predictors.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,24 @@ class PythonPredictor:
455455
```
456456

457457
Note that the autoscaling configuration (i.e. `target_replica_concurrency`) for the API that is making the request should be modified with the understanding that requests will still be considered "in-flight" with the first API as the request is being fulfilled in the second API (during which it will also be considered "in-flight" with the second API).
458+
459+
## Structured logging
460+
461+
You can use Cortex's logger in your predictor implemention to log in JSON. This will enrich your logs with Cortex's metadata, and you can add custom metadata to the logs by adding key value pairs to the `extra` key when using the logger. For example:
462+
463+
```python
464+
...
465+
from cortex_internal.lib.log import logger as cortex_logger
466+
467+
class PythonPredictor:
468+
def predict(self, payload):
469+
cortex_logger.info("received payload", extra={"payload": payload})
470+
```
471+
472+
The dictionary passed in via the `extra` will be flattened by one level. e.g.
473+
474+
```text
475+
{"asctime": "2021-01-19 15:14:05,291", "levelname": "INFO", "message": "received payload", "process": 235, "payload": "this movie is awesome"}
476+
```
477+
478+
To avoid overriding essential Cortex metadata, please refrain from specifying the following extra keys: `asctime`, `levelname`, `message`, `labels`, and `process`. Log lines greater than 5 MB in size will be ignored.

docs/workloads/task/definitions.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,25 @@ class Task:
5151
"""
5252
pass
5353
```
54+
55+
## Structured logging
56+
57+
You can use Cortex's logger in your predictor implemention to log in JSON. This will enrich your logs with Cortex's metadata, and you can add custom metadata to the logs by adding key value pairs to the `extra` key when using the logger. For example:
58+
59+
```python
60+
...
61+
from cortex_internal.lib.log import logger as cortex_logger
62+
63+
class Task:
64+
def __call__(self, config):
65+
...
66+
cortex_logger.info("completed validations", extra={"accuracy": accuracy})
67+
```
68+
69+
The dictionary passed in via the `extra` will be flattened by one level. e.g.
70+
71+
```text
72+
{"asctime": "2021-01-19 15:14:05,291", "levelname": "INFO", "message": "completed validations", "process": 235, "accuracy": 0.97}
73+
```
74+
75+
To avoid overriding essential Cortex metadata, please refrain from specifying the following extra keys: `asctime`, `levelname`, `message`, `labels`, and `process`. Log lines greater than 5 MB in size will be ignored.

0 commit comments

Comments
 (0)