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 1 commit
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
21 changes: 11 additions & 10 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@
// limitations under the License.

use log::LevelFilter;
use logforth::append;
use logforth::filter;
use logforth::filter::FilterResult;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use logforth::{
append,
filter::{CustomFilter, FilterResult},
layout::CustomLayout,
Dispatch, 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
7 changes: 4 additions & 3 deletions examples/json_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::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
7 changes: 4 additions & 3 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,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(layout::JsonLayout)
.append(append::RollingFile::new(writer)),
)
.apply()
.unwrap();
Expand Down
15 changes: 6 additions & 9 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use log::LevelFilter;
use logforth::append;
use logforth::filter;
use logforth::layout;
use logforth::logger::Dispatch;
use logforth::logger::Logger;
use log::Level;
use logforth::{append, filter::MinLevel, layout::TextLayout, Dispatch, Logger};

fn main() {
Logger::new()
.dispatch(
Dispatch::builder(append::Stdout)
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default()),
Dispatch::new()
.filter(MinLevel(Level::Trace))
.layout(TextLayout::default())
.append(append::Stdout),
)
.apply()
.unwrap();
Expand Down
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![]
}
}
16 changes: 5 additions & 11 deletions src/append/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ use opentelemetry::InstrumentationLibrary;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::logs::LoggerProvider;

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

#[derive(Debug)]
pub struct OpenTelemetryLog {
pub struct OpentelemetryLog {
name: String,
category: String,
library: Arc<InstrumentationLibrary>,
provider: LoggerProvider,
}

impl OpenTelemetryLog {
impl OpentelemetryLog {
pub fn new(
name: impl Into<String>,
category: impl Into<String>,
Expand Down Expand Up @@ -68,8 +68,8 @@ impl OpenTelemetryLog {
}
}

impl Append for OpenTelemetryLog {
fn try_append(&self, log_record: &Record) -> anyhow::Result<()> {
impl Append for OpentelemetryLog {
fn append(&self, log_record: &Record) -> anyhow::Result<()> {
let provider = self.provider.clone();
let logger = provider.library_logger(self.library.clone());

Expand Down Expand Up @@ -107,9 +107,3 @@ 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)
}
}
File renamed without changes.
Loading
Loading