3

I am getting a compiler error trying to use the log crate in a package in a workspace. The other crates in the workspace are using logging without problem.

Cargo.toml:

[dependencies]
log = "^0"
rocket = "^0"

[dependencies.uuid]
version = "^0"
features = ["v4"]

lib.rs:

#![feature(proc_macro_hygiene, decl_macro)]

use rocket::{
    Request, 
    Data, 
    Response
};

use rocket::request::{
    self, 
    FromRequest,
    Outcome
};

use rocket::fairing::{
    Fairing, 
    Info, 
    Kind
};

use uuid::Uuid;

use std::fmt;

use log;



pub struct LoggerFairing {
    service_name: &'static str
}


impl LoggerFairing {
    pub fn new(service_name: &'static str) -> Self {
        LoggerFairing {
            service_name
        }
    }
}


impl Fairing for LoggerFairing {
    fn info(&self) -> Info {
        Info {
            name: self.service_name,
            kind: Kind::Request | Kind::Response
        }
    }


    fn on_request(&self, req: &mut Request, _: &Data) {
        let ip_addr = req.client_ip().map(|addr| addr.to_string())
            .unwrap_or("IP Address unknown".to_string());

        let method = req.method();

        let url = req.uri();

        let request_id = get_request_id(req);

        log::info!("request {:?} from {}: {} {}", request_id, ip_addr, method, url);
    }


    fn on_response(&self, req: &Request, res: &mut Response) {
        let request_id = get_request_id(req);

        let status = res.status();

        log::info!("request {:?} responded with {}", request_id, status);
    }
}


fn get_request_id<'t, 'r>(req: &'t Request<'r>) -> Option<RequestId<'t>> {
    match req.guard::<RequestId>() {
        Outcome::Success(request_id) => Some(request_id),
        _ => None
    }
}



pub struct RequestId<'t> {
    pub id: &'t Uuid
}    


impl<'t, 'r> FromRequest<'t, 'r> for RequestId<'t> {
    type Error = ();
    
    fn from_request(req: &'t Request<'r>) -> request::Outcome<Self, Self::Error> {
        let id = req.local_cache(|| Uuid::new_v4());
        
        request::Outcome::Success(RequestId {
            id
        })
    }
}


impl<'t> fmt::Display for RequestId<'t> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.id)
    }
}

The error message:

error: cannot find macro `log` in this scope
  --> utils\logging\src\lib.rs:62:9
   |
62 |         log::info!("request {:?} from {}: {} {}", request_id, ip_addr, method, url);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot find macro `log` in this scope
  --> utils\logging\src\lib.rs:71:9
   |
71 |         log::info!("request {:?} responded with {}", request_id, status);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

error: could not compile `logging`.

I've used several variations of the use log statement and how I'm calling the info! macro, but they all cause the same error message. I've tried specifying exact versions in Cargo.toml.

I'm stumped. This is exactly how I'm using log in other crates.

Jeff Ruby
  • 51
  • 1
  • 4
  • `log = "^0"` this is about the least useful way of specifying dependency versions. – Shepmaster Aug 18 '20 at 18:00
  • At the very least, you need to tell us what versions you are using. Please [edit] your question to include the exact versions, ideally by reflecting that in the Cargo.toml. – Shepmaster Aug 18 '20 at 18:02
  • This code does not produce the errors you are asking about. This only states *`std::option::Option<&uuid::Uuid>` cannot be formatted with the default formatter*. – Shepmaster Aug 18 '20 at 18:04
  • If you are using Rocket, that also means you are using nightly Rust. You need to specify exactly which version of Rust. – Shepmaster Aug 18 '20 at 18:05
  • *`RequestId<'_>` doesn't implement `std::fmt::Debug`*. **Please** ensure that the code you are posting produces the error you are experiencing. – Shepmaster Aug 18 '20 at 18:16
  • It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a **brand new Cargo project**, then [edit] your question to include the additional info. If it doesn't reproduce there, you will need to minimize your workspace to the basics. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Aug 18 '20 at 18:18

1 Answers1

1

Specifying the exact version of the log crate (0.4.11) in Cargo.toml fixes this problem.

I'm assuming there is something funny happening when Cargo tries to resolve the dependencies.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jeff Ruby
  • 51
  • 1
  • 4