-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add Debug implementation for AndroidLogger to facilitate use with log4rs #79
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![cfg(feature = "log4rs")] | ||
|
||
use android_logger::AndroidLogger; | ||
use log::{debug, error, info, trace, warn, LevelFilter}; | ||
use std::sync::OnceLock; | ||
|
||
#[test] | ||
fn test_log4rs() { | ||
use android_logger::Config as AndroidConfig; | ||
use log4rs::append::console::ConsoleAppender; | ||
use log4rs::config::{Appender, Root}; | ||
use log4rs::encode::pattern::PatternEncoder; | ||
use log4rs::Config; | ||
|
||
static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new(); | ||
let android_logger = ANDROID_LOGGER.get_or_init(|| { | ||
AndroidLogger::new(AndroidConfig::default().with_max_level(LevelFilter::Trace)) | ||
}); | ||
let stdout = ConsoleAppender::builder() | ||
.encoder(Box::new(PatternEncoder::new("{m}{n}"))) | ||
.build(); | ||
match Config::builder() | ||
.appender(Appender::builder().build("stdout", Box::new(stdout))) | ||
.appender(Appender::builder().build("android_logger", Box::new(android_logger))) | ||
.build( | ||
Root::builder() | ||
.appender("stdout") | ||
.appender("android_logger") | ||
.build(LevelFilter::Debug), | ||
) { | ||
Ok(config) => { | ||
let handle = log4rs::init_config(config); | ||
if let Err(e) = handle { | ||
println!("ERROR: failed to configure logging for stdout with {e:?}"); | ||
} | ||
} | ||
Err(e) => { | ||
println!("ERROR: failed to prepare default logging configuration with {e:?}"); | ||
} | ||
} | ||
// This will not be logged to the Console because of its category's custom level filter. | ||
info!(target: "Settings", "Info"); | ||
|
||
warn!(target: "Settings", "Warn"); | ||
error!(target: "Settings", "Error"); | ||
|
||
trace!("Trace"); | ||
debug!("Debug"); | ||
info!("Info"); | ||
warn!(target: "Database", "Warn"); | ||
error!("Error"); | ||
} |
Oops, something went wrong.
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.
derivative is not supported.
But even if supported, why use extract crate, when you can write just
5 lines of code?
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.
How about unconditionally (i.e. remove
feature = "log4rs"
- that's irrelevant) implementingDebug
onConfig
? It looks like all fields except the function can be formatted, so if we take some inspiration from thendk
crate you end up with a much simpler change.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.
Pushed changes so this is no longer feature gated and is simpler. Thanks for the suggestions. I was trying to leave the default builds as they were, but that's probably overkill here.