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 2 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
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
12 changes: 6 additions & 6 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::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(layout::JsonLayout)
.append(append::Stdout),
)
.apply()
.unwrap();
Expand Down
7 changes: 4 additions & 3 deletions examples/no_color_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use logforth::logger::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(layout::Text::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();
}
}
103 changes: 12 additions & 91 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,121 +12,42 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub use boxdyn::*;
use std::fmt;

#[cfg(feature = "fastrace")]
pub use fastrace::*;
#[cfg(feature = "file")]
pub use file::*;
#[cfg(feature = "opentelemetry")]
pub use opentelemetry::*;
pub use stdio::*;

use crate::dynlog::DynLog;
use crate::filter::FilterImpl;
use crate::layout;
use crate::filter::Filter;
use crate::layout::IdenticalLayout;
use crate::layout::Layout;

mod boxdyn;
#[cfg(feature = "fastrace")]
mod fastrace;
#[cfg(feature = "file")]
mod file;
#[cfg(feature = "opentelemetry")]
mod opentelemetry;
#[cfg(feature = "file")]
andylokandy marked this conversation as resolved.
Show resolved Hide resolved
pub mod rolling_file;
mod stdio;

pub trait Append {
pub trait Append: fmt::Debug + Send + Sync + 'static {
/// Dispatches a log record to the append target.
fn try_append(&self, record: &log::Record) -> anyhow::Result<()>;
fn append(&self, record: &log::Record) -> anyhow::Result<()>;

/// Flushes any buffered records.
fn flush(&self) {}

/// Default layout to use when [Dispatch][crate::logger::Dispatch] does not configure a
/// preferred layout.
fn default_layout(&self) -> Layout {
Layout::Identical(layout::Identical)
Layout::Identical(IdenticalLayout)
}

/// Default filters associated to this append. [log::Log] is mixed with
/// [Filter][crate::filter::Filter] and [Append].
fn default_filters(&self) -> Option<Vec<FilterImpl>> {
None
}
}

#[derive(Debug)]
pub enum AppendImpl {
BoxDyn(BoxDyn),
DynLog(DynLog),
#[cfg(feature = "fastrace")]
Fastrace(Fastrace),
#[cfg(feature = "opentelemetry")]
OpenTelemetryLog(OpenTelemetryLog),
#[cfg(feature = "file")]
RollingFile(RollingFile),
Stdout(Stdout),
Stderr(Stderr),
}

impl Append for AppendImpl {
fn try_append(&self, record: &log::Record) -> anyhow::Result<()> {
match self {
AppendImpl::BoxDyn(append) => append.try_append(record),
AppendImpl::DynLog(append) => append.try_append(record),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.try_append(record),
#[cfg(feature = "opentelemetry")]
AppendImpl::OpenTelemetryLog(append) => append.try_append(record),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.try_append(record),
AppendImpl::Stdout(append) => append.try_append(record),
AppendImpl::Stderr(append) => append.try_append(record),
}
}

fn flush(&self) {
match self {
AppendImpl::BoxDyn(append) => append.flush(),
AppendImpl::DynLog(append) => append.flush(),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.flush(),
#[cfg(feature = "opentelemetry")]
AppendImpl::OpenTelemetryLog(append) => append.flush(),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.flush(),
AppendImpl::Stdout(append) => append.flush(),
AppendImpl::Stderr(append) => append.flush(),
}
}

fn default_layout(&self) -> Layout {
match self {
AppendImpl::BoxDyn(append) => append.default_layout(),
AppendImpl::DynLog(append) => append.default_layout(),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.default_layout(),
#[cfg(feature = "opentelemetry")]
AppendImpl::OpenTelemetryLog(append) => append.default_layout(),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.default_layout(),
AppendImpl::Stdout(append) => append.default_layout(),
AppendImpl::Stderr(append) => append.default_layout(),
}
}

fn default_filters(&self) -> Option<Vec<FilterImpl>> {
match self {
AppendImpl::BoxDyn(append) => append.default_filters(),
AppendImpl::DynLog(append) => append.default_filters(),
#[cfg(feature = "fastrace")]
AppendImpl::Fastrace(append) => append.default_filters(),
#[cfg(feature = "opentelemetry")]
AppendImpl::OpenTelemetryLog(append) => append.default_filters(),
#[cfg(feature = "file")]
AppendImpl::RollingFile(append) => append.default_filters(),
AppendImpl::Stdout(append) => append.default_filters(),
AppendImpl::Stderr(append) => append.default_filters(),
}
/// [Filter] and [Append].
fn default_filters(&self) -> Vec<Filter> {
vec![]
}
}
Loading
Loading