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

feat: typed DispatchBuilder #16

Merged
merged 1 commit 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/tisonkun/logforth"
rust-version = "1.71.0"
version = "0.3.0"
version = "0.4.0"

[package.metadata.docs.rs]
all-features = true
Expand Down
5 changes: 2 additions & 3 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use logforth::logger::Logger;
fn main() {
Logger::new()
.dispatch(
Dispatch::new()
Dispatch::builder(append::Stdout)
.filter(filter::BoxDyn::new(|metadata: &log::Metadata| {
if metadata.level() > LevelFilter::Info {
FilterResult::Accept
Expand All @@ -33,8 +33,7 @@ fn main() {
}))
.layout(layout::CustomLayout::new(|record, f| {
f(format_args!("[system alert] {}", record.args()))
}))
.append(append::Stdout),
})),
)
.apply()
.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ use logforth::logger::Logger;
fn main() {
Logger::new()
.dispatch(
Dispatch::new()
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleJson)
.append(append::Stdout),
.layout(layout::SimpleJson),
)
.apply()
.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions examples/no_color_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ use logforth::logger::Logger;
fn main() {
Logger::new()
.dispatch(
Dispatch::new()
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default())
.append(append::Stdout),
.layout(layout::SimpleText::default()),
)
.apply()
.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ fn main() {

Logger::new()
.dispatch(
Dispatch::new()
Dispatch::builder(append::RollingFile::new(writer))
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleJson)
.append(append::RollingFile::new(writer)),
.layout(layout::SimpleJson),
)
.apply()
.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ use logforth::logger::Logger;
fn main() {
Logger::new()
.dispatch(
Dispatch::new()
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default())
.append(append::Stdout),
.layout(layout::SimpleText::default()),
)
.apply()
.unwrap();
Expand Down
8 changes: 7 additions & 1 deletion src/append/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use opentelemetry::InstrumentationLibrary;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::logs::LoggerProvider;

use crate::append::Append;
use crate::append::{Append, AppendImpl};

#[derive(Debug)]
pub struct OpenTelemetryLog {
Expand Down Expand Up @@ -107,3 +107,9 @@ fn log_level_to_otel_severity(level: log::Level) -> Severity {
log::Level::Trace => Severity::Trace,
}
}

impl From<OpenTelemetryLog> for AppendImpl {
fn from(append: OpenTelemetryLog) -> Self {
AppendImpl::OpenTelemetryLog(append)
}
}
79 changes: 62 additions & 17 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,85 @@ use crate::filter::FilterResult;
use crate::layout::Layout;

#[derive(Debug)]
pub struct Dispatch {
filters: Vec<FilterImpl>,
pub struct DispatchBuilder {
appends: Vec<AppendImpl>,
preferred_layout: Option<Layout>,
}

impl Default for Dispatch {
fn default() -> Self {
Self::new()
impl DispatchBuilder {
pub fn new(append: impl Into<AppendImpl>) -> Self {
Self {
appends: vec![append.into()],
}
}
}

impl Dispatch {
pub fn new() -> Self {
Self {
pub fn append(mut self, append: impl Into<AppendImpl>) -> Self {
self.appends.push(append.into());
self
}

pub fn filter(self, filter: impl Into<FilterImpl>) -> DispatchFilterBuilder {
DispatchFilterBuilder {
appends: self.appends,
filters: vec![filter.into()],
}
}

pub fn layout(self, layout: impl Into<Layout>) -> Dispatch {
Dispatch {
filters: vec![],
appends: self.appends,
preferred_layout: Some(layout.into()),
}
}

pub fn finish(self) -> Dispatch {
Dispatch {
filters: vec![],
appends: vec![],
appends: self.appends,
preferred_layout: None,
}
}
}

#[derive(Debug)]
pub struct DispatchFilterBuilder {
appends: Vec<AppendImpl>,
filters: Vec<FilterImpl>,
}

impl DispatchFilterBuilder {
pub fn filter(mut self, filter: impl Into<FilterImpl>) -> Self {
self.filters.push(filter.into());
self
}

pub fn append(mut self, append: impl Into<AppendImpl>) -> Self {
self.appends.push(append.into());
self
pub fn layout(self, layout: impl Into<Layout>) -> Dispatch {
Dispatch {
filters: self.filters,
appends: self.appends,
preferred_layout: Some(layout.into()),
}
}

pub fn layout(mut self, layout: impl Into<Layout>) -> Self {
self.preferred_layout = Some(layout.into());
self
pub fn finish(self) -> Dispatch {
Dispatch {
filters: self.filters,
appends: self.appends,
preferred_layout: None,
}
}
}

#[derive(Debug)]
pub struct Dispatch {
filters: Vec<FilterImpl>,
appends: Vec<AppendImpl>,
preferred_layout: Option<Layout>,
}

impl Dispatch {
pub fn builder(append: impl Into<AppendImpl>) -> DispatchBuilder {
DispatchBuilder::new(append)
}

fn enabled(&self, metadata: &Metadata) -> bool {
Expand Down