Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use more comprehensive api #69

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
run: |
cargo run --example simple_stdio
cargo run --example fn_layout_filter
cargo run --example env_filter
cargo run --example default_filter_respect_env
cargo run --features="no-color" --example simple_stdio
cargo run --features="json" --example json_stdio
cargo run --features="json,rolling_file" --example rolling_file
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ name = "fn_layout_filter"
path = "examples/fn_layout_filter.rs"

[[example]]
name = "env_filter"
path = "examples/env_filter.rs"
name = "default_filter_respect_env"
path = "examples/default_filter_respect_env.rs"
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@ Then, you can use the logger with the simplest default setup:

```rust
fn main() {
logforth::stderr().finish();
logforth::stderr().apply();
}
```

Or configure the logger in a more fine-grained way:

```rust
use log::LevelFilter;
use logforth::append;

fn main() {
logforth::builder()
.filter(log::LevelFilter::Debug)
.append(logforth::append::Stderr::default())
.dispatch()
.filter(log::LevelFilter::Info)
.append(logforth::append::Stdout::default())
.finish();
logforth::dispatch(|b| b.filter(LevelFilter::Debug).append(append::Stderr::default()))
.and_dispatch(|b| b.filter(LevelFilter::Info).append(append::Stdout::default()))
.apply();
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use logforth::append;
use logforth::filter::EnvFilter;

fn main() {
logforth::builder()
.filter(EnvFilter::from_default_env())
.append(append::Stdout::default())
.finish();
logforth::stdout().apply();

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
7 changes: 4 additions & 3 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use logforth::filter::FilterResult;
use logforth::layout::CustomLayout;

fn main() {
logforth::builder()
.filter(CustomFilter::new(|metadata: &log::Metadata| {
logforth::dispatch(|b| {
b.filter(CustomFilter::new(|metadata: &log::Metadata| {
if metadata.level() > LevelFilter::Info {
FilterResult::Accept
} else {
Expand All @@ -32,7 +32,8 @@ fn main() {
Ok(format!("[system alert] {}", record.args()).into_bytes())
})),
)
.finish();
})
.apply();

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
5 changes: 2 additions & 3 deletions examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use logforth::append;
use logforth::layout::JsonLayout;

fn main() {
logforth::builder()
.append(append::Stdout::default().with_layout(JsonLayout::default()))
.finish();
logforth::dispatch(|b| b.append(append::Stdout::default().with_layout(JsonLayout::default())))
.apply();

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
13 changes: 6 additions & 7 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ fn main() {
.unwrap();
let (writer, _guard) = NonBlockingBuilder::default().finish(rolling);

logforth::builder()
.filter("trace")
.append(RollingFile::new(writer).with_layout(JsonLayout::default()))
.dispatch()
.filter("info")
.append(Stdout::default())
.finish();
logforth::dispatch(|b| {
b.filter("trace")
.append(RollingFile::new(writer).with_layout(JsonLayout::default()))
})
.and_dispatch(|b| b.filter("info").append(Stdout::default()))
.apply();

let repeat = 1;

Expand Down
11 changes: 6 additions & 5 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ use log::LevelFilter;
use logforth::append;

fn main() {
logforth::builder()
.filter(LevelFilter::Trace)
.append(append::Stdout::default())
.append(append::Stderr::default())
.finish();
logforth::dispatch(|b| {
b.filter(LevelFilter::Trace)
.append(append::Stdout::default())
.append(append::Stderr::default())
})
.apply();

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@ cargo add logforth
Then, you can use the logger with the simplest default setup:

```rust
logforth::stderr().finish();
logforth::stderr().apply();
```

Or configure the logger in a more fine-grained way:

```rust
logforth::builder()
.filter(log::LevelFilter::Debug)
.append(logforth::append::Stderr::default())
.dispatch()
.filter(log::LevelFilter::Info)
.append(logforth::append::Stdout::default())
.finish();
use log::LevelFilter;
use logforth::append;

logforth::dispatch(|b| b.filter(LevelFilter::Debug).append(append::Stderr::default()))
.and_dispatch(|b| b.filter(LevelFilter::Info).append(append::Stdout::default()))
.apply();
```

Read more demos under the [examples](https://github.com/fast/logforth/tree/main/examples) directory.
Expand Down
Loading