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 charactersformat_string
: Format string NOT supporting special ASCII control characterstimestamp_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
impl Formatter
Sourcepub fn new(
color_format_string: &str,
format_string: &str,
timestamp_format: &str,
) -> Formatter
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 charactersformat_string
: Format string NOT supporting special ASCII control characterstimestamp_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
Sourcepub fn format<'a>(
&self,
output: Output,
level: Level,
message: &'a str,
extra_arguments: Vec<(&str, String)>,
) -> String
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 objectoutput
: TheOutput
to write tolevel
: The logLevel
to use for formattingmessage
: The message to logarguments
: 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:
Name | Description | Example |
---|---|---|
message | Log message | My message |
level | Uppercase level name. Will have colors attached to it if the output is stdout or stderr | ERROR |
timestamp | UTC timestamp the log function was called (Technically the time the format function was called). Can be formatted using the timestamp_format field of the formatter | 2023-11-27 20:49:47 |
path | Relative path to the caller of the log macro | src\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.
Name | Description |
---|---|
end | Escapes any previously started escape sequences |
bold | Makes the text bold |
italic | Makes the text italic |
underline | Underlines the text |
overline | Overlines the text (Not supported by all terminals) |
color.black | Makes the text black |
color.red | Makes the text red |
color.green | Makes the text green |
color.yellow | Makes the text yellow |
color.blue | Makes the text blue |
color.magenta | Makes the text magenta |
color.cyan | Makes the text cyan |
color.white | Makes the text white |
color.bright_black | Makes the text bright black |
color.bright_red | Makes the text bright red |
color.bright_green | Makes the text bright green |
color.bright_yellow | Makes the text bright yellow |
color.bright_blue | Makes the text bright blue |
color.bright_magenta | Makes the text bright magenta |
color.bright_cyan | Makes the text bright cyan |
color.bright_white | Makes the text bright white |
back.black | Makes the text background black |
back.red | Makes the text background red |
back.green | Makes the text background green |
back.yellow | Makes the text background yellow |
back.blue | Makes the text background blue |
back.magenta | Makes the text background magenta |
back.cyan | Makes the text background cyan |
back.white | Makes the text background white |
back.bright_black | Makes the text background bright black |
back.bright_red | Makes the text background bright red |
back.bright_green | Makes the text background bright green |
back.bright_yellow | Makes the text background bright yellow |
back.bright_blue | Makes the text background bright blue |
back.bright_magenta | Makes the text background bright magenta |
back.bright_cyan | Makes the text background bright cyan |
back.bright_white | Makes the text background bright white |