Skip to content

Commit 2a69e10

Browse files
authored
refactor: improve api doc (#71)
1 parent d3394df commit 2a69e10

File tree

25 files changed

+489
-217
lines changed

25 files changed

+489
-217
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ jobs:
7171
run: cargo test --all-features -- --nocapture
7272
- name: Run examples
7373
run: |
74-
cargo run --example text_stdio
74+
cargo run --example simple_stdout
75+
cargo run --example multiple_dispatches
7576
cargo run --example custom_layout_filter
76-
cargo run --example env_filter
77-
cargo run --features="no-color" --example text_stdio
78-
cargo run --features="json" --example json_stdio
77+
cargo run --features="no-color" --example simple_stdout
78+
cargo run --features="json" --example json_stdout
7979
cargo run --features="json,rolling_file" --example rolling_file
8080
8181
required:

Cargo.toml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ colored = { version = "2.1" }
4848
env_filter = { version = "0.1" }
4949
jiff = { version = "0.1.13" }
5050
log = { version = "0.4", features = ["std", "kv_unstable"] }
51-
paste = { version = "1.0" }
5251

5352
[dev-dependencies]
5453
rand = "0.8"
5554
tempfile = "3.13"
55+
tokio = { version = "1", features = ["rt-multi-thread"] }
5656

5757
## Serde dependencies
5858
[dependencies.serde]
@@ -96,13 +96,16 @@ version = "0.26"
9696

9797
## Examples
9898
[[example]]
99-
name = "text_stdio"
100-
path = "examples/text_stdio.rs"
99+
name = "simple_stdout"
100+
path = "examples/simple_stdout.rs"
101101

102102
[[example]]
103-
name = "json_stdio"
104-
path = "examples/json_stdio.rs"
105-
required-features = ["json"]
103+
name = "json_stdout"
104+
path = "examples/json_stdout.rs"
105+
106+
[[example]]
107+
name = "multiple_dispatches"
108+
path = "examples/multiple_dispatches.rs"
106109

107110
[[example]]
108111
name = "rolling_file"
@@ -112,7 +115,3 @@ required-features = ["rolling_file", "json"]
112115
[[example]]
113116
name = "custom_layout_filter"
114117
path = "examples/custom_layout_filter.rs"
115-
116-
[[example]]
117-
name = "env_filter"
118-
path = "examples/env_filter.rs"

README.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Logforth Project
1+
# Logforth
22

33
[![Crates.io][crates-badge]][crates-url]
44
[![Documentation][docs-badge]][docs-url]
@@ -16,40 +16,58 @@
1616
[actions-badge]: https://github.com/fast/logforth/workflows/CI/badge.svg
1717
[actions-url]:https://github.com/fast/logforth/actions?query=workflow%3ACI
1818

19-
## Overview
19+
Logforth is a flexible and easy-to-use logging framework for Rust applications. It allows you to configure multiple dispatches, filters, and appenders to customize your logging setup according to your needs.
2020

21-
A versatile and extensible logging implementation.
21+
## Features
2222

23-
## Usage
23+
- **Multiple Dispatches**: Configure different logging behaviors for different parts of your application.
24+
- **Flexible Filters**: Use built-in or custom filters to control which log records are processed.
25+
- **Various Appenders**: Output logs to stdout, stderr, files, or even send them to OpenTelemetry collectors.
26+
- **Custom Layouts**: Format log records using predefined layouts or create your own.
2427

25-
Add the dependencies to your `Cargo.toml` with:
28+
## Getting Started
29+
30+
Add `log` and `logforth` to your `Cargo.toml`:
2631

2732
```shell
2833
cargo add log
2934
cargo add logforth
3035
```
3136

32-
... where [log](https://crates.io/crates/log) is the logging facade and [logforth](https://crates.io/crates/logforth) is the logging implementation.
37+
## Simple Usage
3338

34-
Then, you can use the logger with the simplest default setup:
39+
Set up a basic logger that outputs to stdout:
3540

3641
```rust
3742
fn main() {
38-
logforth::stderr().apply();
43+
logforth::stdout().apply();
44+
45+
log::info!("This is an info message.");
46+
log::debug!("This debug message will not be printed by default.");
3947
}
4048
```
4149

42-
Or configure the logger in a more fine-grained way:
50+
## Advanced Usage
51+
52+
Configure multiple dispatches with different filters and appenders:
4353

4454
```rust
45-
use log::LevelFilter;
4655
use logforth::append;
56+
use log::LevelFilter;
4757

4858
fn main() {
4959
logforth::builder()
50-
.dispatch(|d| d.filter(LevelFilter::Debug).append(append::Stderr::default()))
51-
.dispatch(|d| d.filter(LevelFilter::Info).append(append::Stdout::default()))
60+
.dispatch(|d| d
61+
.filter(LevelFilter::Error)
62+
.append(append::Stderr::default()))
63+
.dispatch(|d| d
64+
.filter(LevelFilter::Info)
65+
.append(append::Stdout::default()))
5266
.apply();
67+
68+
log::error!("This error will be logged to stderr.");
69+
log::info!("This info will be logged to stdout.");
70+
log::debug!("This debug message will not be logged.");
5371
}
5472
```
5573

examples/custom_layout_filter.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use log::LevelFilter;
1615
use logforth::append;
1716
use logforth::filter::CustomFilter;
1817
use logforth::filter::FilterResult;
@@ -21,16 +20,16 @@ use logforth::layout::CustomLayout;
2120
fn main() {
2221
logforth::builder()
2322
.dispatch(|d| {
24-
d.filter(CustomFilter::new(|metadata: &log::Metadata| {
25-
if metadata.level() > LevelFilter::Info {
23+
d.filter(CustomFilter::new(|metadata| {
24+
if metadata.level() < log::Level::Info {
2625
FilterResult::Accept
2726
} else {
2827
FilterResult::Reject
2928
}
3029
}))
3130
.append(
3231
append::Stdout::default().with_layout(CustomLayout::new(|record| {
33-
Ok(format!("[system alert] {}", record.args()).into_bytes())
32+
Ok(format!("[Alert] {}", record.args()).into_bytes())
3433
})),
3534
)
3635
})
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ fn main() {
2020
.dispatch(|d| d.append(append::Stdout::default().with_layout(JsonLayout::default())))
2121
.apply();
2222

23-
log::error!("Hello error!");
24-
log::warn!("Hello warn!");
25-
log::info!("Hello info!");
26-
log::debug!("Hello debug!");
27-
log::trace!("Hello trace!");
23+
log::info!("This is an info message.");
24+
log::debug!("This debug message will not be printed by default.");
2825
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ use logforth::append;
1818
fn main() {
1919
logforth::builder()
2020
.dispatch(|d| {
21-
d.filter(LevelFilter::Trace)
22-
.append(append::Stdout::default())
21+
d.filter(LevelFilter::Error)
2322
.append(append::Stderr::default())
2423
})
24+
.dispatch(|d| {
25+
d.filter(LevelFilter::Info)
26+
.append(append::Stdout::default())
27+
})
2528
.apply();
2629

2730
log::error!("Hello error!");

examples/rolling_file.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,22 @@ use logforth::append::rolling_file::NonBlockingBuilder;
1616
use logforth::append::rolling_file::RollingFile;
1717
use logforth::append::rolling_file::RollingFileWriter;
1818
use logforth::append::rolling_file::Rotation;
19-
use logforth::append::Stdout;
2019
use logforth::layout::JsonLayout;
2120

2221
fn main() {
23-
let rolling = RollingFileWriter::builder()
24-
.rotation(Rotation::Minutely)
25-
.filename_prefix("example")
26-
.filename_suffix("log")
27-
.max_log_files(10)
28-
.max_file_size(1024 * 1024)
22+
let rolling_writer = RollingFileWriter::builder()
23+
.rotation(Rotation::Daily)
24+
.filename_prefix("app_log")
2925
.build("logs")
3026
.unwrap();
31-
let (writer, _guard) = NonBlockingBuilder::default().finish(rolling);
27+
28+
let (non_blocking, _guard) = NonBlockingBuilder::default().finish(rolling_writer);
3229

3330
logforth::builder()
3431
.dispatch(|d| {
35-
d.filter("trace")
36-
.append(RollingFile::new(writer).with_layout(JsonLayout::default()))
32+
d.filter(log::LevelFilter::Trace)
33+
.append(RollingFile::new(non_blocking).with_layout(JsonLayout::default()))
3734
})
38-
.dispatch(|d| d.filter("info").append(Stdout::default()))
3935
.apply();
4036

4137
let repeat = 1;

src/append/fastrace.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
//! Appender for integrating with [fastrace](https://crates.io/crates/fastrace).
16+
1517
use std::borrow::Cow;
1618

1719
use jiff::Zoned;
@@ -21,6 +23,14 @@ use crate::append::Append;
2123
use crate::layout::collect_kvs;
2224

2325
/// An appender that adds log records to fastrace as an event associated to the current span.
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// use logforth::append::FastraceEvent;
31+
///
32+
/// let fastrace_appender = FastraceEvent::default();
33+
/// ```
2434
#[derive(Default, Debug, Clone)]
2535
pub struct FastraceEvent {
2636
_private: (), // suppress structure literal syntax

src/append/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! Dispatch log records to the appropriate target.
15+
//! Dispatch log records to various targets.
1616
1717
use std::fmt;
1818

19+
#[cfg(feature = "fastrace")]
20+
mod fastrace;
21+
#[cfg(feature = "opentelemetry")]
22+
pub mod opentelemetry;
23+
#[cfg(feature = "rolling_file")]
24+
pub mod rolling_file;
25+
mod stdio;
26+
1927
#[cfg(feature = "fastrace")]
2028
pub use self::fastrace::FastraceEvent;
2129
#[cfg(feature = "opentelemetry")]
@@ -25,14 +33,9 @@ pub use self::rolling_file::RollingFile;
2533
pub use self::stdio::Stderr;
2634
pub use self::stdio::Stdout;
2735

28-
#[cfg(feature = "fastrace")]
29-
mod fastrace;
30-
#[cfg(feature = "opentelemetry")]
31-
pub mod opentelemetry;
32-
#[cfg(feature = "rolling_file")]
33-
pub mod rolling_file;
34-
mod stdio;
35-
36+
/// A trait representing an appender that can process log records.
37+
///
38+
/// Implementors of this trait can handle log records in custom ways.
3639
pub trait Append: fmt::Debug + Send + Sync + 'static {
3740
/// Dispatches a log record to the append target.
3841
fn append(&self, record: &log::Record) -> anyhow::Result<()>;

0 commit comments

Comments
 (0)