Comments (12)
@rap2hpoutre looks very nice indeed! I would just suggest following the convention about the snipet form described in https://brson.github.io/rust-cookbook/about.html#a-note-about-error-handling and explained in my own PR ;) (https://github.com/brson/rust-cookbook/pull/53#issuecomment-300244571)
Imho this would be a separate example. Something along the lines of "Creating a custom logger".
from rust-cookbook.
@rap2hpoutre As discussed on https://github.com/brson/rust-cookbook/pull/53 the typical form is
// the real main with the whole setup and execution
fn run() -> Result<(), SetLoggerError> {
log::set_logger(|max_log_level| {
max_log_level.set(LogLevelFilter::Info);
Box::new(ConsoleLogger)
})?;
info!("hello log");
warn!("warning");
error!("oops");
Ok(())
}
// boilerplate
fn main() {
run().unwrap();
}
I think it's "Log a message to the console" (which covers)
I would argue that this example is more involved (and rich with important usecase) than minimal (~5 lines) one that would use env_logger
. But one of the maintainers would have to decide ;)
if it's almost OK, I can submit a PR then.
I would submit PR anyway as code review is much easier and does not spam the tracking issue ;)
from rust-cookbook.
I recall that sameone has posted interest in these examples on the libz-blitz thread but I would like to create PR later today for "Log to the Linux syslog" since I have a working example already
https://play.integer32.com/?gist=a21a9b6735f80d072ebea471840d2534&version=undefined
from rust-cookbook.
Here is an example I just wrote for a simple use case (console log):
#[macro_use]
extern crate log;
use log::{LogRecord, LogLevel, LogMetadata, LogLevelFilter};
struct ConsoleLogger;
impl log::Log for ConsoleLogger {
fn enabled(&self, metadata: &LogMetadata) -> bool {
metadata.level() <= LogLevel::Info
}
fn log(&self, record: &LogRecord) {
if self.enabled(record.metadata()) {
println!("{} - {}", record.level(), record.args());
}
}
}
fn main() {
log::set_logger(|max_log_level| {
max_log_level.set(LogLevelFilter::Info);
Box::new(ConsoleLogger)
})
.unwrap();
info!("hello log");
warn!("warning");
error!("oops");
}
Link to playground: http://play.integer32.com/?gist=80a6e5c21d710723a13a3817a6bb195c&version=undefined
Is this kind of example usefull? Did I missed something? Should I submit a PR?
from rust-cookbook.
@budziq Thanks a lot! Here is a rewritten example:
#[macro_use]
extern crate log;
use log::{LogRecord, LogLevel, LogMetadata, LogLevelFilter, SetLoggerError};
struct ConsoleLogger;
impl log::Log for ConsoleLogger {
fn enabled(&self, metadata: &LogMetadata) -> bool {
metadata.level() <= LogLevel::Info
}
fn log(&self, record: &LogRecord) {
if self.enabled(record.metadata()) {
println!("{} - {}", record.level(), record.args());
}
}
}
fn init_logger() -> Result<(), SetLoggerError> {
log::set_logger(|max_log_level| {
max_log_level.set(LogLevelFilter::Info);
Box::new(ConsoleLogger)
})?;
Ok(())
}
fn main() {
init_logger().unwrap();
info!("hello log");
warn!("warning");
error!("oops");
}
Is this what you suggested? Did I understand well?
Imho this would be a separate example. Something along the lines of "Creating a custom logger".
I think it's "Log various messages to the console" (which covers info, warning and error). if it's almost OK, I can submit a PR then.
from rust-cookbook.
I checked off "Log to the Unix syslog" in #70 and "Log messages with a custom logger" in #74.
from rust-cookbook.
Awesome. Thanks @budziq and @rap2hpoutre!
from rust-cookbook.
An exemple with a timestamp could be nice. Maybe with the custom logger.
from rust-cookbook.
Thanks @bbigras! I added it to the checklist. Would you be interested in writing that example and submitting a PR?
from rust-cookbook.
Would you be interested in writing that example and submitting a PR
Yes
from rust-cookbook.
I believe all the examples listed here have been implemented.
At the time of my writing those examples listed do not reflect they were merged :
Log a debug message to the console
Log an error message to the console
Log messages to a custom location
Include a timestamp on log messages
https://brson.github.io/rust-cookbook/app.html
from rust-cookbook.
You're right! Thanks for your help everybody.
from rust-cookbook.
Related Issues (20)
- Indexing a map with a non-Copy structural key
- Replace lazy_static with once_cell
- Text refers to "Expand" button, actually it's "Show hidden lines"
- The header for the bitfield page is incorrect
- querying-the-web-api examples don't work due to missing user-agent-headers
- `GUI` create‘s introduce and contrast
- Use clap-derive instead of raw clap for argument parsing HOT 1
- 3.1 Decompress a tarball while removing a prefix from the paths with E0107 HOT 1
- Some documents are unavailable
- Mime crate badges show CSV's version HOT 1
- Missing (hard to find?) edit link to github
- Add recipe to building custom format-like macros
- Is this repo abandoned? HOT 5
- Bug: Sample Code Doesn't Work
- replace crossbeam's scoped threads with std's scoped threads
- Dead links in Mathematics section
- rusqlite first example does not compile HOT 2
- clap-basic.md is stale, wrong
- Clap basic got a wrong demo
- update Log Messages/Configure Logging sections HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-cookbook.