Skip to content

changing target to stdout did not take effect? #108

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

Closed
sallyyu0 opened this issue Nov 2, 2018 · 4 comments
Closed

changing target to stdout did not take effect? #108

sallyyu0 opened this issue Nov 2, 2018 · 4 comments

Comments

@sallyyu0
Copy link

sallyyu0 commented Nov 2, 2018

Hello,

I'm changing println! to info! when using the env_logger and trying to change target to stdout. However it does not seem to make any different. All logs still go to console. Is not it supposed to be different?

Cargo.toml:

[dependencies]
log = "0.4"
env_logger = "0.5"

lib.rs:

extern crate log;
extern crate env_logger;.

logger.rs
extern crate log;
extern crate env_logger;

use std::env;
use log::*;
use self::env_logger::{Builder, Target};

::std::env::set_var("RUST_LOG", "info");

println!("env RUST_LOG as {:?}", env::var("RUST_LOG"));

let mut builder = Builder::new();
builder.target(Target::Stdout);
if env::var("RUST_LOG").is_ok() {
    builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.init();
info!("builder={:?}", builder);

Note that I did get following error and not sure why So I made change as instructed by adding "self", it passed.

8 | use env_logger::{Builder, Target};
| ^^^^^^^^^^ Did you mean self::env_logger?

==========================
Here is the output of cargo run as well as docker run:

env RUST_LOG as Ok("info")
INFO 2018-11-02T19:17:37Z: myapp::utils::logger: builder=Logger { filter: Filter { filter: None, directives: [] }, writer: Logger { target: Stdout, write_style: Auto } }

@KodrAus
Copy link
Collaborator

KodrAus commented Nov 5, 2018

Hi @sallyyu0 👋

There are a few things going on here.

All logs still go to console. Is not it supposed to be different?

Your logs should end up being written to stdout and appear in your terminal. Is the problem that you're running cargo test and your logs are suddenly showing up there instead of being captured like println! is?

Note that I did get following error and not sure why So I made change as instructed by adding "self", it passed.

This one is Rust itself being picky. Usually you only put extern crate ..; statements in your root lib.rs or main.rs. When you do that you can just use ..; in other modules like logger.rs.

INFO 2018-11-02T19:17:37Z: myapp::utils::logger: builder=Logger { filter: Filter { filter: None, directives: [] }, writer: Logger { target: Stdout, write_style: Auto } }

This one is our bad. That Logger { .. } is actually lying about its fields. After calling builder.init() we replace those fields with default values so it doesn't tell you what the Builder looked like before. I opened #110 to fix that up.

@sallyyu0
Copy link
Author

sallyyu0 commented Nov 5, 2018

Hi KodrAus,
Thanks for the response. I was hoping a way NOT to print the logs to the terminal instead to a file, or a server even though I see there are only two options, stderr and stdout.

About the build error: When I don't have "extern crate env_logger;", I get this error instead.
error[E0658]: access to extern crates through prelude is experimental (see issue #44660)

@KodrAus
Copy link
Collaborator

KodrAus commented Nov 6, 2018

I was hoping a way NOT to print the logs to the terminal instead to a file, or a server

Ah right. Yeh, env_logger only supports logging to a terminal. If you'd like to send your logs elsewhere you could take a look at log4rs which has rolling file support.

access to extern crates through prelude is experimental

I think this is another picky modules error. You shouldn't need the extern crate env_logger; statement in your logger.rs file, but you might instead need a use env_logger; statement. These are nasty little papercuts that are cleaned up in a new edition of the language. If you transition your project to the current 2018 edition then you shouldn't need any extern crate env_logger or use env_logger; statements at all.

@KodrAus
Copy link
Collaborator

KodrAus commented Nov 12, 2018

It looks we've worked through your original concerns @sallyyu0 so I'll go ahead and close this one now. Please feel free to re-open it or start a new issue if there's anything else you'd like to clarify!

@KodrAus KodrAus closed this as completed Nov 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants