log4rs错误Log4rs(Os {code:2,kind:NotFound,message:“No such file or directory”})

时间:2018-05-19 21:19:07

标签: rust

我正在尝试按照the docs实施log4rs。我的目标是将info!("INFO")的结果放入文件requests.log中,但是我收到错误:

  

线程'主'在'Result::unwrap()上被叫Err时惊慌失措   值:Log4rs(Os {代码:2,种类:NotFound,消息:“没有这样的文件或   目录“})',libcore / result.rs:945:5

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

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

main.rs:

#[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 个答案:

答案 0 :(得分:1)

键入optionsBuilder.UseLazyLoadingProxies()时,您的工作目录是当前目录。这意味着您的所有相对路径将取决于此工作目录。

例如,如果您在主目录(cargo run)中,并且项目文件夹名为~。当你进入它时,它会给你foo。如果您现在输入~/foo,则表示当cargo run尝试打开您的文件时,它会尝试打开文件log4rs。该文件不在此处,而是在~/foo/log4rs.yml

你有很多解决方案:

  • ~/foo/src/log4rs.yml
  • log4rs::init_file("src/log4rs.yml", Default::default()).unwrap();移至log4rs.yml
  • 使用绝对路径(对于您当前的情况不是一个好的解决方案)