Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 1, 2024
1 parent d0432e0 commit 045be6b
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 158 deletions.
41 changes: 18 additions & 23 deletions examples/fn_layout_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,26 @@
// limitations under the License.

use log::LevelFilter;

use logforth::append::{DispatchAppend, StdoutAppend};
use logforth::filter::{BoxDynFilter, FilterResult};
use logforth::layout::BoxDynLayout;
use logforth::logger::Logger;
use logforth::{append, filter, layout};
use logforth::filter::{FilterResult};
use logforth::logger::{Dispatch, Logger};

fn main() {
let layout = BoxDynLayout::new(|record: &log::Record| {
let message = format!("[box dyn] {}", record.args());
Ok(message.into_bytes())
// ...or
// anyhow::bail!("boom: {}", message)
});

let filter = BoxDynFilter::new(|metadata: &log::Metadata| {
if metadata.level() <= LevelFilter::Info {
FilterResult::Accept
} else {
FilterResult::Reject
}
});

let append = StdoutAppend::default().with_layout(layout);
let append = DispatchAppend::new(append).filter(filter);
Logger::new().add_append(append).apply().unwrap();
Logger::new().dispatch(Dispatch::new()
.filter(filter::BoxDyn::new(|metadata: &log::Metadata| {
if metadata.level() <= LevelFilter::Info {
FilterResult::Accept
} else {
FilterResult::Reject
}
}))
.layout(layout::BoxDyn::new(|record: &log::Record| {
let message = format!("[box dyn] {}", record.args());
Ok(message.into_bytes())
// ...or
// anyhow::bail!("boom: {}", message)
}))
.append(append::Stdout));

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
16 changes: 9 additions & 7 deletions examples/json_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
// limitations under the License.

use log::LevelFilter;
use logforth::append::{DispatchAppend, StdoutAppend};
use logforth::filter::LogLevelFilter;
use logforth::layout::SimpleJsonLayout;
use logforth::logger::Logger;

use logforth::logger::{Dispatch, Logger};
use logforth::{append, filter, layout};

fn main() {
let append = StdoutAppend::default().with_layout(SimpleJsonLayout);
let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace));
Logger::new().add_append(append).apply().unwrap();
Logger::new().dispatch(
Dispatch::new()
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleJson)
.append(append::Stdout),
);

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
12 changes: 6 additions & 6 deletions examples/no_color_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// limitations under the License.

use log::LevelFilter;
use logforth::append::{DispatchAppend, StdoutAppend};
use logforth::filter::LogLevelFilter;
use logforth::logger::Logger;
use logforth::{append, filter, layout};
use logforth::logger::{Dispatch, Logger};

fn main() {
let append = StdoutAppend::default();
let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace));
Logger::new().add_append(append).apply().unwrap();
Logger::new().dispatch(Dispatch::new()
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default())
.append(append::Stdout));

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
14 changes: 7 additions & 7 deletions examples/rolling_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
// limitations under the License.

use log::LevelFilter;
use logforth::append::{DispatchAppend, NonBlockingBuilder, RollingFileAppend, RollingFileWriter, Rotation};
use logforth::filter::LogLevelFilter;
use logforth::layout::SimpleJsonLayout;
use logforth::logger::Logger;
use logforth::{append, filter, layout};
use logforth::append::{NonBlockingBuilder, RollingFileWriter, Rotation};
use logforth::logger::{Dispatch, Logger};

fn main() {
let rolling = RollingFileWriter::builder()
Expand All @@ -28,9 +27,10 @@ fn main() {
.unwrap();
let (writer, _guard) = NonBlockingBuilder::default().finish(rolling);

let append = RollingFileAppend::new(writer).with_layout(SimpleJsonLayout);
let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace));
Logger::new().add_append(append).apply().unwrap();
Logger::new().dispatch(Dispatch::new()
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleJson)
.append(append::RollingFile::new(writer)));

let repeat = 1;

