From 24e799fe32ffea04fa879dd4532f2101461d3495 Mon Sep 17 00:00:00 2001 From: JessamyT <75634662+JessamyT@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:53:53 -0800 Subject: [PATCH] DOCS-3147: Use resource-level logging for Python modules (#3736) --- docs/how-tos/create-module.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/how-tos/create-module.md b/docs/how-tos/create-module.md index da7ae33e69..fb3311a22c 100644 --- a/docs/how-tos/create-module.md +++ b/docs/how-tos/create-module.md @@ -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: @@ -1137,6 +1158,8 @@ LOGGER.exception("error info", exc_info=True) LOGGER.critical("critical info") ``` +{{< /expand >}} + {{% /tab %}} {{% tab name="Go"%}}