Skip to content

Commit

Permalink
DOCS-3147: Use resource-level logging for Python modules (#3736)
Browse files Browse the repository at this point in the history
  • Loading branch information
JessamyT authored Dec 10, 2024
1 parent f2f8572 commit 24e799f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion docs/how-tos/create-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,28 @@ The example code shown above under [Write your new resource model definition](#w
{{< tabs name="Configure logging">}}
{{% tab name="Python"%}}
To enable your Python module to write log messages to the Viam app, add the following lines to your code:
To enable your Python module to write resource-level log messages to the Viam app, add the following lines to your code:
```python {class="line-numbers linkable-line-numbers"}
# Within some method, log information:
self.logger.debug("debug info")
self.logger.info("info")
self.logger.warn("warning info")
self.logger.error("error info")
self.logger.exception("error info", exc_info=True)
self.logger.critical("critical info")
```

Resource-level logs are recommended instead of global logs for modular resources, because they make it easier to determine which component or service an error is coming from.
Resource-level error logs appear in the **ERROR LOGS** section of each resource's configuration card in the app.

{{% alert title="Note" color="note" %}}
In order to see resource-level debug logs when using your modular resource, you'll either need to run `viam-server` with the `-debug` option or [configure your machine or individual resource to display debug logs](/architecture/viam-server/#logging).
{{% /alert %}}

{{< expand "Click to see global logging" >}}

If you need to publish to the global machine-level logs instead of using the recommended resource-level logging, you can follow this example:

```python {class="line-numbers linkable-line-numbers" data-line="2,5"}
# In your import block, import the logging package:
Expand All @@ -1137,6 +1158,8 @@ LOGGER.exception("error info", exc_info=True)
LOGGER.critical("critical info")
```

{{< /expand >}}

{{% /tab %}}
{{% tab name="Go"%}}

Expand Down

0 comments on commit 24e799f

Please sign in to comment.