Browse the docs

silk.io.logger_wasi.

A Logger provider for Preview 2 components whose host offers wasi:logging: WasiLogger passes each message to wasi:logging/logging.log.

When to use#

Provide WasiLogger for Logger in a wasm32-wasip2 component that runs on a platform with a wasi:logging host, such as Spin or wasmCloud, so the platform collects the messages with their level. Everywhere else, StderrLogger from silk.io.logger_native writes to standard error, which every WASI runtime supports.

Details#

wasi:logging is a draft proposal, wasi:logging@0.1.0-draft, and not part of the command world, so no logger uses it by default. The bundled WIT world command lists the interface, and a component imports it only when it provides WasiLogger. A component with its own [component].world imports wasi:logging/logging@0.1.0-draft there.

Each call passes the level, the context given to WasiLogger.make, and the formatted message. The logger formats into a 512-byte buffer on the stack, so a longer message is passed as several records, in order, each ending on a whole character. The logger does not allocate.

Errors#

None. wasi:logging cannot refuse a message.

Gotchas#

  • The provider exists only on wasm32-wasip2.

  • A host without wasi:logging, such as wasmtime run, refuses to instantiate a component that uses WasiLogger.

  • LogLevel.Warning is warn in wasi:logging. Its critical level has no Silk level.

Examples#

silk
import silk.core.effect { Effect }
import silk.io.logger { LogError, Logger }
import silk.io.logger_wasi { WasiLogger }

pub effect fn main() -> i32
  ! LogError {
  let mut logger = WasiLogger.make("startup")
  let values = .{ count: 3 }

  // Logs "started 3 workers" at `info` with the context "startup".
  run Effect.logInfo("started {count} workers", &values)
    |> Effect.provideMut<Logger>(&mut logger)
  return 0
}

Import as WasiLogger with import silk.io.logger_wasi { WasiLogger }.

Public declarations: 1.

WasiLogger#

silk
pub struct WasiLogger

A Logger provider that passes each formatted message, its level and a fixed context to wasi:logging.

Details#

A message longer than 512 bytes is passed as several records, in order, each ending on a whole character.

Implementation WasiLogger#

silk
impl WasiLogger

Associated function make#

silk
pub fn make(context: string<'static>) -> WasiLogger

Returns a logger that passes context with every message, such as the name of the component's part that logs. It does not call the host.

Implementation Logger for WasiLogger#

silk
impl Logger for WasiLogger

Operation log#

silk
log: WasiLogger.writeRecord

Category: Input and output.

Profiles: wasm32-wasip2.

View the source of this page