witness#
gleam add witness@1 logging@1
import gleam/io
import witness
pub fn main() -> Nil {
configure_logging()
// log a message
witness.this(witness.Info, "Some log message", [
witness.string("example", "you can add structured data"),
witness.bool("also_bools", True),
witness.float("and_floats", 1.23),
witness.int("ints_as_well", 3),
])
}
/// Configure the logging. (Only needs to be run once at startup)
///
fn configure_logging() {
// create a new config
witness.empty_config()
// log formatted text to stdout
|> witness.with_sink(witness.Text, fn(level, message) {
io.println(witness.level_to_badge(level) <> ": " <> message)
})
// log as json
|> witness.with_sink(witness.Json, fn(_, message) {
// ideally you'd log this to a file using, for example, the erlang logger
io.println(message)
})
// apply this config globally
|> witness.set_config()
}
Will log this json line:
{"timestamp":"2026-07-22T17:13:17.100815963Z","level":"info","message":"Some log message","example":"you can add structured data","also_bools":true,"and_floats":1.23,"ints_as_well":3}
And this text to the console:
[INFO]: 19:13:17 Some log message
example: you can add structured data
also_bools: True
and_floats: 1.23
ints_as_well: 3
Further documentation can be found at https://witness.hexdocs.pm.