Browse the docs

silk.io.logger_console.

A Logger provider for browser and JavaScript hosts: ConsoleLogger hands each message to the host's console.

When to use#

Provide ConsoleLogger for Logger in a wasm32-unknown-unknown program that runs in a browser, Node.js, Deno or another JavaScript host. On WASI and native targets, use silk.io.logger_native instead. Code that logs uses silk.io.logger, so a test can provide InMemoryLogger instead.

Details#

The module imports one host function, console from the import module silk, and only a program that provides ConsoleLogger imports it: a module that never logs this way keeps importing nothing. The standard library's runner, host/wasm-runner.mjs, supplies the import. It passes each message to options.onLog with its level, and by default to the console method of the level: console.debug for trace and debug, console.info, console.warn and console.error.

The import is console(level, address, length, more). level counts from 0 for trace to 4 for error, address and length name UTF-8 bytes in the module's memory, and more is 1 when the next call continues the same message. The logger formats into a 256-byte buffer on the stack, so a longer message arrives in several parts, which the host joins before it decodes them. The logger does not allocate.

Errors#

None. A host cannot refuse a message.

Gotchas#

  • The provider exists only on wasm32-unknown-unknown.

  • A host that instantiates the module itself supplies silk.console, or the instantiation fails.

  • The logger adds no level, timestamp, or newline. The console shows the level by its method.

Examples#

silk
import silk.core.effect { Effect }
import silk.io.logger { LogError, Logger }
import silk.io.logger_console { ConsoleLogger }

pub effect fn main() -> ()
  ! LogError {
  let mut logger = ConsoleLogger.make()
  let values = .{ name: "silk", count: 3 }

  // Calls `console.info("silk started 3 workers")` in the host.
  run Effect.logInfo("{name} started {count} workers", &values)
    |> Effect.provideMut<Logger>(&mut logger)
  return ()
}

Import as ConsoleLogger with import silk.io.logger_console { ConsoleLogger }.

Public declarations: 1.

ConsoleLogger#

silk
pub struct ConsoleLogger

A Logger provider that passes each formatted message, with its level, to the host's console.

Details#

The host receives the message without a newline. A message longer than 256 bytes arrives in parts that the host joins.

Implementation ConsoleLogger#

silk
impl ConsoleLogger

Associated function make#

silk
pub fn make() -> ConsoleLogger

Returns a logger. It does not call the host.

Implementation Logger for ConsoleLogger#

silk
impl Logger for ConsoleLogger

Operation log#

silk
log: ConsoleLogger.writeConsole

Category: Input and output.

Profiles: wasm32-unknown-unknown.

View the source of this page