|
| 1 | +--- |
| 2 | +title: "Module logging" |
| 3 | +linkTitle: "Module logging" |
| 4 | +weight: 45 |
| 5 | +layout: "docs" |
| 6 | +type: "docs" |
| 7 | +description: "Use logging with modules" |
| 8 | +--- |
| 9 | + |
| 10 | +From modules you can log messages at the [resource level](#resource-level-logging) or at the [machine level](#machine-level-logging). |
| 11 | + |
| 12 | +## Resource-level logging |
| 13 | + |
| 14 | +The following log severity levels are available for resource logs: |
| 15 | + |
| 16 | +{{< tabs >}} |
| 17 | +{{% tab name="Python" %}} |
| 18 | + |
| 19 | +```python {class="line-numbers linkable-line-numbers"} |
| 20 | +# Within some method, log information: |
| 21 | +self.logger.debug("debug info") |
| 22 | +self.logger.info("info") |
| 23 | +self.logger.warn("warning info") |
| 24 | +self.logger.error("error info") |
| 25 | +self.logger.exception("error info", exc_info=True) |
| 26 | +self.logger.critical("critical info") |
| 27 | +``` |
| 28 | + |
| 29 | +{{% /tab %}} |
| 30 | +{{% tab name="Go" %}} |
| 31 | + |
| 32 | +```go {class="line-numbers linkable-line-numbers"} |
| 33 | +fn (c *component) someFunction(ctx context.Context, a int) { |
| 34 | + // Log with severity info: |
| 35 | + c.logger.CInfof(ctx, "performing some function with a=%v", a) |
| 36 | + // Log with severity debug (using value wrapping): |
| 37 | + c.logger.CDebugw(ctx, "performing some function", "a" ,a) |
| 38 | + // Log with severity warn: |
| 39 | + c.logger.CWarnw(ctx, "encountered warning for component", "name", c.Name()) |
| 40 | + // Log with severity error without a parameter: |
| 41 | + c.logger.CError(ctx, "encountered an error") |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +{{% /tab %}} |
| 46 | +{{< /tabs >}} |
| 47 | + |
| 48 | +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. |
| 49 | +Resource-level error logs appear in the <strong>ERROR LOGS</strong> section of each resource's configuration card in the app. |
| 50 | + |
| 51 | +{{% alert title="Note" color="note" %}} |
| 52 | +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](/operate/reference/viam-server/#logging). |
| 53 | +{{% /alert %}} |
| 54 | + |
| 55 | +## Machine-level logging |
| 56 | + |
| 57 | +If you need to publish to the global machine-level logs instead of using the recommended resource-level logging, you can follow this example: |
| 58 | + |
| 59 | +```python {class="line-numbers linkable-line-numbers" data-line="2,5"} |
| 60 | +# In your import block, import the logging package: |
| 61 | +from viam.logging import getLogger |
| 62 | + |
| 63 | +# Before your first class or function, define the LOGGER variable: |
| 64 | +LOGGER = getLogger(__name__) |
| 65 | + |
| 66 | +# in some method, log information |
| 67 | +LOGGER.debug("debug info") |
| 68 | +LOGGER.info("info info") |
| 69 | +LOGGER.warn("warn info") |
| 70 | +LOGGER.error("error info") |
| 71 | +LOGGER.exception("error info", exc_info=True) |
| 72 | +LOGGER.critical("critical info") |
| 73 | +``` |
0 commit comments