Skip to content

Parse-logger-config-text ​

Parse JSON or YAML text into a typed LoggerConfig. Text that starts with { or [ is parsed as JSON. Any other text is parsed as YAML 1.2 through moonbitstack/moonyaml and then validated against the same schema.

At the public src layer, this parser is a facade over the concrete config parser owned by src/config_model, and the returned config object is the shared @config_model.LoggerConfig model.

Interface ​

moonbit
pub fn parse_logger_config_text(input : String) -> LoggerConfig raise ConfigError {}

input ​

  • input : String - Raw JSON or YAML text matching the supported logger config schema.

output ​

  • LoggerConfig - Parsed typed configuration value.

Explanation ​

Detailed rules explaining key parameters and behaviors

  • Parsing is separated from runtime construction.
  • Any parsed file rotation field is stored as the shared @file_model.FileRotation model inside SinkConfig.rotation.
  • This API is ideal for validating, editing, or inspecting config values before calling build_logger(...).
  • Supported keys are intentionally constrained to stable built-in sink shapes and formatter options.
  • Omitted top-level keys fall back to the same defaults used by the typed config constructors: min_level=Info, target="", timestamp=false, sink=default_sink_config(), and queue=None.
  • When a sink object is present, parsing selects the built-in sink kind and validates sink-specific data before any runtime logger is built. In particular, kind=File requires a non-empty path.
  • parse_and_build_logger(...) is only this parser plus build_logger(...), so using the two-step path here does not change the configured runtime pipeline.
  • The error surface is ConfigError rather than silent fallback behavior.

How to Use ​

Here are some specific examples provided.

When Need Validation Before Runtime Build ​

When config should be reviewed before creating the logger:

moonbit
let config = parse_logger_config_text(raw) catch {
  err => return
}
let logger = build_logger(config)

In this example, parsing and building remain separate phases.

When Need To Inspect Parsed Values ​

When config should be normalized into typed values first:

moonbit
let config = parse_logger_config_text(raw) catch {
  err => return
}
println(config.target)

In this example, the parsed object can be inspected or rewritten before runtime use.

Error Case ​

e.g.:

  • If JSON text is invalid, parsing raises ConfigError and the message starts with Invalid JSON:.

  • If supported fields have wrong types or unsupported enum text, parsing raises ConfigError.

  • If sink.kind is File but sink.path is empty, parsing raises ConfigError before runtime construction.

  • If YAML text is refused by the YAML reader, parsing raises ConfigError and the message starts with Invalid config:.

Notes ​

  1. Prefer this API when you need typed config inspection before building.

  2. Prefer parse_and_build_logger(...) when one-step bootstrapping is enough.

Published from the repository docs folder with VitePress.