Skip to content

Commit

Permalink
refactor: use more comprehensive api (#69)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Oct 30, 2024
1 parent cf14588 commit aee4d10
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 151 deletions.
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

0 comments on commit aee4d10

Please sign in to comment.