Skip to content

Provide a method to easily set log level names #33

@gpanders

Description

@gpanders

The add_default_keys() method sets the level field to use the short version version of log fields (e.g. ERRO for Error). If a user does not want to use the short field names, they must essentially re-implement add_default_keys() themselves.

That is,

let drain = slog_json::Json::default(std::io::stderr())

now becomes

let drain = slog_json::Json::new(std::io::stderr())
    .add_key_value(slog::o!(
        "ts" => slog::FnValue(move |_: &slog::Record| {
            time::OffsetDateTime::now_utc()
            .format(&time::format_description::well_known::Rfc3339)
            .ok()
        }),
        "level" => slog::FnValue(move |rinfo: &slog::Record| {
            rinfo.level().as_str()
        }),
        "msg" => slog::PushFnValue(move |record: &slog::Record, ser| {
            ser.emit(record.msg())
        }),
    ))
    .build();

which also requires adding a (top level) dependency on the time crate.

I would like to be able to use the default keys, but be able to choose the strings used for the log levels. Example API:

let drain = slot_json::Json::new(std::io::stderr())
    .add_default_keys()
    .with_log_levels(["off", "critical", "error", "warn", "info", "debug", "trace"])
    .build();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions