Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 1, 2024
1 parent 8f654ff commit c141fc8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
- name: Run examples
run: |
cargo run --example simple_stdio
cargo run --example fn_layout_filter
cargo run --features="no-color" --example no_color_stdio
cargo run --features="json" --example json_stdio
cargo run --features="json,file" --example rolling_file
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ time = { version = "0.3", features = [
"parsing",
], optional = true }

[[example]]
name = "fn_layout_filter"
path = "examples/fn_layout_filter.rs"

[[example]]
name = "simple_stdio"
path = "examples/simple_stdio.rs"
Expand Down
48 changes: 48 additions & 0 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 tison <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use log::LevelFilter;
use logforth::BoxDynFilter;
use logforth::BoxDynLayout;
use logforth::DispatchAppend;
use logforth::FilterResult;
use logforth::Logger;
use logforth::StdoutAppend;

fn main() {
let layout = BoxDynLayout::new(|record: &log::Record| {
let message = format!("[box dyn] {}", record.args());
Ok(message.into_bytes())
// ...or
// anyhow::bail!("boom: {}", message)
});

let filter = BoxDynFilter::new(|metadata: &log::Metadata| {
if metadata.level() <= LevelFilter::Info {
FilterResult::Accept
} else {
FilterResult::Reject
}
});

let append = StdoutAppend::default().with_layout(layout);
let append = DispatchAppend::new(append).filter(filter);
Logger::new().add_append(append).apply().unwrap();

log::error!("Hello error!");
log::warn!("Hello warn!");
log::info!("Hello info!");
log::debug!("Hello debug!");
log::trace!("Hello trace!");
}
1 change: 1 addition & 0 deletions src/layout/boxdyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::fmt::Debug;

use log::Record;

use crate::Layout;
Expand Down
26 changes: 13 additions & 13 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ impl log::Log for Logger {
fn handle_error(record: &Record, error: anyhow::Error) {
let Err(fallback_error) = write!(
std::io::stderr(),
r#"
Error perform logging.
Attempted to log: {args}
Record: {record:?}
Error: {error}
"#,
r###"
Error perform logging.
Attempted to log: {args}
Record: {record:?}
Error: {error}
"###,
args = record.args(),
record = record,
error = error,
Expand All @@ -86,13 +86,13 @@ fn handle_error(record: &Record, error: anyhow::Error) {
};

panic!(
r#"
Error performing stderr logging after error occurred during regular logging.
Attempted to log: {args}
Record: {record:?}
Error: {error}
Fallback error: {fallback_error}
"#,
r###"
Error performing stderr logging after error occurred during regular logging.
Attempted to log: {args}
Record: {record:?}
Error: {error}
Fallback error: {fallback_error}
"###,
args = record.args(),
record = record,
error = error,
Expand Down

0 comments on commit c141fc8

Please sign in to comment.