Skip to content

Commit

Permalink
merge encoder into layout
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 15, 2024
1 parent 7e1f505 commit 98bed57
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 345 deletions.
4 changes: 2 additions & 2 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ fn main() {
}
}))
.append(
append::Stdout::default().with_encoder(CustomLayout::new(|record| {
Ok(format!("[system alert] {}", record.args()))
append::Stdout::default().with_layout(CustomLayout::new(|record| {
Ok(format!("[system alert] {}", record.args()).into_bytes())
})),
),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use log::LevelFilter;
use logforth::append;
use logforth::encoder::JsonEncoder;
use logforth::layout::JsonLayout;
use logforth::Dispatch;
use logforth::Logger;

Expand All @@ -23,7 +23,7 @@ fn main() {
.dispatch(
Dispatch::new()
.filter(LevelFilter::Trace)
.append(append::Stdout::default().with_encoder(JsonEncoder::default())),
.append(append::Stdout::default().with_layout(JsonLayout::default())),
)
.apply()
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use logforth::append::rolling_file::RollingFile;
use logforth::append::rolling_file::RollingFileWriter;
use logforth::append::rolling_file::Rotation;
use logforth::append::Stdout;
use logforth::encoder::JsonEncoder;
use logforth::layout::JsonLayout;
use logforth::Dispatch;
use logforth::Logger;

Expand All @@ -37,7 +37,7 @@ fn main() {
.dispatch(
Dispatch::new()
.filter(LevelFilter::Trace)
.append(RollingFile::new(writer).with_encoder(JsonEncoder::default()))
.append(RollingFile::new(writer).with_layout(JsonLayout::default()))
.append(Stdout::default()),
)
.apply()
Expand Down
6 changes: 3 additions & 3 deletions src/append/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ impl Append for OpentelemetryLog {
log_record_.severity_number = Some(log_level_to_otel_severity(record.level()));
log_record_.severity_text = Some(record.level().as_str());
log_record_.target = Some(record.target().to_string().into());
log_record_.body = Some(AnyValue::from(match self.layout.as_ref() {
None => record.args().to_string(),
log_record_.body = Some(AnyValue::Bytes(Box::new(match self.layout.as_ref() {
None => record.args().to_string().into_bytes(),
Some(layout) => layout.format(record)?,
}));
})));

if let Some(module_path) = record.module_path() {
log_record_.add_attribute("module_path", module_path.to_string());
Expand Down
15 changes: 7 additions & 8 deletions src/append/rolling_file/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ use log::Record;

use crate::append::rolling_file::non_blocking::NonBlocking;
use crate::append::Append;
use crate::encoder::IntoEncoder;
use crate::layout::TextLayout;
use crate::Encoder;
use crate::Layout;

/// An appender that writes log records to a file that rolls over when it reaches a certain date
/// time.
#[derive(Debug)]
pub struct RollingFile {
encoder: Encoder,
layout: Layout,
writer: NonBlocking,
}

Expand All @@ -34,21 +33,21 @@ impl RollingFile {
/// This appender by default uses [`TextLayout`] to format log records as bytes.
pub fn new(writer: NonBlocking) -> Self {
Self {
encoder: TextLayout::default().no_color().into(),
layout: TextLayout::default().no_color().into(),
writer,
}
}

/// Sets the encoder used to format log records as bytes.
pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self {
self.encoder = encoder.into();
/// Sets the layout used to format log records as bytes.
pub fn with_layout(mut self, layout: impl Into<Layout>) -> Self {
self.layout = layout.into();
self
}
}

impl Append for RollingFile {
fn append(&self, record: &Record) -> anyhow::Result<()> {
let mut bytes = self.encoder.format(record)?;
let mut bytes = self.layout.format(record)?;
bytes.push(b'\n');
self.writer.send(bytes)?;
Ok(())
Expand Down
27 changes: 13 additions & 14 deletions src/append/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,34 @@
use std::io::Write;

use crate::append::Append;
use crate::encoder::IntoEncoder;
use crate::layout::TextLayout;
use crate::Encoder;
use crate::Layout;

/// An appender that prints log records to stdout.
#[derive(Debug)]
pub struct Stdout {
encoder: Encoder,
layout: Layout,
}

impl Default for Stdout {
fn default() -> Self {
Self {
encoder: TextLayout::default().into(),
layout: TextLayout::default().into(),
}
}
}

impl Stdout {
/// Creates a new `Stdout` appender with the given encoder.
pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self {
self.encoder = encoder.into();
/// Creates a new `Stdout` appender with the given layout.
pub fn with_layout(mut self, layout: impl Into<Layout>) -> Self {
self.layout = layout.into();
self
}
}

impl Append for Stdout {
fn append(&self, record: &log::Record) -> anyhow::Result<()> {
let mut bytes = self.encoder.format(record)?;
let mut bytes = self.layout.format(record)?;
bytes.push(b'\n');
std::io::stdout().write_all(&bytes)?;
Ok(())
Expand All @@ -57,28 +56,28 @@ impl Append for Stdout {
/// An appender that prints log records to stderr.
#[derive(Debug)]
pub struct Stderr {
encoder: Encoder,
layout: Layout,
}

impl Default for Stderr {
fn default() -> Self {
Self {
encoder: TextLayout::default().into(),
layout: TextLayout::default().into(),
}
}
}

impl Stderr {
/// Creates a new `Stderr` appender with the given encoder.
pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self {
self.encoder = encoder.into();
/// Creates a new `Stderr` appender with the given layout.
pub fn with_layout(mut self, encoder: impl Into<Layout>) -> Self {
self.layout = encoder.into();
self
}
}

impl Append for Stderr {
fn append(&self, record: &log::Record) -> anyhow::Result<()> {
let mut bytes = self.encoder.format(record)?;
let mut bytes = self.layout.format(record)?;
bytes.push(b'\n');
std::io::stderr().write_all(&bytes)?;
Ok(())
Expand Down
67 changes: 0 additions & 67 deletions src/encoder/custom.rs

This file was deleted.

36 changes: 0 additions & 36 deletions src/encoder/json.rs

This file was deleted.

59 changes: 0 additions & 59 deletions src/encoder/mod.rs

This file was deleted.

Loading

0 comments on commit 98bed57

Please sign in to comment.