Giter Club home page Giter Club logo

Comments (12)

budziq avatar budziq commented on September 24, 2024 1

@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.

budziq avatar budziq commented on September 24, 2024 1

@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.

budziq avatar budziq commented on September 24, 2024

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.

rap2hpoutre avatar rap2hpoutre commented on September 24, 2024

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.

rap2hpoutre avatar rap2hpoutre commented on September 24, 2024

@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.

dtolnay avatar dtolnay commented on September 24, 2024

I checked off "Log to the Unix syslog" in #70 and "Log messages with a custom logger" in #74.

from rust-cookbook.

brson avatar brson commented on September 24, 2024

Awesome. Thanks @budziq and @rap2hpoutre!

from rust-cookbook.

bbigras avatar bbigras commented on September 24, 2024

An exemple with a timestamp could be nice. Maybe with the custom logger.

from rust-cookbook.

dtolnay avatar dtolnay commented on September 24, 2024

Thanks @bbigras! I added it to the checklist. Would you be interested in writing that example and submitting a PR?

from rust-cookbook.

bbigras avatar bbigras commented on September 24, 2024

Would you be interested in writing that example and submitting a PR

Yes

from rust-cookbook.

meven avatar meven commented on September 24, 2024

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.

dtolnay avatar dtolnay commented on September 24, 2024

You're right! Thanks for your help everybody.

from rust-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.