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#
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#
pub struct ConsoleLoggerA 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#
impl ConsoleLoggerAssociated function make#
pub fn make() -> ConsoleLoggerReturns a logger. It does not call the host.
Associated function shared#
pub effect fn shared() -> Arc<ConsoleLogger> ! OutOfMemoryErrorReturns 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#
OutOfMemoryErrorif the system allocator cannot allocate theArc.
writeWarning#
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#
impl Logger for ConsoleLoggerOperation log#
log: ConsoleLogger.writeConsoleCategory: Input and output.
Profiles: wasm32-unknown-unknown, wasm64-unknown-unknown.