Expand Down
13 changes: 6 additions & 7 deletions examples/simple_stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
// limitations under the License.

use log::LevelFilter;
use logforth::append::{DispatchAppend, StdoutAppend};
use logforth::filter::LogLevelFilter;
use logforth::layout::SimpleTextLayout;
use logforth::logger::Logger;
use logforth::{append, filter, layout};
use logforth::logger::{Dispatch, Logger};

fn main() {
let append = StdoutAppend::default().with_layout(SimpleTextLayout::default());
let append = DispatchAppend::new(append).filter(LogLevelFilter::new(LevelFilter::Trace));
Logger::new().add_append(append).apply().unwrap();
Logger::new().dispatch(Dispatch::new()
.filter(filter::LogLevel::new(LevelFilter::Trace))
.layout(layout::SimpleText::default())
.append(append::Stdout));

log::error!("Hello error!");
log::warn!("Hello warn!");
Expand Down
12 changes: 6 additions & 6 deletions src/append/boxdyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ use log::Metadata;
use log::Record;
use crate::append::{Append, AppendImpl};

pub struct BoxDynAppend(Box<dyn Append + Send + Sync>);
pub struct BoxDyn(Box<dyn Append + Send + Sync>);

impl BoxDynAppend {
impl BoxDyn {
pub fn new(append: impl Append + Send + Sync + 'static) -> Self {
Self(Box::new(append))
}
}

impl Debug for BoxDynAppend {
impl Debug for BoxDyn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BoxDynAppend {{ ... }}")
}
}

impl Append for BoxDynAppend {
impl Append for BoxDyn {
fn enabled(&self, metadata: &Metadata) -> bool {
(*self.0).enabled(metadata)
}
Expand All @@ -46,8 +46,8 @@ impl Append for BoxDynAppend {
}
}

impl From<BoxDynAppend> for AppendImpl {
fn from(append: BoxDynAppend) -> Self {
impl From<BoxDyn> for AppendImpl {
fn from(append: BoxDyn) -> Self {
AppendImpl::BoxDyn(append)
}
}
12 changes: 6 additions & 6 deletions src/append/boxlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ use log::Metadata;
use log::Record;
use crate::append::{Append, AppendImpl};

pub struct BoxLogAppend(Box<dyn Log>);
pub struct BoxLog(Box<dyn Log>);

impl Debug for BoxLogAppend {
impl Debug for BoxLog {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BoxLogAppend {{ ... }}")
}
}

impl BoxLogAppend {
impl BoxLog {
pub fn new(log: impl Log + 'static) -> Self {
Self(Box::new(log))
}
}

impl Append for BoxLogAppend {
impl Append for BoxLog {
fn enabled(&self, metadata: &Metadata) -> bool {
(*self.0).enabled(metadata)
}
Expand All @@ -48,8 +48,8 @@ impl Append for BoxLogAppend {
}
}

impl From<BoxLogAppend> for AppendImpl {
fn from(append: BoxLogAppend) -> Self {
impl From<BoxLog> for AppendImpl {
fn from(append: BoxLog) -> Self {
AppendImpl::BoxLog(append)
}
}
8 changes: 4 additions & 4 deletions src/append/fastrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::append::{Append, AppendImpl};
use crate::layout::KvDisplay;

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

impl Append for FastraceAppend {
impl Append for Fastrace {
fn try_append(&self, record: &Record) -> anyhow::Result<()> {
let message = format!(
"{} {:>5} {}{}",
Expand All @@ -36,8 +36,8 @@ impl Append for FastraceAppend {
}
}

impl From<FastraceAppend> for AppendImpl {
fn from(append: FastraceAppend) -> Self {
impl From<Fastrace> for AppendImpl {
fn from(append: Fastrace) -> Self {
AppendImpl::Fastrace(append)
}
}
10 changes: 5 additions & 5 deletions src/append/file/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ use crate::append::file::non_blocking::NonBlocking;
use crate::append::{Append, AppendImpl};

#[derive(Debug)]
pub struct RollingFileAppend {
pub struct RollingFile {
writer: NonBlocking,
}

impl RollingFileAppend {
impl RollingFile {
pub fn new(writer: NonBlocking) -> Self {
Self { writer }
}
}

impl Append for RollingFileAppend {
impl Append for RollingFile {
fn try_append(&self, record: &Record) -> anyhow::Result<()> {
let bytes = format!("{}\n", record.args()).into_bytes();
self.writer.send(bytes)?;
Ok(())
}
}

impl From<RollingFileAppend> for AppendImpl {
fn from(append: RollingFileAppend) -> Self {
impl From<RollingFile> for AppendImpl {
fn from(append: RollingFile) -> Self {
AppendImpl::RollingFile(append)
}
}
2 changes: 1 addition & 1 deletion src/append/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub use append::RollingFileAppend;
pub use append::RollingFile;
pub use non_blocking::NonBlocking;
pub use non_blocking::NonBlockingBuilder;
pub use non_blocking::WorkerGuard;
Expand Down
18 changes: 10 additions & 8 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub use fastrace::*;
#[cfg(feature = "file")]
pub use file::*;
pub use stdio::*;
use crate::layout::{IdenticalLayout, LayoutImpl};

use crate::layout;
use crate::layout::LayoutImpl;

mod boxdyn;
mod boxlog;
Expand All @@ -44,20 +46,20 @@ pub trait Append {
/// Default layout to use when [Dispatch][crate::logger::Dispatch] does not configure a
/// preferred layout.
fn default_layout(&self) -> LayoutImpl {
LayoutImpl::Identical(IdenticalLayout)
LayoutImpl::Identical(layout::Identical)
}
}

#[derive(Debug)]
pub enum AppendImpl {
BoxDyn(BoxDynAppend),
BoxLog(BoxLogAppend),
BoxDyn(BoxDyn),
BoxLog(BoxLog),
#[cfg(feature = "fastrace")]
Fastrace(FastraceAppend),
Fastrace(Fastrace),
#[cfg(feature = "file")]
RollingFile(RollingFileAppend),
Stdout(StdoutAppend),
Stderr(StderrAppend),
RollingFile(RollingFile),
Stdout(Stdout),
Stderr(Stderr),
}

impl Append for AppendImpl {
Expand Down
16 changes: 8 additions & 8 deletions src/append/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::io::Write;
use crate::append::{Append, AppendImpl};

#[derive(Default, Debug)]
pub struct StdoutAppend;
pub struct Stdout;

impl Append for StdoutAppend {
impl Append for Stdout {
fn try_append(&self, record: &log::Record) -> anyhow::Result<()> {
let bytes = format!("{}\n", record.args()).into_bytes();
std::io::stdout().write_all(&bytes)?;
Expand All @@ -31,15 +31,15 @@ impl Append for StdoutAppend {
}
}

impl From<StdoutAppend> for AppendImpl {
fn from(append: StdoutAppend) -> Self {
impl From<Stdout> for AppendImpl {
fn from(append: Stdout) -> Self {
AppendImpl::Stdout(append)
}
}

#[derive(Default, Debug)]
pub struct StderrAppend;
impl Append for StderrAppend {
pub struct Stderr;
impl Append for Stderr {
fn try_append(&self, record: &log::Record) -> anyhow::Result<()> {
let bytes = format!("{}\n", record.args()).into_bytes();
std::io::stderr().write_all(&bytes)?;
Expand All @@ -51,8 +51,8 @@ impl Append for StderrAppend {
}
}

impl From<StderrAppend> for AppendImpl {
fn from(append: StderrAppend) -> Self {
impl From<Stderr> for AppendImpl {
fn from(append: Stderr) -> Self {
AppendImpl::Stderr(append)
}
}
Loading

0 comments on commit 045be6b

Please sign in to comment.