Skip to content
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

Doesn't work with /examples folder #647

Open
setoelkahfi opened this issue Nov 28, 2024 · 5 comments
Open

Doesn't work with /examples folder #647

setoelkahfi opened this issue Nov 28, 2024 · 5 comments

Comments

@setoelkahfi
Copy link

setoelkahfi commented Nov 28, 2024

Hello, this log doesn't seem to work with the Cargo examples directory. Is it by design?

I have a basic.rs file inside my examples folder crate. This doesn't print the log information:

use std::env;
use log::{debug, error, info};

#[tokio::main]
async fn main() {
  debug!("Try to run the example");
  // Some other lines...

This one does:

use std::env;
use log::{debug, error, info};

#[tokio::main]
async fn main() {
  println!("Try to run the example");
  // Some other lines...

I ran that example with: cargo run --example basic

@Thomasdezeeuw
Copy link
Collaborator

@setoelkahfi the log crate only provides a logging facade, you'll also need an implementation that does the actual logging. See one of the implementation in https://github.com/rust-lang/log?tab=readme-ov-file#in-executables.

@setoelkahfi
Copy link
Author

@Thomasdezeeuw I'm building a library. And per the readme, I should only link the log crate.

@Thomasdezeeuw
Copy link
Collaborator

@Thomasdezeeuw I'm building a library. And per the readme, I should only link the log crate.

Yes. Then you can add an implementation as dev-dependency (https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies), which you can use in the examples, but it will not get added to your library.

@setoelkahfi
Copy link
Author

setoelkahfi commented Nov 28, 2024

I don't think I get this. I tried to use the env_logger one, for example, I tried to init it in my lib.rs file, but it says Syntax Error: expected BANGrust-analyzer[syntax-error](https://doc.rust-lang.org/stable/reference/)

use crate::model::{ErrorDetail, ErrorResponseCode, Link, SessionError};
use log::{debug, info};
use reqwest::{Client, Response, StatusCode};

env_logger::init(); // Can't do

Do I need to specify a specific feature?

@Thomasdezeeuw
Copy link
Collaborator

Thomasdezeeuw commented Nov 28, 2024

In Rust you can't put a statement like that at the top level of the source code.

You can put in the main function of your example. For example:

#[tokio::main]
async fn main() {
    env_logger::init();
    log::info!("It worked");
}

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