代码之家  ›  专栏  ›  技术社区  ›  Kabard

log4rs错误log4rs(OS代码:2,类型:NotFound,消息:“无此类文件或目录”)

  •  0
  • Kabard  · 技术社区  · 8 年前

    我试图通过以下方法实现log4rs the docs . 我的目标是把 info!("INFO") 进入文件requests.log,但我得到一个错误:

    线程'main'在调用' Result::unwrap() 关于一个 Err 值:log4rs(os code:2,kind:notfound,message:“no such file or 目录“”),libcore/result.rs:945:5

    我在SRC文件夹中有以下文件:

    - main.rs
    - log4rs.yml
    - requests.log
    

    主要风险:

    #[macro_use]
    extern crate log;
    extern crate log4rs;
    
    fn main() {
        println!("Hello, world!");
        log4rs::init_file("log4rs.yml", Default::default()).unwrap();
        info!("INFO");
    }
    

    配置文件log4rs.yml:

    # Scan this file for changes every 30 seconds
    refresh_rate: 30 seconds
    
    appenders:
      # An appender named "stdout" that writes to stdout
      stdout:
        kind: console
    
      # An appender named "requests" that writes to a file with a custom pattern encoder
      requests:
        kind: file
        path: "requests.log"
        encoder:
          pattern: "{d} - {m}{n}"
    
    # Set the default logging level to "warn" and attach the "stdout" appender to the root
    root:
      level: warn
      appenders:
        - stdout
    
    loggers:
      # Raise the maximum log level for events sent to the "app::backend::db" logger to "info"
      app::backend::db:
        level: info
    
      # Route log events sent to the "app::requests" logger to the "requests" appender,
      # and *not* the normal appenders installed at the root
      app::requests:
        level: info
        appenders:
          - requests
        additive: false
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Shepmaster Tim Diekmann    8 年前

    当你打字时 cargo run ,您的工作目录是当前目录。这意味着所有相对路径都将依赖于此工作目录。

    例如,如果您在主目录中( ~ )您的项目文件夹名为 foo . 当你进去的时候,这就给了你 ~/foo . 如果你现在打字 货物运输 也就是说当 log4rs 尝试打开您的文件。它将尝试打开该文件。 ~/foo/log4rs.yml . 文件不在这里,但在 ~/foo/src/log4rs.yml

    您有许多解决方案:

    • log4rs::init_file("src/log4rs.yml", Default::default()).unwrap();
    • 移动 log4rs.yml
    • 使用绝对路径(不是当前案例的好解决方案)
    推荐文章