Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-log

Structured event logging for Pudu: message templates, enrichment, filtering, formatting, and sinks

0.1.0Apache-2.01

InstallClose

Settings.pudu

Pudu51 lines2.1 KB

GitHub ↗
1/** @Example.Settings.Program — a logger configured from JSON settings */2module Settings34import Std.Io as Io5import PuduLangLog.Configuration as Configuration6import PuduLangLog.LevelSwitch as LevelSwitch7import PuduLangLog as Log8import PuduLangLog.Logger as Logger9import PuduLangLog.Settings.Registry as Registry10import PuduLangLog.Settings as Settings11import PuduLangLog.Value as Value1213/// The settings an application would keep in its configuration file, one line each.14const DOCUMENT: Array[Str] = [15  "\{\"Log\": \{",16  "  \"MinimumLevel\": \{\"Default\": \"Debug\", \"Override\": \{\"Settings.Http\": \"Warning\"\}\},",17  "  \"LevelSwitches\": \{\"$console\": \"Information\"\},",18  "  \"Properties\": \{\"Application\": \"Settings example\"\},",19  "  \"Enrich\": [\"MachineName\"],",20  "  \"Filter\": [\{\"ByExcluding\": \"RequestPath like '/health%'\"\}],",21  "  \"WriteTo\": [\{\"Name\": \"Console\", \"Args\": \{\"theme\": \"none\", \"levelSwitch\": \"$console\", \"outputTemplate\": \"[\{Level:u3\}] \{Message:lj\} (\{Application\})\{NewLine\}\"\}\}]",22  "\}\}"23]2425/// Loads the settings, writes a few events, lowers the console's level, and answers 0.26fn main() -> Int {27  let pairs = match Settings.fromJson(DOCUMENT.join("\n"), "Log") {28    case Ok(found) => found29    case Err(problem) => {30      let _said = Io.writeErrorLine(problem)31      return 132    }33  }34  let loaded = match Settings.apply(&Configuration.create(), &pairs.concat(Settings.fromEnvironment("SETTINGS_EXAMPLE")), &Registry.standard()) {35    case Ok(found) => found36    case Err(problems) => {37      for problem in problems { let _said = Io.writeErrorLine(problem) }38      return 139    }40  }41  let logger = loaded.configuration.createLogger()42  logger.information("Started with \{Count\} settings", [Value.int(pairs.length())])43  logger.debug("Hidden until the console switch is lowered", [])44  logger.forSource("Settings.Http").information("Hidden by the override", [])45  logger.information("Served \{RequestPath\}", [Value.text("/health/live")])46  if let Some(console) = Settings.switchNamed(&loaded, "$console") { LevelSwitch.set(&console, Log.Debug) }47  logger.debug("Visible after lowering the console switch", [])48  Logger.close(&logger)49  050}51