Struct Builder
pub struct Builder { /* private fields */ }
Builder acts as builder for initializing a Logger.
It can be used to customize the log format, change the environment variable used to provide the logging directives and also set the default log level filter.
Examples
# use Write;
use Builder;
use ;
let mut builder = from_default_env;
builder
.format
.filter
.init;
error!;
info!;
Implementations
impl Builder
fn new() -> BuilderInitializes the log builder with defaults.
NOTE: This method won't read from any environment variables. Use the
filterandwrite_stylemethods to configure the builder or usefrom_envorfrom_default_envinstead.Examples
Create a new builder and configure filters and style:
use LevelFilter; use ; let mut builder = new; builder .filter .write_style .init;fn from_env<'a, E>(env: E) -> Self where E: Into<Env<'a>>,Initializes the log builder from the environment.
The variables used to read configuration from can be tweaked before passing in.
Examples
Initialise a logger reading the log filter from an environment variable called
MY_LOG:use Builder; let mut builder = from_env; builder.init;Initialise a logger using the
MY_LOGvariable for filtering andMY_LOG_STYLEfor whether or not to write styles:use ; let env = new.filter.write_style; let mut builder = from_env; builder.init;fn parse_env<'a, E>(&mut self, env: E) -> &mut Self where E: Into<Env<'a>>,Applies the configuration from the environment.
This function allows a builder to be configured with default parameters, to be then overridden by the environment.
Examples
Initialise a logger with filter level
Off, then override the log filter from an environment variable calledMY_LOG:use LevelFilter; use Builder; let mut builder = new; builder.filter_level; builder.parse_env; builder.init;Initialise a logger with filter level
Off, then use theMY_LOGvariable to override filtering andMY_LOG_STYLEto override whether or not to write styles:use LevelFilter; use ; let env = new.filter.write_style; let mut builder = new; builder.filter_level; builder.parse_env; builder.init;fn from_default_env() -> SelfInitializes the log builder from the environment using default variable names.
This method is a convenient way to call
from_env(Env::default())without having to use theEnvtype explicitly. The builder will use the default environment variables.Examples
Initialise a logger using the default environment variables:
use Builder; let mut builder = from_default_env; builder.init;fn parse_default_env(&mut self) -> &mut SelfApplies the configuration from the environment using default variable names.
This method is a convenient way to call
parse_env(Env::default())without having to use theEnvtype explicitly. The builder will use the default environment variables.Examples
Initialise a logger with filter level
Off, then configure it using the default environment variables:use LevelFilter; use Builder; let mut builder = new; builder.filter_level; builder.parse_default_env; builder.init;fn format<F>(&mut self, format: F) -> &mut Self where F: Fn(&mut Formatter, &Record<'_>) -> Result<()> + Sync + Send + 'static,Sets the format function for formatting the log output.
This function is called on each record logged and should format the log record and output it to the given
Formatter.The format function is expected to output the string directly to the
Formatterso that implementations can use thestd::fmtmacros to format and output without intermediate heap allocations. The defaultenv_loggerformatter takes advantage of this.When the
colorfeature is enabled, styling via ANSI escape codes is supported and the output will automatically respectBuilder::write_style.Examples
Use a custom format to write only the log message:
use Write; use Builder; let mut builder = new; builder.format;fn default_format(&mut self) -> &mut SelfUse the default format.
This method will clear any custom format set on the builder.
fn format_level(&mut self, write: bool) -> &mut SelfWhether or not to write the level in the default format.
fn format_file(&mut self, write: bool) -> &mut SelfWhether or not to write the source file path in the default format.
fn format_line_number(&mut self, write: bool) -> &mut SelfWhether or not to write the source line number path in the default format.
Only has effect if
format_fileis also enabledfn format_source_path(&mut self, write: bool) -> &mut SelfWhether or not to write the source path and line number
Equivalent to calling both
format_fileandformat_line_numberwithtruefn format_module_path(&mut self, write: bool) -> &mut SelfWhether or not to write the module path in the default format.
fn format_target(&mut self, write: bool) -> &mut SelfWhether or not to write the target in the default format.
fn format_indent(&mut self, indent: Option<usize>) -> &mut SelfConfigures the amount of spaces to use to indent multiline log records. A value of
Nonedisables any kind of indentation.fn format_timestamp(&mut self, timestamp: Option<TimestampPrecision>) -> &mut SelfConfigures if timestamp should be included and in what precision.
fn format_timestamp_secs(&mut self) -> &mut SelfConfigures the timestamp to use second precision.
fn format_timestamp_millis(&mut self) -> &mut SelfConfigures the timestamp to use millisecond precision.
fn format_timestamp_micros(&mut self) -> &mut SelfConfigures the timestamp to use microsecond precision.
fn format_timestamp_nanos(&mut self) -> &mut SelfConfigures the timestamp to use nanosecond precision.
fn format_suffix(&mut self, suffix: &'static str) -> &mut SelfConfigures the end of line suffix.
fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut SelfAdds a directive to the filter for a specific module.
Examples
Only include messages for info and above for logs in
path::to::module:use Builder; use LevelFilter; let mut builder = new; builder.filter_module;fn filter_level(&mut self, level: LevelFilter) -> &mut SelfAdds a directive to the filter for all modules.
Examples
Only include messages for info and above for logs globally:
use Builder; use LevelFilter; let mut builder = new; builder.filter_level;fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut SelfAdds filters to the logger.
The given module (if any) will log at most the specified level provided. If no module is provided then the filter will apply to all log messages.
Examples
Only include messages for info and above for logs in
path::to::module:use Builder; use LevelFilter; let mut builder = new; builder.filter;fn parse_filters(&mut self, filters: &str) -> &mut SelfParses the directives string in the same form as the
RUST_LOGenvironment variable.See the module documentation for more details.
fn target(&mut self, target: Target) -> &mut SelfSets the target for the log output.
Env logger can log to either stdout, stderr or a custom pipe. The default is stderr.
The custom pipe can be used to send the log messages to a custom sink (for example a file). Do note that direct writes to a file can become a bottleneck due to IO operation times.
Examples
Write log message to
stdout:use ; let mut builder = new; builder.target;fn write_style(&mut self, write_style: WriteStyle) -> &mut SelfSets whether or not styles will be written.
This can be useful in environments that don't support control characters for setting colors.
Examples
Never attempt to write styles:
use ; let mut builder = new; builder.write_style;fn parse_write_style(&mut self, write_style: &str) -> &mut SelfParses whether or not to write styles in the same form as the
RUST_LOG_STYLEenvironment variable.See the module documentation for more details.
fn is_test(&mut self, is_test: bool) -> &mut SelfSets whether or not the logger will be used in unit tests.
If
is_testistruethen the logger will allow the testing framework to capture log records rather than printing them to the terminal directly.fn try_init(&mut self) -> Result<(), SetLoggerError>Initializes the global logger with the built env logger.
This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.
Errors
This function will fail if it is called more than once, or if another library has already initialized a global logger.
fn init(&mut self)Initializes the global logger with the built env logger.
This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.
Panics
This function will panic if it is called more than once, or if another library has already initialized a global logger.
fn build(&mut self) -> LoggerBuild an env logger.
The returned logger implements the
Logtrait and can be installed manually or nested within another logger.
Trait Implementations
impl Debug for Builder
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Builder
fn default() -> Builder
Auto Trait Implementations
impl !RefUnwindSafe for Builder
impl !Sync for Builder
impl !UnwindSafe for Builder
impl Freeze for Builder
impl Send for Builder
impl Unpin for Builder
impl UnsafeUnpin for Builder
Blanket Implementations
impl<T> Any for Builder
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Builder
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Builder
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Builder
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Builder
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Builder
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Builder
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>