【问题标题】:log4rs error Log4rs(Os { code: 2, kind: NotFound, message: "No such file or directory" })log4rs error Log4rs(Os { code: 2, kind: NotFound, message: "No such file or directory" })
【发布时间】:2018-05-19 21:19:06
【问题描述】:

我正在尝试通过关注the docs 来实现 log4rs。我的目标是将info!("INFO") 的结果放入文件requests.log 中,但出现错误:

线程'main'在Err上的'称为Result::unwrap()时惊慌失措 value: Log4rs(Os { code: 2, kind: NotFound, message: "No such file or 目录" })', 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

【问题讨论】:

    标签: rust


    【解决方案1】:

    当您键入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 移动到foo
    • 使用绝对路径(对于您当前的情况不是一个好的解决方案)

    【讨论】:

      猜你喜欢
      • 2017-02-08
      • 1970-01-01
      • 2022-07-11
      • 2012-06-03
      • 1970-01-01
      • 2019-07-23
      • 2023-04-03
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多