Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed assets/how-tos/hello-sensor.png
Binary file not shown.
Binary file removed assets/how-tos/sensor-module-config.png
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/dev/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ result, err := myButton.DoCommand(context.Background(), command)
Using the Viam Registry you can create _{{< glossary_tooltip term_id="resource" text="resources" >}}_ for additional hardware types or models and then deploy them to your machines.
You can use an existing component or service type or create generic resources.

[Create a module →](/operate/modules/support-hardware/hello-world-module/)
[Create a module →](/operate/modules/support-hardware/)

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/operate/modules/advanced/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ For full examples, see [<file>ackermann.py</file>](https://github.com/mcvella/vi
Most Go modules use `resource.AlwaysRebuild` within the `<module-name><resource-name>` struct, which means that the resource rebuilds every time the module is reconfigured.

The steps above use `resource.AlwaysRebuild`.
If you need to maintain the state of your resource, see [(Optional) Create and edit a `Reconfigure` function](/operate/modules/support-hardware/#implement-the-component-api).
If you need to maintain the state of your resource, see [(Optional) Create and edit a `Reconfigure` function](/operate/modules/support-hardware/#implement-api-methods).

{{% /alert %}}

Expand Down
73 changes: 73 additions & 0 deletions docs/operate/modules/advanced/logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: "Module logging"
linkTitle: "Module logging"
weight: 45
layout: "docs"
type: "docs"
description: "Use logging with modules"
---

From modules you can log messages at the [resource level](#resource-level-logging) or at the [machine level](#machine-level-logging).

## Resource-level logging

The following log severity levels are available for resource logs:

{{< tabs >}}
{{% tab name="Python" %}}

```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")
```

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

```go {class="line-numbers linkable-line-numbers"}
fn (c *component) someFunction(ctx context.Context, a int) {
// Log with severity info:
c.logger.CInfof(ctx, "performing some function with a=%v", a)
// Log with severity debug (using value wrapping):
c.logger.CDebugw(ctx, "performing some function", "a" ,a)
// Log with severity warn:
c.logger.CWarnw(ctx, "encountered warning for component", "name", c.Name())
// Log with severity error without a parameter:
c.logger.CError(ctx, "encountered an error")
}
```

{{% /tab %}}
{{< /tabs >}}

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 <strong>ERROR LOGS</strong> 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](/operate/reference/viam-server/#logging).
{{% /alert %}}

## Machine-level 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:
from viam.logging import getLogger

# Before your first class or function, define the LOGGER variable:
LOGGER = getLogger(__name__)

# in some method, log information
LOGGER.debug("debug info")
LOGGER.info("info info")
LOGGER.warn("warn info")
LOGGER.error("error info")
LOGGER.exception("error info", exc_info=True)
LOGGER.critical("critical info")
```
1,344 changes: 820 additions & 524 deletions docs/operate/modules/support-hardware/_index.md

Large diffs are not rendered by default.

Loading
Loading