I want to configure the logger in another file, but when I do this, the logger doesn't add anything to this file. Why? For simplicity I got the following example from the library documentation(the function body).
*.cpp
namespace logger
{
void LoggerConfigurations(data::DataManager& data_manager, Loggers logger_type)
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::warn);
console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("Staff/Logs/multisink.txt");
file_sink->set_level(spdlog::level::trace);
spdlog::logger logger("multi_sink", {console_sink, file_sink});
logger.set_level(spdlog::level::debug);
logger.warn("this should appear in both console and file");
logger.info("this message should not appear in the console, only in the file");
spdlog::set_default_logger(std::make_shared<spdlog::logger>("multi_sink", spdlog::sinks_init_list({console_sink, file_sink})));
}
}
*.hpp
#pragma once
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/ostream_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/spdlog.h>
namespace logger{
void LoggerConfigurations(data::DataManager& data_manager, Loggers logger_type);
}
I'm more interested in the fact that when I use:
auto file = std::make_shared<std::ofstream>(path, std::ios_base::app);
auto ostream_sink = std::make_shared<spdlog::sinks::ostream_sink_mt> (*file);
ostream_sink->set_level(spdlog::level::info);
ostream_sink->set_pattern("] %v ");
It is working. But ... when I save the file in data_manager (just add one to std::vector<FilePtr>
), it stops working))
(All this is only for the case when I configure it in another * .cpp not in main.cpp, and the file was always created)