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 programs using the default warning and error output or ConsoleLogger use this import. 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 { Logger }
import silk.mem.allocator { OutOfMemoryError }
import silk.io.logger_console { ConsoleLogger }

pub effect fn main() -> ()
  ! OutOfMemoryError {
  let logger = run ConsoleLogger.shared()
  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.provideAmbient<Logger>(&logger)
  return ()
}

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

Public declarations: 2.

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.

Associated function shared#

silk
pub effect fn shared() -> Arc<ConsoleLogger> ! OutOfMemoryError

Returns a logger in an Arc, ready for Effect.provideAmbient<Logger>. It allocates the Arc with the system allocator and does not call the host.

Details#

The logger passes each formatted message, with its level, to the host's console.

Errors#

  • OutOfMemoryError if the system allocator cannot allocate the Arc.

writeWarning#

silk
pub effect fn writeWarning<Args>(level: LogLevel, static template: string<'static>, args: &Args) -> ()

Sends one default warning or error to the host console.

Implementation Logger for ConsoleLogger#

silk
impl Logger for ConsoleLogger

Operation log#

silk
log: ConsoleLogger.writeConsole

Category: Input and output.

Profiles: wasm32-unknown-unknown, wasm64-unknown-unknown.

View the source of this page