-
-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
Install Pudu 0.1.2 or later, then create a project and add the package:
pudu init my-app
cd my-app
pudu install @chrismichaelps/pudu-lang-log@0.1.0A logger is a value built from a configuration. Each call on the configuration makes one decision — the minimum level, the clock, a sink — and createLogger builds the logger. Events are written through message templates: the text names each property in braces, and the arguments fill them in order. In Pudu source a brace inside a string starts interpolation, so templates escape theirs with \{ and \}.
module Ex01First
import PuduLangLog.Clock as Clock
import PuduLangLog.Configuration as Configuration
import PuduLangLog as Log
import PuduLangLog.Logger as Logger
import PuduLangLog.Sinks.Console as Console
import PuduLangLog.Value as Value
export fn main() -> Int {
let console = Console.sink(Console.Options{..Console.defaults(), colored: false})
let logger = Configuration.create()
.minimumLevel(Log.Debug)
.clock(Clock.fixed(Log.Timestamp{millis: 1790604309000, offset: 0}))
.writeTo(console)
.createLogger()
logger.information("Hello, \{Name\}!", [Value.text("world")])
let orders = logger.forSource("Shop.Orders")
orders.debug("Placing order \{@Order\}", [Value.structure("Order", [("Id", Value.int(42)), ("Total", Value.decimal(19.99d))])])
orders.warning("Order \{OrderId\} took \{Elapsed:0.0\} ms", [Value.int(42), Value.float(1234.567)])
logger.verbose("Not written: below the minimum", [])
Logger.close(&logger)
0
}
Check it and run it:
pudu check src/Ex01First.pudu
pudu run src/Ex01First.puduOutput:
[14:05:09 INF] Hello, world!
[14:05:09 DBG] Placing order {"Id":42,"Total":19.99,"$type":"Order"}
[14:05:09 WRN] Order 42 took 1234.6 ms
The fixed clock makes the output repeatable; leave it out and events carry the system time. forSource derives a logger whose events carry a SourceContext property, which also selects level overrides. {@Order} keeps the structure's members instead of rendering it to text; the console's default {Message:lj} writes structures as JSON. The verbose event is below the minimum and costs one comparison. Logger.close flushes and closes every sink; call it once before the program ends.