moonlog-config

    Configuration loader and global hub for moonbitstack/moonlog.

    moonlog
    log
    config
    yaml
    toml
    json
    moonbit
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    30 minutes ago
    Downloads
    2

    #moonlog-config

    Configuration loader and process-wide hub for moonbitstack/moonlog.

    Refused

    pub(all) suberror Refused {
    Refused(at~ : String, got~ : String, want~ : String)
    }

    What went wrong in a configuration file, said in the terms a reader can act on: where in the document, what was there, and what belongs there.

    at is a key path such as slots.file.max_size, or empty for the document as a whole.

    Refused::to_text

    fn Refused::to_text(self : Refused) -> String

    A refusal with the key path it happened at.

    Alarm

    pub(all) struct Alarm {
    tiers : Array[Array[String]]
    remind : Int64
    fallback :
    Fallback

    }

    An alarm as a document writes it: which sinks to try, in which tiers, how often to remind, and what to do when every tier is spent.

    Alarm::tiers

    The alarm as the hub wants it.

    Config

    pub(all) struct Config {
    profile :
    Profile

    filter : String
    slots : Array[Slot]
    alarm : Alarm?
    app : String
    env : String
    instance : String
    levels : Array[
    Level
    ]
    chain : Array[&
    Processor
    ]
    process : Array[
    Json
    ]
    alarms : Array[(String, Alarm)]
    watch : Int64?
    filter_env : String?
    kinds : Array[Kind]
    }

    A logging configuration, read from a document and checked against the settings the shipped sinks read.

    Reading a document does not build anything: it says what the deployment asked for, and the application — or moonlog/global — turns that into sinks. That split is what lets the same document be checked, dumped and reloaded without a file system in the way.

    Config::alarm

    fn Config::alarm(self : Config) -> Alarm?

    The alarm the document describes, if any.

    Config::alarms

    fn Config::alarms(self : Config) -> Array[(String, Alarm)]

    The alarms the document names, each with the name a sink points at.

    Config::app

    fn Config::app(self : Config) -> String

    The deployment's own name, as the document gives it.

    Config::build

    fn Config::build(self : Config, slot : Slot) -> &
    Sink
    ? raise Refused

    The sink the program's own kind makes for slot, or None when the slot is a built-in kind.

    Config::chain

    The chain every record walks before a slot sees it.

    Config::dump

    fn Config::dump(self : Config, format? : Format) -> String

    Writes the configuration as a document, which is what dump is for: a deployment can be read back and checked without the file that produced it.

    Config::env

    fn Config::env(self : Config) -> String

    The environment the deployment calls itself part of.

    Config::filter

    fn Config::filter(self : Config) -> String

    The filter the document asks for, as written.

    Config::filter_env

    fn Config::filter_env(self : Config) -> String?

    The variable whose value overrides the filter, when the document names one.

    Config::instance

    fn Config::instance(self : Config) -> String

    The node within that environment.

    Config::levels

    The levels the document defines, in the order it defines them.

    Config::load

    fn Config::load(path~ : String) -> Config raise Refused

    Reads a configuration file, taking the format from its extension.

    Config::new

    fn Config::new() -> Config

    What a configuration with no document says: the default profile, the default filter, and nothing to write to.

    Config::parse

    fn Config::parse(text : StringView, format? : Format, lookup? : (String) -> String?, kinds? : Array[Kind]) -> Config raise Refused

    Reads a document.

    ${NAME} in any string is replaced by the environment variable of that name; a name the environment does not have is refused, because a configuration that silently logs to an empty URL is worse than one that will not start.

    kinds are sinks of the program's own that the document may name beside the built-in ones.

    Config::process

    The chain's steps as the document wrote them, which is what dump writes back.

    Config::profile

    The profile the document asks for.

    Config::save

    fn Config::save(self : Config, path~ : String) -> Unit raise Refused

    Writes the configuration to path in the format its extension asks for.

    Config::schema

    The JSON Schema a document of this shape answers to.

    It is built from the same tables the parser checks against, so the schema an editor or a pipeline validates with cannot drift from the parser: a key the schema allows is a key the parser reads, and a value of the wrong shape is refused in both places. Publish it next to a deployment for the editor to pick up; moonlog/config never needs it to read a file.

    Config::slot

    fn Config::slot(self : Config, name : String) -> Slot?

    The slot called name, if there is one.

    Config::slots

    fn Config::slots(self : Config) -> Array[Slot]

    The slots the document describes, in document order.

    Config::to_json

    The document this configuration is written as.

    Config::watch

    fn Config::watch(path~ : String, format? : Format, lookup? : (String) -> String?) -> Watcher raise Refused

    Starts watching path.

    Config::watch_span

    fn Config::watch_span(self : Config) -> Int64?

    How often the document asks for its file to be checked, in nanoseconds.

    Named watch_span because Config::watch is the watcher this package hands back for a file.

    Format

    pub(all) enum Format {
    Yaml
    Json
    Toml
    }

    The three shapes a configuration file is written in. All three read into the same tree, so the same document means the same configuration whichever one it is written in.

    Format::name

    fn Format::name(self : Format) -> StringView

    The name this format is written as.

    Format::of

    fn Format::of(path : StringView) -> Format raise Refused

    The format a file name asks for, by its extension.

    Format::parse

    fn Format::parse(name : StringView) -> Format raise Refused

    A format by name.

    Format::read

    fn Format::read(self : Format, text : StringView, at : String) ->
    Json
    raise Refused

    Reads a document into the tree every reader in this family produces.

    Format::write

    fn Format::write(self : Format, tree :
    Json
    ) -> String

    Writes the tree back out in this format.

    Kind

    pub(all) struct Kind {
    name : String
    keys : Array[String]
    build : (Slot) -> &
    Sink
    raise Refused
    }

    A sink of your own that a document can name: what kind it answers to, the keys it reads beside the ones every slot has, and how to make it from the slot once the document is read.

    A name a built-in kind has is refused: a document that says file should not change meaning with what the program passed in.

    Kind::new

    fn Kind::new(name : String, build : (Slot) -> &
    Sink
    raise Refused, keys? : Array[String]) -> Kind

    A kind called name, built by build, whose keys are checked for being there and no more: the shape of each is for build to judge, through Slot::text and the rest.

    Queue

    pub(all) struct Queue {
    size : Int
    batch : Int
    wait : Int64
    full :
    Full

    }

    A queue as a document writes it.

    Slot

    pub(all) struct Slot {
    name : String
    kind : String
    filter : String?
    layout : String?
    colour : Bool?
    retries : Int?
    probe : Int64?
    queue : Queue?
    chain : Array[&
    Processor
    ]
    process : Array[
    Json
    ]
    on_fail : String?
    alarm_only : Bool
    settings : Map[String,
    Json
    ]
    }

    One sink as the document describes it.

    settings holds the keys that belong to the kind; text, flag, size and the rest read them back with the shape the parser already checked.

    Slot::count

    fn Slot::count(self : Slot, key : String) -> Int?

    A setting as a whole number.

    Slot::flag

    fn Slot::flag(self : Slot, key : String) -> Bool?

    A setting as a yes or no.

    Slot::keys

    fn Slot::keys(self : Slot) -> Array[String]

    The keys this slot's built-in kind reads, which is what a deployment file is checked against.

    Slot::pairs

    fn Slot::pairs(self : Slot, key : String) -> Array[(String, String)]?

    A setting as a table of strings, in document order where the format keeps one.

    Slot::size

    fn Slot::size(self : Slot, key : String) -> Int64?

    A setting as a byte count, 10mb and a bare number alike.

    Slot::span

    fn Slot::span(self : Slot, key : String) -> Int64?

    A setting as a number of nanoseconds.

    Slot::text

    fn Slot::text(self : Slot, key : String) -> String?

    A setting as a string. The parser has already checked that anything that is there has the shape the key asks for, so a None here only means the document left the key out.

    Slot::texts

    fn Slot::texts(self : Slot, key : String) -> Array[String]?

    A setting as a list of strings.

    Watcher

    pub struct Watcher {
    path : String
    seen : String
    format : Format
    lookup : (String) -> String?
    }

    A configuration file watched for changes.

    There is no background task here: poll reads the file and answers it when the bytes differ from the last document it saw, which keeps the reload on the same thread as everything else and makes it something a test can step through. Comparing content — rather than the modification time — means a rewrite is never missed because two edits landed inside one timestamp tick, which is the usual way a fast test (or a coarse filesystem clock) fools an mtime check. A configuration document is small, so reading it on each poll is cheap; parsing happens only when the content actually moved.

    Watcher::path

    fn Watcher::path(self : Watcher) -> String

    The file being watched.

    Watcher::poll

    fn Watcher::poll(self : Watcher) -> Result[Config?, String]

    The newest configuration, when the file has changed since the last answer.

    Ok(None) means the bytes are unchanged. An Err means the file changed and the new one will not do — the caller keeps what it has and decides whether to say so.

    nanos_as_span

    fn nanos_as_span(nanos : Int64) -> String

    A span in the shortest form that reads back as the same number of nanoseconds.

    parse_size

    fn parse_size(at : String, text : StringView) -> Int64 raise Refused

    How many bytes a size names: 512, 4kb, 10MiB, 1.5gb.

    A bare number is bytes. k, m, g and t are powers of a thousand, ki, mi, gi and ti powers of 1024, with or without a b.

    parse_span

    fn parse_span(at : String, text : StringView) -> Int64 raise Refused

    How many nanoseconds a span names: 200ms, 5s, 1m30s, 2h.

    A span always says its unit — a bare number is refused, because a configuration that means seconds in one program and nanoseconds in another is a configuration that is wrong silently.

    remind_default

    let remind_default : Int64

    One second, the reminder an alarm gets when the document does not say.