KScript docs
EN

The language

Logging: $log

Every script has a logger. Entries at info and above go to the terminal (stderr) until you set up somewhere else for them:

<log>Import started</log>
<log level="warn" category="payments" fields="{ order: $order.id }">Card declined for {$customer.name}</log>
<do>$log->info('Saved', { id: $id, size: $bytes })</do>

<let name="db" value="$log->named('db')->with({ host: $config.database.host })" />
<do>$db->debug('Connected')</do> <!-- category db, with a host field -->
<if $log->is_enabled(debug)>
  <do>$log->debug($everything->convert(to: json))</do>
</if>

<try>
  <do>@read_json(./missing.json)</do>
  <catch as="err">
    <do>$log->error('Could not read the file', $err)</do> <!-- error, error_type and error_line fields -->
  </catch>
</try>
log "Import started"
log(level: warn, category: 'payments', fields: { order: $order.id }) "Card declined for {$customer.name}"
$log->info('Saved', { id: $id, size: $bytes })

$db = $log->named('db')->with({ host: $config.database.host })
$db->debug('Connected') // category db, with a host field
if $log->is_enabled(debug) => $log->debug($everything->convert(to: json))

try {
  @read_json(./missing.json)
} catch err {
  $log->error('Could not read the file', $err) // error, error_type and error_line fields
}

Levels are trace, debug, info, warn, error and fatal. Providers decide where entries go, and are usually set in config:

JSON
// kscript.json
{
  "config": {
    "logging": {
      "level": "info",              // the lowest level written (default info)
      "levels": { "db": "debug" },  // per category; db also covers db.queries
      "providers": [
        { "type": "console", "level": "warn" },                  // stderr, coloured at a terminal; stream: stdout, format: json, time: HH:mm:ss.fff
        { "type": "file", "path": "logs/app.log", "format": "json" }, // text, or json lines; roll: daily writes app-2026-11-20.log
        { "fn": "notify", "level": "error" }                     // a fn in the script, called with each entry
      ]
    }
  }
}

log_provider(…) adds one from a script, with the same options (replace removes the ones already set up, including the default console one). A memory provider keeps entries for $log->entries(), which is handy in tests. Entries that a provider holds back, such as an http batch, are sent when the script ends or on $log->flush(). A provider that fails is reported once on stderr, and the others carry on.

Packages add provider types with p.LogProvider("type", description, create), and programs that run KScript can add their own with host.Logs.AddProvider(provider, level), where a provider implements ILogProvider.

Edit this page on GitHub