-
Notifications
You must be signed in to change notification settings - Fork 6
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
docs: add docs and some inplace fixes #21
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,15 @@ use crate::append::Append; | |
use crate::filter::Filter; | ||
use crate::filter::FilterResult; | ||
use crate::layout::Layout; | ||
|
||
/// A grouped set of appenders, filters, and optional layout. | ||
/// | ||
/// The [Logger] facade dispatches log records to one or more [Dispatch] instances. | ||
/// Each [Dispatch] instance contains a set of filters, appenders, and an optional layout. | ||
/// | ||
/// `filters` are used to determine whether a log record should be passed to the appenders. | ||
/// `appends` are used to write log records to a destination. Each appender has its own | ||
/// default layout. If the [Dispatch] has a layout, it will be used instead of the default layout. | ||
#[derive(Debug)] | ||
pub struct Dispatch<const LAYOUT: bool = true, const APPEND: bool = true> { | ||
filters: Vec<Filter>, | ||
|
@@ -36,6 +45,9 @@ impl Default for Dispatch<false, false> { | |
} | ||
|
||
impl Dispatch<false, false> { | ||
/// Create a new incomplete [Dispatch] instance. | ||
/// | ||
/// At least one append must be added to the [Dispatch] before it can be used. | ||
pub fn new() -> Dispatch<false, false> { | ||
Self { | ||
filters: vec![], | ||
|
@@ -44,11 +56,14 @@ impl Dispatch<false, false> { | |
} | ||
} | ||
|
||
/// Add a [Filter] to the [Dispatch]. | ||
pub fn filter(mut self, filter: impl Into<Filter>) -> Dispatch<false, false> { | ||
self.filters.push(filter.into()); | ||
self | ||
} | ||
|
||
/// Add the preferred [Layout] to the [Dispatch]. At most one layout can be added to a | ||
/// [Dispatch]. | ||
pub fn layout(self, layout: impl Into<Layout>) -> Dispatch<true, false> { | ||
Dispatch { | ||
filters: self.filters, | ||
|
@@ -59,10 +74,13 @@ impl Dispatch<false, false> { | |
} | ||
|
||
impl<const LAYOUT: bool, const APPEND: bool> Dispatch<LAYOUT, APPEND> { | ||
pub fn append(self, append: impl Append) -> Dispatch<true, true> { | ||
/// Add an [Append] to the [Dispatch]. | ||
pub fn append(mut self, append: impl Append) -> Dispatch<true, true> { | ||
self.appends.push(Box::new(append)); | ||
|
||
Dispatch { | ||
filters: self.filters, | ||
appends: vec![Box::new(append)], | ||
appends: self.appends, | ||
layout: self.layout, | ||
Comment on lines
+78
to
84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix a bug that previously |
||
} | ||
} | ||
|
@@ -83,7 +101,6 @@ impl Dispatch { | |
|
||
fn log(&self, record: &Record) -> anyhow::Result<()> { | ||
let layout = self.layout.as_ref(); | ||
|
||
for append in &self.appends { | ||
match layout { | ||
Some(layout) => layout.format(record, &|record| append.append(record))?, | ||
|
@@ -92,7 +109,6 @@ impl Dispatch { | |
.format(record, &|record| append.append(record))?, | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -103,37 +119,58 @@ impl Dispatch { | |
} | ||
} | ||
|
||
/// A logger facade that dispatches log records to one or more [Dispatch] instances. | ||
/// | ||
/// This struct implements [log::Log] to bridge Logforth's logging implementations | ||
/// with the [log] crate. | ||
#[derive(Debug)] | ||
pub struct Logger { | ||
pub struct Logger<const APPEND: bool = true> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hide |
||
dispatches: Vec<Dispatch>, | ||
} | ||
|
||
impl Default for Logger { | ||
impl Default for Logger<false> { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Logger { | ||
pub fn new() -> Self { | ||
impl Logger<false> { | ||
/// Create a new [Logger] instance. | ||
/// | ||
/// The [Logger] instance is incomplete and must have at least one [Dispatch] added to it. | ||
pub fn new() -> Logger<false> { | ||
Self { dispatches: vec![] } | ||
} | ||
} | ||
|
||
pub fn dispatch(mut self, dispatch: Dispatch) -> Self { | ||
self.dispatches.push(dispatch); | ||
self | ||
} | ||
|
||
impl Logger<true> { | ||
/// Set up the global logger with the [Logger] instance. | ||
/// | ||
/// # Errors | ||
/// | ||
/// An error is returned if the global logger has already been set. | ||
pub fn apply(self) -> Result<(), log::SetLoggerError> { | ||
log::set_boxed_logger(Box::new(self))?; | ||
log::set_max_level(LevelFilter::Trace); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<const APPEND: bool> Logger<APPEND> { | ||
/// Add a [Dispatch] to the [Logger]. | ||
pub fn dispatch(mut self, dispatch: Dispatch) -> Logger<true> { | ||
self.dispatches.push(dispatch); | ||
Logger { | ||
dispatches: self.dispatches, | ||
} | ||
} | ||
} | ||
|
||
impl log::Log for Logger { | ||
fn enabled(&self, _: &Metadata) -> bool { | ||
true | ||
fn enabled(&self, metadata: &Metadata) -> bool { | ||
self.dispatches | ||
.iter() | ||
.any(|dispatch| dispatch.enabled(metadata)) | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
|
@@ -153,6 +190,7 @@ impl log::Log for Logger { | |
} | ||
} | ||
|
||
// TODO(tisonkun): logback and log4j2 support custom error handling (status listener). | ||
fn handle_error(record: &Record, error: anyhow::Error) { | ||
let Err(fallback_error) = write!( | ||
std::io::stderr(), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lift the appender to the same level for docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can't hide
rolling_file::RollingFile
anyway?