Struct Formatter

Source
pub struct Formatter {
    pub color_format_string: String,
    pub format_string: String,
    pub timestamp_format: String,
}
Expand description

Logging formatter object.

Use Formatter::new() to create formatter objects instead of using this struct.

§Parameters

  • color_format_string: Format string supporting special ASCII control characters
  • format_string: Format string NOT supporting special ASCII control characters
  • timestamp_format: Timestamp format string in strftime format

§Returns

A new Formatter object with the specified format strings.

§Examples

logging_rs::Formatter {
    color_format_string: "format string with color support".to_owned(),
    format_string: "format string".to_owned(),
    timestamp_format: "timestamp format".to_owned()
};

Fields§

§color_format_string: String

Format string supporting special ASCII control characters

§format_string: String

Format string NOT supporting special ASCII control characters

§timestamp_format: String

Timestamp format string in strftime format

Implementations§

Source§

impl Formatter

Source

pub fn new( color_format_string: &str, format_string: &str, timestamp_format: &str, ) -> Formatter

Creates a new formatter object.

§Parameters
  • color_format_string: Format string supporting special ASCII control characters
  • format_string: Format string NOT supporting special ASCII control characters
  • timestamp_format: Timestamp format string in strftime format
§Returns

A new Formatter object with the specified format strings.

§Examples
logging_rs::Formatter::new(
    "[{{color.bright_blue}}{{timestamp}}{{end}}] [{{level}}] {{path}}: {{message}}",
    "[{{timestamp}}] [{{level}}] {{path}}: {{message}}",
    "%Y-%m-%d %H:%M:%S"
);
§See also
Source

pub fn format<'a>( &self, output: Output, level: Level, message: &'a str, extra_arguments: Vec<(&str, String)>, ) -> String

Formats the given message.

§Parameters
  • self: The formatter object
  • output: The Output to write to
  • level: The log Level to use for formatting
  • message: The message to log
  • arguments: A vector of additional formatting arguments
§Returns

A String containing the formatted message.

§Examples
formatter.format(
    logging_rs::Output::default(),
    logging_rs::Level::default(),
    "Some message with an {{argument}}",
    vec![("argument", "replaced value".to_string())]
);
§See also
§Formatting codes

When formatting a message you can use arguments in the form of {{name}}. Following arguments are available by default:

NameDescriptionExample
messageLog messageMy message
levelUppercase level name. Will have colors attached to it if the output is stdout or stderrERROR
timestampUTC timestamp the log function was called (Technically the time the format function was called). Can be formatted using the timestamp_format field of the formatter2023-11-27 20:49:47
pathRelative path to the caller of the log macrosrc\main.rs

Users can also specify custom arguments by either supplying a Vec<(&str, String)> of key-value pairs of the argument name and value or using the fields in the macros:

logging_rs::log!(logger, "My message with {{arg}}", "arg" = "my arguments")
§ASCII format characters

Most terminals support special ASCII characters.

NameDescription
endEscapes any previously started escape sequences
boldMakes the text bold
italicMakes the text italic
underlineUnderlines the text
overlineOverlines the text (Not supported by all terminals)
color.blackMakes the text black
color.redMakes the text red
color.greenMakes the text green
color.yellowMakes the text yellow
color.blueMakes the text blue
color.magentaMakes the text magenta
color.cyanMakes the text cyan
color.whiteMakes the text white
color.bright_blackMakes the text bright black
color.bright_redMakes the text bright red
color.bright_greenMakes the text bright green
color.bright_yellowMakes the text bright yellow
color.bright_blueMakes the text bright blue
color.bright_magentaMakes the text bright magenta
color.bright_cyanMakes the text bright cyan
color.bright_whiteMakes the text bright white
back.blackMakes the text background black
back.redMakes the text background red
back.greenMakes the text background green
back.yellowMakes the text background yellow
back.blueMakes the text background blue
back.magentaMakes the text background magenta
back.cyanMakes the text background cyan
back.whiteMakes the text background white
back.bright_blackMakes the text background bright black
back.bright_redMakes the text background bright red
back.bright_greenMakes the text background bright green
back.bright_yellowMakes the text background bright yellow
back.bright_blueMakes the text background bright blue
back.bright_magentaMakes the text background bright magenta
back.bright_cyanMakes the text background bright cyan
back.bright_whiteMakes the text background bright white

Trait Implementations§

Source§

impl Clone for Formatter

Source§

fn clone(&self) -> Formatter

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Formatter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Formatter

Source§

fn default() -> Formatter

Returns the “default value” for a type. Read more
Source§

impl Hash for Formatter

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Formatter

Source§

fn cmp(&self, other: &Formatter) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Formatter

Source§

fn eq(&self, other: &Formatter) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Formatter

Source§

fn partial_cmp(&self, other: &Formatter) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Formatter

Source§

impl StructuralPartialEq for Formatter

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.