mirror of
https://github.com/XRPLF/clio.git
synced 2026-06-03 00:36:44 +00:00
feat: Optional log rotation (#3016)
This PR adds an option to disable log rotation.
This commit is contained in:
@@ -422,6 +422,8 @@ getClioConfig()
|
||||
{"log.directory_max_files",
|
||||
ConfigValue{ConfigType::Integer}.defaultValue(25).withConstraint(gValidateUint32)},
|
||||
|
||||
{"log.rotate", ConfigValue{ConfigType::Boolean}.defaultValue(true)},
|
||||
|
||||
{"log.tag_style",
|
||||
ConfigValue{ConfigType::String}.defaultValue("none").withConstraint(gValidateLogTag)},
|
||||
|
||||
|
||||
@@ -357,6 +357,10 @@ Documentation can be found at: <https://github.com/gabime/spdlog/wiki/Custom-for
|
||||
"file starts."},
|
||||
KV{.key = "log.directory_max_files",
|
||||
.value = "The maximum number of log files in the directory."},
|
||||
KV{.key = "log.rotate",
|
||||
.value = "Enables or disables log file rotation. When disabled, a single log file is "
|
||||
"used without size-based rotation. Useful when rotation is managed externally "
|
||||
"(e.g., via logrotate)."},
|
||||
KV{.key = "log.tag_style",
|
||||
.value = "Log tags are unique identifiers for log messages. `uint`/`int` starts logging "
|
||||
"from 0 and increments, "
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <spdlog/formatter.h>
|
||||
#include <spdlog/logger.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -184,12 +185,18 @@ spdlog::sink_ptr
|
||||
LogService::createFileSink(FileLoggingParams const& params, std::string const& format)
|
||||
{
|
||||
std::filesystem::path const dirPath(params.logDir);
|
||||
// the below are taken from user in MB, but spdlog needs it to be in bytes
|
||||
auto const rotationSize = mbToBytes(params.rotationSizeMB);
|
||||
auto fileSink = [&]() -> std::shared_ptr<spdlog::sinks::sink> {
|
||||
auto const logPath = (dirPath / "clio.log").string();
|
||||
if (params.rotation.has_value()) {
|
||||
// rotation sizes are taken from user in MB, but spdlog needs bytes
|
||||
auto const rotationSize = mbToBytes(params.rotation->sizeMB);
|
||||
return std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
|
||||
logPath, rotationSize, params.rotation->maxFiles
|
||||
);
|
||||
}
|
||||
return std::make_shared<spdlog::sinks::basic_file_sink_mt>(logPath, /*truncate=*/false);
|
||||
}();
|
||||
|
||||
auto fileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
|
||||
(dirPath / "clio.log").string(), rotationSize, params.dirMaxFiles
|
||||
);
|
||||
fileSink->set_level(spdlog::level::trace);
|
||||
fileSink->set_formatter(std::make_unique<spdlog::pattern_formatter>(format));
|
||||
|
||||
@@ -336,10 +343,17 @@ LogService::getSinks(config::ClioConfigDefinition const& config)
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<RotationParams> rotation = std::nullopt;
|
||||
if (config.get<bool>("log.rotate")) {
|
||||
rotation = RotationParams{
|
||||
.sizeMB = config.get<uint32_t>("log.rotation_size"),
|
||||
.maxFiles = config.get<uint32_t>("log.directory_max_files"),
|
||||
};
|
||||
}
|
||||
|
||||
FileLoggingParams const params{
|
||||
.logDir = logDir.value(),
|
||||
.rotationSizeMB = config.get<uint32_t>("log.rotation_size"),
|
||||
.dirMaxFiles = config.get<uint32_t>("log.directory_max_files"),
|
||||
.rotation = rotation,
|
||||
};
|
||||
allSinks.push_back(createFileSink(params, format));
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ class sink; // NOLINT(readability-identifier-naming)
|
||||
struct BenchmarkLoggingInitializer;
|
||||
class LoggerFixture;
|
||||
struct LogServiceInitTests;
|
||||
struct LogFileRotationTests;
|
||||
|
||||
namespace util {
|
||||
|
||||
@@ -229,6 +230,7 @@ class LogServiceState {
|
||||
protected:
|
||||
friend struct ::LogServiceInitTests;
|
||||
friend class ::LoggerFixture;
|
||||
friend struct ::LogFileRotationTests;
|
||||
friend class Logger;
|
||||
friend class ::util::impl::OnAssert;
|
||||
|
||||
@@ -388,11 +390,14 @@ private:
|
||||
expected<std::vector<std::shared_ptr<spdlog::sinks::sink>>, std::string>
|
||||
getSinks(config::ClioConfigDefinition const& config);
|
||||
|
||||
struct RotationParams {
|
||||
uint32_t sizeMB;
|
||||
uint32_t maxFiles;
|
||||
};
|
||||
|
||||
struct FileLoggingParams {
|
||||
std::string logDir;
|
||||
|
||||
uint32_t rotationSizeMB;
|
||||
uint32_t dirMaxFiles;
|
||||
std::optional<RotationParams> rotation; ///< nullopt when rotation is disabled
|
||||
};
|
||||
|
||||
friend struct ::BenchmarkLoggingInitializer;
|
||||
|
||||
Reference in New Issue
Block a user