Skip to content

Diagnostics And Testing

Chris Michael edited this page Sep 29, 2026 · 1 revision

Pudu

Diagnostics And Testing

Logging never fails the caller. Problems inside the pipeline — a sink that fails, arguments that do not match the template, an invalid property name — go to the self-log, which is silent until enabled with SelfLog.enable or created with SelfLog.toErrorStream.

For tests, Sinks.Memory keeps events to inspect, Sinks.Observable hands them to observers, and Clock.fixed and Clock.stepping make timestamps exact.

module Ex10Diagnostics

import Std.Io as Io
import PuduLangLog.Clock as Clock
import PuduLangLog.Configuration as Configuration
import PuduLangLog.Event as Event
import PuduLangLog as Log
import PuduLangLog.Logger as Logger
import PuduLangLog.SelfLog as SelfLog
import PuduLangLog.Sink as Sink
import PuduLangLog.Sinks.Memory as Memory
import PuduLangLog.Value as Value

export fn main() -> Int {
  let problems = SelfLog.create()
  SelfLog.enable(&problems, fn(line: Str) -> () { let _said = Io.writeLine("self-log: " + line) })
  let memory = Memory.create()
  let broken = Sink.of(fn(_event: &Log.Event) -> Result[(), Str] { Err("connection refused") })
  let logger = Configuration.create()
    .selfLog(&problems)
    .clock(Clock.stepping(Log.Timestamp{millis: 0, offset: 0}, 1000))
    .writeTo(broken)
    .writeTo(Memory.sink(&memory))
    .createLogger()
  logger.information("Hello \{Name\} and \{Other\}", [Value.text("ada")])
  logger.forContext(" ", Value.int(1)).information("second", [])
  logger.warning("third \{N\}", [Value.int(3)])
  let times = Memory.events(&memory).map(|event: Log.Event| show(event.timestamp.millis))
  let _times = Io.writeLine("timestamps: " + times.join(", "))
  let _warnings = Io.writeLine("warnings: " + show(Memory.matching(&memory, fn(event: &Log.Event) -> Bool { event.level == Log.Warning }).length()))
  let _latest = Io.writeLine("latest: " + match Memory.latest(&memory) {
      case Some(event) => Event.message(&event)
      case None => "none"
    })
  Logger.close(&logger)
  0
}

Check it and run it:

pudu check src/Ex10Diagnostics.pudu
pudu run src/Ex10Diagnostics.pudu

Output (the self-log's own timestamps are the system time):

self-log: 2026-09-29T14:37:36.5060000+00:00 Named property count does not match parameter count: Hello {Name} and {Other}
self-log: 2026-09-29T14:37:36.5070000+00:00 Caught failure while emitting to sink: connection refused
self-log: 2026-09-29T14:37:36.5090000+00:00 Attempt to add a context property with an invalid name ` `
self-log: 2026-09-29T14:37:36.5100000+00:00 Caught failure while emitting to sink: connection refused
self-log: 2026-09-29T14:37:36.5150000+00:00 Caught failure while emitting to sink: connection refused
timestamps: 0, 1000, 2000
warnings: 1
latest: third 3

Related

Clone this wiki locally