logform
A flexible log format library designed for chaining and composing log transformations in Rust.
use ;
LogInfo Objects
The LogInfo struct represents a single log message.
let info = LogInfo ;
//OR
let info = new;
//add meta
let info = new.with_meta;//you can chain more
//remove meta
info.without_meta;
//get meta
info.meta.get;
Several of the formats in logform itself add to the meta:
| Property | Format added by | Description |
|---|---|---|
timestamp |
timestamp() |
Timestamp the message was received. |
ms |
ms() |
Number of milliseconds since the previous log message. |
As a consumer, you may add whatever meta you wish
Understanding Formats
Formats in logform are structs that implement a transform method with the signature transform(info: LogInfo, opts: FormatOptions) -> Option<LogInfo>.
info: The LogInfo struct representing the log message.opts: Settings(Options) specific to the current instance of the format.
They are expected to return one of two things:
- A
LogInfoObject representing a new transformed version of theinfoargument. The LogInfo struct is treated as immutable, meaning a new instance is created and returned with the desired modifications. - A None value indicating that the
infoargument should be ignored by the caller. (See: FilteringLogInfoObjects) below.
Creating formats is designed to be as simple as possible. To define a new format, use Format::new() and pass a closure that implements the transformation logic:transform(info: LogInfo, opts: FormatOptions).
The named Format returned can be used to create as many copies of the given Format as desired:
use Format;
Combining Formats
Any number of formats may be combined into a single format using logform::combine. Since logform::combine takes no options, it returns a pre-created instance of the combined format.
use ;
//info: Test message {"key":"value","timestamp":"2024-08-27 02:39:15"}
Filtering LogInfo Objects
If you wish to filter out a given LogInfo Object completely, simply return None.
use Format;
The use of logform::combine will respect any None values returned and stop the evaluation of later formats in the series. For example:
use ;
let will_never_panic = combine;
let info = new;
println!;
// None
Formats
Align
The align format adds a tab character before the message.
let aligned_format = align;
CLI
The cli format is a combination of the colorize and pad_levels formats and accepts both of their options. It pads, colorize, and then formats the log message as level:message.
use HashMap;
use crate::;
let cli_format = cli
.with_option
.with_option
.with_option;
let info = new;
let transformed_info = cli_format.transform;
println!;
// Output: LogInfo { level: "\x1b[34minfo\x1b[39m", message: "\x1b[34m**my message\x1b[39m", meta: {} }
Colorize
The colorize format adds colors to log levels and messages.
let colorizer = colorize
.with_option
.with_option;
Combine
The combine format allows you to chain multiple formats together.
let combined_format = combine;
JSON
The json format converts the log info into a JSON string.
let json_format = json;
Label
The label format adds a specified label to the log message or metadata. It accepts the following options:
- label: The label to prepend to the message or store in the metadata.
- message (optional): Determines where the label is added.
- true (default): Adds the label before the message.
- false: Adds the label to the
metafield instead of the message.
use HashMap;
use crate::;
let label_format = label;
let info = new;
let mut opts = new;
opts.insert;
opts.insert;
let result = label_format.transform.unwrap;
println!;
// Output: LogInfo { level: "info", message: "[MY_LABEL] Test message", meta: {} }
opts.insert;
let result_meta = label_format.transform.unwrap;
println!;
// Output: LogInfo { level: "info", message: "Test message", meta: {"label": "MY_LABEL"} }
Logstash
The logstash format converts the log info into a Logstash-compatible JSON string.
use ;
let logstash_format = combine;
let mut info = new;
let formatted_info = logstash_format.transform.unwrap;
println!;
// {"@message":"my message","@timestamp":"2025-01-12T13:10:05.202213+00:00","@fields":{"level":"info"}}
Metadata
The metadata format collects metadata from the log and adds it to the specified key. It defaults to using the key "metadata", and includes all the keys in info.meta unless exclusions are specified.
It accepts the following options:
- key (optional): Name of the key used for the metadata. Default is
"metadata". - fillExcept (optional): Comma-separated list of keys to exclude from the metadata object.
- fillWith (optional): Comma-separated list of keys to include in the metadata object.
By default, all keys in info.meta are collected into the metadata, except those specified in fillExcept.
use ;
use json;
use HashMap;
let metadata_format = metadata;
let mut info = new;
info.meta.insert;
info.meta.insert;
// Example 1: Default behavior (no options given)
let result = metadata_format.transform.unwrap;
println!;
// Output: LogInfo { level: "info", message: "Test message", meta: {"metadata": Object {"key1": String("value1"), "key2": String("value2")}} }
// Example 2: Only include `key1` in metadata
let mut opts = new;
opts.insert;
let result = metadata_format.transform.unwrap;
println!;
// Output: LogInfo { level: "info", message: "Test message", meta: {"key2": String("value2"), "metadata": Object {"key1": String("value1")}} }
// Example 3: Exclude only `key1` from metadata
let mut opts = new;
opts.insert;
let result = metadata_format.transform.unwrap;
println!;
// Output: LogInfo { level: "info", message: "Test message", meta: {"metadata": Object {"key2": String("value2")}, "key1": String("value1")} }
PadLevels
The pad_levels format pads levels to be the same length.
use HashMap;
use crate::;
let pad_levels_format = pad_levels;
let info = new;
let transformed_info = pad_levels_format.transform;
println!;
// Output: LogInfo { level: "info", message: " my message", meta: {} }
Ms
The ms format adds the time in milliseconds since the last log message.
let ms_format = ms;
PrettyPrint
The pretty_print format provides a more readable output of the log info.
let pretty_format = pretty_print.with_option;
Printf
The printf format allows you to define a custom formatting function.
let printf_format = printf;
Simple
The simple format provides a basic string representation of the log info.
let simple_format = simple;
Timestamp
The timestamp format adds a timestamp to the log info.
let timestamp_format = timestamp
.with_option
.with_option;
Uncolorize
The uncolorize format removes ANSI color codes from the log info.
let uncolorize_format = uncolorize;
Usage
To use logform in your project, add it to your Cargo.toml:
[]
= "0.3"
or with
Then, in your Rust code:
use ;
let format = combine;
let info = new;
let formatted_info = format.transform.unwrap;
println!;
Testing
Run the tests using:
License
This project is licensed under the MIT License.
Acknowledgements
This library is inspired by the logform package for Node.js.