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

Cookbook ideas for log crate #61

Closed
brson opened this Issue May 10, 2017 · 12 comments

Comments

Projects
None yet
6 participants
@brson
Contributor

brson commented May 10, 2017

Write examples for these use cases following the existing precedent and guidelines in CONTRIBUTING.md

Crate evaluation for log: See https://internals.rust-lang.org/t/crate-evaluation-for-2017-05-16-log/5185

Suggest ideas for other examples here if you have them!

@budziq

This comment has been minimized.

Show comment
Hide comment
@budziq

budziq May 11, 2017

Collaborator

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

Collaborator

budziq commented May 11, 2017

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

@rap2hpoutre

This comment has been minimized.

Show comment
Hide comment
@rap2hpoutre

rap2hpoutre May 11, 2017

Contributor

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?

Contributor

rap2hpoutre commented May 11, 2017

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?

@budziq

This comment has been minimized.

Show comment
Hide comment
@budziq

budziq May 11, 2017

Collaborator

@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 ;) (#53 (comment))

Imho this would be a separate example. Something along the lines of "Creating a custom logger".

Collaborator

budziq commented May 11, 2017

@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 ;) (#53 (comment))

Imho this would be a separate example. Something along the lines of "Creating a custom logger".

@rap2hpoutre

This comment has been minimized.

Show comment
Hide comment
@rap2hpoutre

rap2hpoutre May 11, 2017

Contributor

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

Contributor

rap2hpoutre commented May 11, 2017

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

@budziq

This comment has been minimized.

Show comment
Hide comment
@budziq

budziq May 11, 2017

Collaborator

@rap2hpoutre As discussed on #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 ;)

Collaborator

budziq commented May 11, 2017

@rap2hpoutre As discussed on #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 ;)

@dtolnay

This comment has been minimized.

Show comment
Hide comment
@dtolnay

dtolnay May 12, 2017

Member

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

Member

dtolnay commented May 12, 2017

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

@brson

This comment has been minimized.

Show comment
Hide comment
@brson

brson May 13, 2017

Contributor

Awesome. Thanks @budziq and @rap2hpoutre!

Contributor

brson commented May 13, 2017

Awesome. Thanks @budziq and @rap2hpoutre!

@bbigras

This comment has been minimized.

Show comment
Hide comment
@bbigras

bbigras May 15, 2017

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

bbigras commented May 15, 2017

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

@dtolnay

This comment has been minimized.

Show comment
Hide comment
@dtolnay

dtolnay May 15, 2017

Member

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

Member

dtolnay commented May 15, 2017

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

@bbigras

This comment has been minimized.

Show comment
Hide comment
@bbigras

bbigras May 15, 2017

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

Yes

bbigras commented May 15, 2017

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

Yes

@dtolnay dtolnay changed the title from Examples for 'log' crate to Cookbook ideas for log crate May 17, 2017

@meven

This comment has been minimized.

Show comment
Hide comment
@meven

meven May 24, 2017

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

meven commented May 24, 2017

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

@dtolnay

This comment has been minimized.

Show comment
Hide comment
@dtolnay

dtolnay May 24, 2017

Member

You're right! Thanks for your help everybody.

Member

dtolnay commented May 24, 2017

You're right! Thanks for your help everybody.

@dtolnay dtolnay closed this May 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment