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.get_meta;
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;
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;
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.2"
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.