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: re-desgin api #18

Merged
merged 5 commits into from
Aug 1, 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 @@ -66,7 +66,7 @@ jobs:
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
cargo run --features="json,rolling_file" --example rolling_file

required:
name: Required
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ rustdoc-args = ["--cfg", "docs"]

[features]
fastrace = ["dep:fastrace"]
file = ["dep:crossbeam-channel", "dep:parking_lot", "dep:time"]
json = ["dep:serde_json", "dep:serde"]
no-color = ["colored/no-color"]
opentelemetry = [
"dep:opentelemetry",
"dep:opentelemetry-otlp",
"dep:opentelemetry_sdk",
]
rolling_file = ["dep:crossbeam-channel", "dep:parking_lot", "dep:time"]

[dependencies]
anyhow = { version = "1.0" }
Expand Down Expand Up @@ -86,4 +86,4 @@ required-features = ["json"]
[[example]]
name = "rolling_file"
path = "examples/rolling_file.rs"
required-features = ["file", "json"]
required-features = ["rolling_file", "json"]
17 changes: 9 additions & 8 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@

use log::LevelFilter;
use logforth::append;
use logforth::filter;
use logforth::filter::CustomFilter;
use logforth::filter::FilterResult;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use logforth::layout::CustomLayout;
use logforth::Dispatch;
use logforth::Logger;

fn main() {
Logger::new()
.dispatch(
Dispatch::builder(append::Stdout)
.filter(filter::BoxDyn::new(|metadata: &log::Metadata| {
Dispatch::new()
.filter(CustomFilter::new(|metadata: &log::Metadata| {
if metadata.level() > LevelFilter::Info {
FilterResult::Accept
} else {
FilterResult::Reject
}
}))
.layout(layout::CustomLayout::new(|record, f| {
.layout(CustomLayout::new(|record, f| {
f(format_args!("[system alert] {}", record.args()))
})),
}))
.append(append::Stdout),
)
.apply()
.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

use log::LevelFilter;
use logforth::append;
use logforth::filter;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use logforth::layout::JsonLayout;
use logforth::Dispatch;
use logforth::Logger;

fn main() {
Logger::new()
.dispatch(
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleJson),
Dispatch::new()
.filter(LevelFilter::Trace)
.layout(JsonLayout)
.append(append::Stdout),
)
.apply()
.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions examples/no_color_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

use log::LevelFilter;
use logforth::append;
use logforth::filter;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use logforth::layout::TextLayout;
use logforth::Dispatch;
use logforth::Logger;

fn main() {
Logger::new()
.dispatch(
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default()),
Dispatch::new()
.filter(LevelFilter::Trace)
.layout(TextLayout::default())
.append(append::Stdout),
)
.apply()
.unwrap();
Expand Down
22 changes: 11 additions & 11 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
// limitations under the License.

use log::LevelFilter;
use logforth::append;
use logforth::append::NonBlockingBuilder;
use logforth::append::RollingFileWriter;
use logforth::append::Rotation;
use logforth::filter;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use logforth::append::rolling_file::NonBlockingBuilder;
use logforth::append::rolling_file::RollingFile;
use logforth::append::rolling_file::RollingFileWriter;
use logforth::append::rolling_file::Rotation;
use logforth::layout::JsonLayout;
use logforth::Dispatch;
use logforth::Logger;

fn main() {
let rolling = RollingFileWriter::builder()
Expand All @@ -34,9 +33,10 @@ fn main() {

Logger::new()
.dispatch(
Dispatch::builder(append::RollingFile::new(writer))
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleJson),
Dispatch::new()
.filter(LevelFilter::Trace)
.layout(JsonLayout)
.append(RollingFile::new(writer)),
)
.apply()
.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

use log::LevelFilter;
use logforth::append;
use logforth::filter;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use logforth::layout::TextLayout;
use logforth::Dispatch;
use logforth::Logger;

fn main() {
Logger::new()
.dispatch(
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default()),
Dispatch::new()
.filter(LevelFilter::Trace)
.layout(TextLayout::default())
.append(append::Stdout),
)
.apply()
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# comment_width = 120
# format_code_in_doc_comments = true
# group_imports = "StdExternalCrate"
# imports_granularity = "Item"
# wrap_comments = true
comment_width = 120
format_code_in_doc_comments = true
group_imports = "StdExternalCrate"
imports_granularity = "Item"
wrap_comments = true
60 changes: 0 additions & 60 deletions src/append/boxdyn.rs

This file was deleted.

13 changes: 5 additions & 8 deletions src/append/fastrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ use std::time::SystemTime;
use log::Record;

use crate::append::Append;
use crate::append::AppendImpl;
use crate::layout::KvDisplay;

#[derive(Default, Debug, Clone)]
pub struct Fastrace;
pub struct FastraceEvent;

impl Append for Fastrace {
fn try_append(&self, record: &Record) -> anyhow::Result<()> {
impl Append for FastraceEvent {
fn append(&self, record: &Record) -> anyhow::Result<()> {
let message = format!(
"{} {:>5} {}{}",
humantime::format_rfc3339_micros(SystemTime::now()),
Expand All @@ -35,10 +34,8 @@ impl Append for Fastrace {
fastrace::Event::add_to_local_parent(message, || []);
Ok(())
}
}

impl From<Fastrace> for AppendImpl {
fn from(append: Fastrace) -> Self {
AppendImpl::Fastrace(append)
fn flush(&self) {
fastrace::flush();
}
}
Loading