简体   繁体   English

将日志与 Rocket 一起使用时,在此 scope 中找不到宏“日志”

[英]Cannot find macro `log` in this scope when using log with Rocket

I am getting a compiler error trying to use the log crate in a package in a workspace.我在尝试使用工作区 package 中的日志箱时遇到编译器错误。 The other crates in the workspace are using logging without problem.工作区中的其他板条箱正在毫无问题地使用日志记录。

Cargo.toml:货物.toml:

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

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

lib.rs:库.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!我已经使用了use log语句的几种变体以及我如何调用info! macro, but they all cause the same error message.宏,但它们都会导致相同的错误消息。 I've tried specifying exact versions in Cargo.toml.我试过在 Cargo.toml 中指定确切的版本。

I'm stumped.我很难过。 This is exactly how I'm using log in other crates.这正是我在其他板条箱中使用登录的方式。

Specifying the exact version of the log crate (0.4.11) in Cargo.toml fixes this problem.在 Cargo.toml 中指定日志箱 (0.4.11) 的确切版本可以修复此问题。

I'm assuming there is something funny happening when Cargo tries to resolve the dependencies.我假设当 Cargo 试图解决依赖关系时会发生一些有趣的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM