【问题标题】:Logging with spdlog (C++), logger is not writing log to file使用 spdlog (C++) 记录,记录器未将日志写入文件
【发布时间】:2019-11-24 11:26:03
【问题描述】:

我正在尝试在使用 google test 执行测试时将基本信息记录在文本文件中。我的最终目标是记录异常的跟踪。

在我使用 C++11 的项目中,最新版本的 Google 测试通过 CMake 和 spdlog 作为仅标头添加到项目中(添加到项目内的库中)。

由于某种原因,记录器没有写入文件,即使在尝试强制刷新之后也是如此。我已经尝试了来自互联网的不同“快速入门”,但没有什么对我有用。我不认为这可能是一个问题,但一个假设是 您不能在测试的上下文中写入文件。项目结构如下:

.
|
├── build
├── cmake
├── libs
|   ├── ...
|   └── spdlog (*)
├── src
|   ├── ...
|   ├── main.cpp
|   └── CMakeLists.txt
├── test
|   ├── ...
|   ├── CMakeLists.txt
|   └── core
|       └── logging
|           ├── log
|           |   └── logger.txt
|           └── logging_test.cpp
├── ...
└── CMakeLists.txt 

(*) 目录中的文件是 spdlog/include/spdlog https://github.com/gabime/spdlog/tree/v1.x/include/spdlog中的文件

这是测试类中的代码 logging_test.cpp。运行测试check_exception_thrown_equal 后,没有任何内容写入logger.txt。可能是什么问题?

#include <exception>
#include "gtest/gtest.h"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>


class LoggingTest: public testing::Test {

 protected:
  std::shared_ptr<spdlog::logger> logger;

  struct ExceptionTemplate : std::exception {
    const char* what() { return "ExceptionTemplate triggered!"; }
  };

 // create logger 
 void create_logger() {
   // Create basic file logger (not rotated)
   logger = spdlog::basic_logger_mt("logger", "log/logger.txt");
 }

  // setup logger configurations 
  void set_up() {
    logger->set_level(spdlog::level::info);
    logger->flush_on(spdlog::level::info);
    logger->info("Debug logger setup done. \n");
  }

  // critical method that generates and returns my exception of type ExceptionTemplate
  ExceptionTemplate exception_generator() {
    try {

      ///////////////////////////////
      // call to critical method here
      ///////////////////////////////

      throw ExceptionTemplate();
    }
    catch (ExceptionTemplate &e) {
      log_exception(e);
      return e;
    }
  }

  // write to logger 
  void log_exception(ExceptionTemplate e) {
    try {

      LoggingTest::create_logger();
      LoggingTest::set_up();

      logger->info("Exception raised! {}", e.what());
    }
    catch (const spdlog::spdlog_ex &ex) {
      std::cout << "Log initialization failed: " << ex.what() << std::endl;
    }
  }
};


TEST_F(LoggingTest, check_exception_thrown_equal) {
  ASSERT_STREQ(LoggingTest::exception_generator().what(), "ExceptionTemplate triggered!");
}

【问题讨论】:

    标签: c++ googletest spdlog


    【解决方案1】:

    尝试使用更简单的设置,你不需要所有的“帮助”函数等,或者一开始就需要与异常相关的东西。只需在调用类的构造函数时记录一条消息。

    class LoggingTest {
      LoggingTest() {
        auto logger = spdlog::basic_logger_mt("test_logger", "logs/test.txt");
        spdlog::set_default_logger(logger);
        spdlog::flush_on(spdlog::level::info);
    
        spdlog::get("test_logger")->info("LoggingTest::ctor");
      }
    }
    

    然后只需在您的main(或其他任何地方)中创建该类的实例。确保该目录存在且可写(尽管此 usu. 会导致错误,而不是静默失败)。

    【讨论】:

    • 我的只是因为找不到文件而抛出异常。没有相应的文件和目录。 spdlog不应该自己创建文件吗?
    • 它将创建日志文件,但不会创建任何目录。因此,如果指定logs/test.txt 作为路径,则必须确保logs/ 存在。
    • 事实证明我的问题是多个问题。定义记录器时的wstring和string,相对路径不存在,文件夹不存在等
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 2022-12-29
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多