pub struct LoggerBuilder { /* private fields */ }std only.Expand description
Builder for Logger. Create with Logger::builder.
Defaults: filter info for every target, timestamps on, thread-local
context capture on, flush after every record at Level::Error or
above, no default fields, no redaction, empty default target, no
error handler, no sinks.
§Examples
use log_io::{Level, Logger};
let logger = Logger::builder()
.level(Level::Info)
.env_filter("LOG_IO") // e.g. LOG_IO="info,app::db=debug"
.with_default_field("service", "api")
.redact_common_secrets()
.stderr_human()
.try_build()?;Implementations§
Source§impl LoggerBuilder
impl LoggerBuilder
Sourcepub const COMMON_SECRET_KEYS: &'static [&'static str]
pub const COMMON_SECRET_KEYS: &'static [&'static str]
Key patterns redacted by LoggerBuilder::redact_common_secrets.
Each * matches any run of characters (see
LoggerBuilder::redact_key), so *password* covers password,
db_password and user_password_hash, and *token* covers
x-auth-token and refresh_token. Matching treats - and _
alike and ignores ASCII case, so *api_key* also covers the HTTP
header names Api-Key and X-Api-Key.
The patterns favour catching secrets over keeping every value:
*token* also redacts a count such as token_count. Rename such
fields, or list your own keys with LoggerBuilder::redact_key
instead of this set. (Before 2.2 the set held exact names only.)
Sourcepub fn level(self, level: Level) -> Self
pub fn level(self, level: Level) -> Self
Set the default minimum severity, keeping any per-target rules.
Sourcepub fn target_level(self, target: impl Into<String>, level: Level) -> Self
pub fn target_level(self, target: impl Into<String>, level: Level) -> Self
Add (or replace) a per-target rule. See Filter::with_rule.
Sourcepub fn filter_directive(self, directive: &str) -> Self
pub fn filter_directive(self, directive: &str) -> Self
Parse directive (see Filter::parse) and replace the filter.
A blank directive (empty or whitespace only) changes nothing. An
invalid directive, including one with only empty segments such
as ",", keeps the previous filter and is reported by Self::try_build as
BuildError::InvalidFilter; Self::build ignores it. Use
Self::try_filter_directive to handle the error immediately.
Sourcepub fn try_filter_directive(
self,
directive: &str,
) -> Result<Self, ParseFilterError>
pub fn try_filter_directive( self, directive: &str, ) -> Result<Self, ParseFilterError>
Parse directive and replace the filter, failing immediately on
an invalid directive.
§Errors
Returns the ParseFilterError from Filter::parse.
Sourcepub fn env_filter(self, var: &str) -> Self
pub fn env_filter(self, var: &str) -> Self
Configure the filter from the environment variable var, using
the Filter::parse directive syntax (RUST_LOG-style), for
example .env_filter("LOG_IO") or .env_filter("RUST_LOG").
If the variable is unset, empty, or blank, the filter configured
so far (for example by an earlier Self::level) is kept. A set
value replaces the whole filter, default level included, as
RUST_LOG does: LOG_IO=app::db=debug logs only app::db, so
write LOG_IO=info,app::db=debug to keep info elsewhere. An
invalid value (including "," and a value that is not valid
Unicode) keeps the previous filter and is reported by
Self::try_build.
Sourcepub fn default_target(self, target: impl Into<String>) -> Self
pub fn default_target(self, target: impl Into<String>) -> Self
Target used by Logger::log, which has no target argument.
Defaults to the empty string. Per-target filter rules apply to
Logger::log records only through this target.
Sourcepub fn no_timestamps(self) -> Self
pub fn no_timestamps(self) -> Self
Do not timestamp records. By default every record gets the
current wall-clock time from std::time::SystemTime.
Sourcepub fn no_context(self) -> Self
pub fn no_context(self) -> Self
Do not attach thread-local context (crate::context).
Sourcepub fn with_default_field<'a, V: Into<Value<'a>>>(
self,
key: impl Into<String>,
value: V,
) -> Self
pub fn with_default_field<'a, V: Into<Value<'a>>>( self, key: impl Into<String>, value: V, ) -> Self
Attach a field to every record, for identifiers such as
service or version. Setting the same key again replaces the
value. Context and call-site fields with the same key take
precedence over defaults.
Sourcepub fn flush_on(self, level: Level) -> Self
pub fn flush_on(self, level: Level) -> Self
Flush every sink after each record at or above level. Default
Level::Error, so errors reach the OS before the logging call
returns. Level::Off disables flush-on-level; Level::Trace
flushes after every record.
The flush goes through Sink::request_flush. The built-in
synchronous sinks flush before the call returns. A
crate::sink::NonBlocking sink with
crate::sink::Overflow::Block also waits, until its queue has
been written and flushed; with
crate::sink::Overflow::DropNewest it only queues the request
and returns at once, so the record is not yet durable when the
call returns. Call Logger::flush when you need that.
Sourcepub fn redact_key(self, key: impl Into<String>) -> Self
pub fn redact_key(self, key: impl Into<String>) -> Self
Replace the value of every field named key with
"[REDACTED]". Applies to call-site fields, context, and default
fields. The message text is never inspected.
Matching is ASCII case-insensitive, treats - and _ as the
same character (so api_key covers the header name Api-Key),
and also tests the last dot-separated segment of a key (so
password covers db.password).
A * in key matches any run of characters, including none
(since 2.2): *_secret covers client_secret, db_* covers
every key starting with db_, and *token* any key containing
token. Without a * the key must match exactly.
Only keys are matched. A secret inside a Debug or Display
value (req = ?login_form) or in the message text is not seen.
Sourcepub fn redact_common_secrets(self) -> Self
pub fn redact_common_secrets(self) -> Self
Redact every key in Self::COMMON_SECRET_KEYS.
Sourcepub fn on_error(self, handler: ErrorHandler) -> Self
pub fn on_error(self, handler: ErrorHandler) -> Self
Install a callback for sink, flush, and formatting errors. See
ErrorHandler.
Sourcepub fn stdout<F: Format + 'static>(self, format: F) -> Self
pub fn stdout<F: Format + 'static>(self, format: F) -> Self
Install a standard-output sink with format.
Sourcepub fn stderr<F: Format + 'static>(self, format: F) -> Self
pub fn stderr<F: Format + 'static>(self, format: F) -> Self
Install a standard-error sink with format.
Sourcepub fn writer<W, F>(self, writer: W, format: F) -> Self
pub fn writer<W, F>(self, writer: W, format: F) -> Self
Install a sink that writes to writer with format.
Sourcepub fn build(self) -> Logger
pub fn build(self) -> Logger
Finish configuration.
An invalid filter directive recorded by
Self::filter_directive or Self::env_filter is ignored
(the previous filter stays in effect); use Self::try_build
to surface it.
§Panics
Panics if no sink has been installed, which is a programming
error rather than a runtime condition. Use Self::try_build
when the sinks depend on configuration.
Sourcepub fn try_build(self) -> Result<Logger, BuildError>
pub fn try_build(self) -> Result<Logger, BuildError>
Finish configuration, reporting problems as values.
§Errors
BuildError::NoSinkif no sink was installed.BuildError::InvalidFilterif a filter directive (including one read from the environment) failed to parse.
Source§impl LoggerBuilder
impl LoggerBuilder
Sourcepub fn stdout_json(self) -> Self
Available on crate feature json only.
pub fn stdout_json(self) -> Self
json only.Install a standard-output sink with crate::format::JsonFormat.
Sourcepub fn stderr_json(self) -> Self
Available on crate feature json only.
pub fn stderr_json(self) -> Self
json only.Install a standard-error sink with crate::format::JsonFormat.
Source§impl LoggerBuilder
impl LoggerBuilder
Sourcepub fn stdout_logfmt(self) -> Self
Available on crate feature logfmt only.
pub fn stdout_logfmt(self) -> Self
logfmt only.Install a standard-output sink with crate::format::LogfmtFormat.
Sourcepub fn stderr_logfmt(self) -> Self
Available on crate feature logfmt only.
pub fn stderr_logfmt(self) -> Self
logfmt only.Install a standard-error sink with crate::format::LogfmtFormat.
Source§impl LoggerBuilder
impl LoggerBuilder
Sourcepub fn stdout_human(self) -> Self
Available on crate feature human only.
pub fn stdout_human(self) -> Self
human only.Install a standard-output sink with crate::format::HumanFormat,
coloured when standard output is a terminal (see
crate::format::ColorChoice::Auto).
Sourcepub fn stderr_human(self) -> Self
Available on crate feature human only.
pub fn stderr_human(self) -> Self
human only.Install a standard-error sink with crate::format::HumanFormat,
coloured when standard error is a terminal (see
crate::format::ColorChoice::Auto).