0

I'm trying to logging something in pybind11 modules with spdlog.

I add a static variable log_initializer, it's constructor will initialize spdlog.

Here's the sample code:


#include <pybind11/pybind11.h>

PYBIND11_MODULE(my_module, m) {
  // def my module here
}

#include <fmt/format.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>


const int MAX_ROTATE_SIZE = 50 * 1024 * 1024;
const int MAX_ROTATE_FILES = 10;
const spdlog::level::level_enum LOG_LEVEL = spdlog::level::info;

struct ModuleInitializer {
  ModuleInitializer() {
    init_logger();
  }

  static void init_logger() {
    auto rotate_logger = spdlog::rotating_logger_mt(
        "my_module", "my_module.log", MAX_ROTATE_SIZE, MAX_ROTATE_FILES);
    spdlog::set_default_logger(rotate_logger);
    spdlog::set_level(LOG_LEVEL);
    spdlog::set_pattern("%Y-%m-%d %H:%M%S.%f | %l | p-%P t-%t | %!:%@ - %v");
    SPDLOG_DEBUG("logging initialized");
  }
};

static ModuleInitializer module_initializer;

When I use python so modules (creating by pybind11) in python3 console, the logging file is created by spdlog, not nothing actually written into the logging file.

Would you please help me ?

Then I try to open a file in pybind11 module and write something into it.

And this FILE *fp = fopen("my_module.log", "w"); fwrite("hello world!\n", 1, 13, fp); fclose(fp); will write successfully.

I don't understand which part is wrong about spdlog, and why it's not working.

linrongbin
  • 2,967
  • 6
  • 31
  • 59

1 Answers1

0

Come out with that, the issue why nothing written to log file, is because the logging level configuration in spdlog, not because of the pybind11/python modules.

linrongbin
  • 2,967
  • 6
  • 31
  • 59