Back to Sway

Logging

docs/reference/src/documentation/operations/log.md

0.71.01.5 KB
Original Source

Logging

Logging is a way to record data as the program runs.

The standard library provides a logging module which contains a generic log function that is used to log a variable of any type.

Each call to log appends 1 of 2 types of a receipt to the list of receipts

  • Log
    • Generated for non-reference types: bool, u8, u16, u32, and u64
  • LogData
    • Generated for reference types and u256

The Rust & Typescript SDKs may be used to decode the data.

Example

To use the log function we must import it from the standard library and pass in any generic type T that we want to log.

sway
{{#include ../../code/operations/logging/src/lib.sw:logging}}

In the example above a u64 is used however we can pass in any generic type such as a struct, enum, string etc.