refactor: Put log options in log section in config (#2440)

This commit is contained in:
Ayaz Salikhov
2025-08-18 15:22:33 +01:00
committed by GitHub
parent 8b1cab46e7
commit 4232359dce
27 changed files with 166 additions and 152 deletions

View File

@@ -40,7 +40,7 @@ struct MockSession : public web::SubscriptionContextInterface {
MOCK_METHOD(uint32_t, apiSubversion, (), (const, override));
util::TagDecoratorFactory tagDecoratorFactory{util::config::ClioConfigDefinition{
{"log_tag_style", util::config::ConfigValue{util::config::ConfigType::String}.defaultValue("none")}
{"log.tag_style", util::config::ConfigValue{util::config::ConfigType::String}.defaultValue("none")}
}};
MockSession() : web::SubscriptionContextInterface(tagDecoratorFactory)

View File

@@ -53,25 +53,25 @@ using util::config::ConfigValue;
struct LogServiceInitTests : virtual public ::testing::Test {
protected:
util::config::ClioConfigDefinition config_{
{"log_channels.[].channel", Array{ConfigValue{ConfigType::String}}},
{"log_channels.[].log_level", Array{ConfigValue{ConfigType::String}}},
{"log.channels.[].channel", Array{ConfigValue{ConfigType::String}}},
{"log.channels.[].level", Array{ConfigValue{ConfigType::String}}},
{"log_level", ConfigValue{ConfigType::String}.defaultValue("info")},
{"log.level", ConfigValue{ConfigType::String}.defaultValue("info")},
{"spdlog_format", ConfigValue{ConfigType::String}.defaultValue(R"(%Y-%m-%d %H:%M:%S.%f %^%3!l:%n%$ - %v)")},
{"spdlog_async", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
{"log.format", ConfigValue{ConfigType::String}.defaultValue(R"(%Y-%m-%d %H:%M:%S.%f %^%3!l:%n%$ - %v)")},
{"log.is_async", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
{"log_to_console", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
{"log.enable_console", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
{"log_directory", ConfigValue{ConfigType::String}.optional()},
{"log.directory", ConfigValue{ConfigType::String}.optional()},
{"log_rotation_size",
{"log.rotation_size",
ConfigValue{ConfigType::Integer}.defaultValue(2048).withConstraint(config::gValidateUint32)},
{"log_directory_max_files",
{"log.directory_max_files",
ConfigValue{ConfigType::Integer}.defaultValue(25).withConstraint(config::gValidateUint32)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("none")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("none")},
};
std::string
@@ -104,7 +104,8 @@ private:
TEST_F(LogServiceInitTests, DefaultLogLevel)
{
auto const parsingErrors = config_.parse(ConfigFileJson{boost::json::object{{"log_level", "warn"}}});
auto const parsingErrors =
config_.parse(ConfigFileJson{boost::json::object{{"log", boost::json::object{{"level", "warn"}}}}});
ASSERT_FALSE(parsingErrors.has_value());
std::string const logString = "some log";
@@ -134,13 +135,15 @@ TEST_F(LogServiceInitTests, ChannelLogLevel)
{
std::string const configStr = R"JSON(
{
"log_level": "error",
"log_channels": [
{
"channel": "Backend",
"log_level": "warning"
}
]
"log": {
"level": "error",
"channels": [
{
"channel": "Backend",
"level": "warning"
}
]
}
}
)JSON";
@@ -177,7 +180,8 @@ TEST_F(LogServiceInitTests, ChannelLogLevel)
TEST_F(LogServiceInitTests, InitReturnsErrorIfCouldNotCreateLogDirectory)
{
// "/proc" directory is read only on any unix OS
auto const parsingErrors = config_.parse(ConfigFileJson{boost::json::object{{"log_directory", "/proc/logs"}}});
auto const parsingErrors =
config_.parse(ConfigFileJson{boost::json::object{{"log", boost::json::object{{"directory", "/proc/logs"}}}}});
ASSERT_FALSE(parsingErrors.has_value());
auto const result = LogService::init(config_);
@@ -189,12 +193,14 @@ TEST_F(LogServiceInitTests, InitReturnsErrorIfProvidedInvalidChannel)
{
auto const jsonStr = R"JSON(
{
"log_channels": [
{
"channel": "SomeChannel",
"log_level": "warn"
}
]
"log": {
"channels": [
{
"channel": "SomeChannel",
"level": "warn"
}
]
}
})JSON";
auto const json = boost::json::parse(jsonStr).as_object();
@@ -208,7 +214,7 @@ TEST_F(LogServiceInitTests, InitReturnsErrorIfProvidedInvalidChannel)
TEST_F(LogServiceInitTests, LogSizeAndHourRotationCannotBeZero)
{
std::vector<std::string_view> const keys{"log_directory_max_files", "log_rotation_size"};
std::vector<std::string_view> const keys{"log.directory_max_files", "log.rotation_size"};
auto const jsonStr = fmt::format(
R"JSON(

View File

@@ -57,7 +57,7 @@ using namespace util::config;
struct WebHandlersTest : virtual NoLoggerFixture {
DOSGuardStrictMock dosGuardMock;
util::TagDecoratorFactory const tagFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
std::string const ip = "some ip";
StrictMockConnection connectionMock{ip, boost::beast::flat_buffer{}, tagFactory};

View File

@@ -57,7 +57,7 @@ protected:
std::shared_ptr<MockHandlerProvider> handlerProvider_ = std::make_shared<MockHandlerProvider>();
MockCounters counters_;
ClioConfigDefinition const config_{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("none")}};
ClioConfigDefinition const config_{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("none")}};
util::TagDecoratorFactory tagFactory_{config_};
rpc::impl::ForwardingProxy<MockCounters, MockHandlerProvider> proxy_{loadBalancer_, counters_, handlerProvider_};

View File

@@ -80,7 +80,7 @@ generateDefaultRPCEngineConfig()
{"workers", ConfigValue{ConfigType::Integer}.defaultValue(4).withConstraint(gValidateUint16)},
{"rpc.cache_timeout",
ConfigValue{ConfigType::Double}.defaultValue(0.0).withConstraint(gValidatePositiveDouble)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"dos_guard.whitelist.[]", Array{ConfigValue{ConfigType::String}.optional()}},
{"dos_guard.max_fetches",
ConfigValue{ConfigType::Integer}.defaultValue(1000'000u).withConstraint(gValidateUint32)},

View File

@@ -1299,7 +1299,7 @@ struct RPCHelpersLogDurationTest : LoggerFixture, testing::WithParamInterface<RP
}}
};
util::TagDecoratorFactory tagFactory{util::config::ClioConfigDefinition{
{"log_tag_style", util::config::ConfigValue{util::config::ConfigType::String}.defaultValue("none")}
{"log.tag_style", util::config::ConfigValue{util::config::ConfigType::String}.defaultValue("none")}
}};
struct DummyTaggable : util::Taggable {
DummyTaggable(util::TagDecoratorFactory& f) : util::Taggable(f)

View File

@@ -182,17 +182,17 @@ TEST_F(ConstraintTest, OneOfConstraintOneValue)
TEST_F(ConstraintTest, OneOfConstraint)
{
std::array<char const*, 3> const arr = {"123", "trace", "haha"};
auto const oneOfCons{OneOf{"log_level", arr}};
auto const oneOfCons{OneOf{"log.level", arr}};
EXPECT_FALSE(oneOfCons.checkConstraint("trace").has_value());
EXPECT_TRUE(oneOfCons.checkConstraint(345).has_value());
EXPECT_EQ(oneOfCons.checkConstraint(345)->error, R"(Key "log_level"'s value must be a string)");
EXPECT_EQ(oneOfCons.checkConstraint(345)->error, R"(Key "log.level"'s value must be a string)");
EXPECT_TRUE(oneOfCons.checkConstraint("PETER_WAS_HERE").has_value());
EXPECT_EQ(
oneOfCons.checkConstraint("PETER_WAS_HERE")->error,
R"(You provided value "PETER_WAS_HERE". Key "log_level"'s value must be one of the following: 123, trace, haha)"
R"(You provided value "PETER_WAS_HERE". Key "log.level"'s value must be one of the following: 123, trace, haha)"
);
}

View File

@@ -96,7 +96,7 @@ struct MockWsBase : public web::ConnectionBase {
struct WebRPCServerHandlerTest : util::prometheus::WithPrometheus, MockBackendTest, SyncAsioContextTest {
util::config::ClioConfigDefinition cfg{
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("none")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("none")},
{"api_version.default", ConfigValue{ConfigType::Integer}.defaultValue(rpc::kAPI_VERSION_DEFAULT)},
{"api_version.min", ConfigValue{ConfigType::Integer}.defaultValue(rpc::kAPI_VERSION_MIN)},
{"api_version.max", ConfigValue{ConfigType::Integer}.defaultValue(rpc::kAPI_VERSION_MAX)}

View File

@@ -125,7 +125,7 @@ getParseServerConfig(boost::json::value val)
{"server.admin_password", ConfigValue{ConfigType::String}.optional()},
{"server.local_admin", ConfigValue{ConfigType::Boolean}.optional()},
{"server.ws_max_sending_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(1500)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"dos_guard.max_fetches", ConfigValue{ConfigType::Integer}},
{"dos_guard.sweep_interval", ConfigValue{ConfigType::Integer}},
{"dos_guard.max_connections", ConfigValue{ConfigType::Integer}},
@@ -525,7 +525,7 @@ getParseAdminServerConfig(boost::json::value val)
{"ssl_key_file", ConfigValue{ConfigType::String}.optional()},
{"prometheus.enabled", ConfigValue{ConfigType::Boolean}.defaultValue(true)},
{"prometheus.compress_reply", ConfigValue{ConfigType::Boolean}.defaultValue(true)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}
};
auto const errors = config.parse(jsonVal);
[&]() { ASSERT_FALSE(errors.has_value()); }();

View File

@@ -38,7 +38,7 @@ using namespace util::config;
struct SubscriptionContextTests : NoLoggerFixture {
protected:
util::TagDecoratorFactory tagFactory_{ClioConfigDefinition{
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
}};
ConnectionBaseStrictMockPtr connection_ =
std::make_shared<testing::StrictMock<ConnectionBaseMock>>(tagFactory_, "some ip");

View File

@@ -44,7 +44,7 @@ using namespace util::config;
struct ErrorHandlingTests : NoLoggerFixture {
protected:
util::TagDecoratorFactory tagFactory_{ClioConfigDefinition{
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
}};
std::string const clientIp_ = "some ip";
ConnectionBaseStrictMockPtr connection_ =

View File

@@ -65,7 +65,7 @@ namespace http = boost::beast::http;
struct NgRpcServerHandlerTest : util::prometheus::WithPrometheus, MockBackendTestStrict, SyncAsioContextTest {
ClioConfigDefinition config{ClioConfigDefinition{
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"api_version.min", ConfigValue{ConfigType::Integer}.defaultValue(1)},
{"api_version.max", ConfigValue{ConfigType::Integer}.defaultValue(2)},
{"api_version.default", ConfigValue{ConfigType::Integer}.defaultValue(1)}

View File

@@ -139,7 +139,7 @@ TEST_F(ResponseTest, asConstBufferJson)
TEST_F(ResponseTest, createFromStringAndConnection)
{
util::TagDecoratorFactory const tagDecoratorFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
StrictMockConnection const connection{"some ip", boost::beast::flat_buffer{}, tagDecoratorFactory};
std::string const responseMessage = "response message";
@@ -158,7 +158,7 @@ TEST_F(ResponseTest, createFromStringAndConnection)
TEST_F(ResponseTest, createFromJsonAndConnection)
{
util::TagDecoratorFactory const tagDecoratorFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
StrictMockConnection const connection{"some ip", boost::beast::flat_buffer{}, tagDecoratorFactory};
boost::json::object const responseMessage{{"key", "value"}};

View File

@@ -87,7 +87,7 @@ TEST_P(MakeServerTest, Make)
{"server.processing_policy", ConfigValue{ConfigType::String}.defaultValue("parallel")},
{"server.parallel_requests_limit", ConfigValue{ConfigType::Integer}.optional()},
{"server.ws_max_sending_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(1500)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"ssl_cert_file", ConfigValue{ConfigType::String}.optional()},
{"ssl_key_file", ConfigValue{ConfigType::String}.optional()}
@@ -174,7 +174,7 @@ protected:
{"server.local_admin", ConfigValue{ConfigType::Boolean}.optional()},
{"server.parallel_requests_limit", ConfigValue{ConfigType::Integer}.optional()},
{"server.ws_max_sending_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(1500)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"ssl_key_file", ConfigValue{ConfigType::String}.optional()},
{"ssl_cert_file", ConfigValue{ConfigType::String}.optional()}
};
@@ -201,7 +201,7 @@ TEST_F(ServerTest, BadEndpoint)
{
boost::asio::ip::tcp::endpoint const endpoint{boost::asio::ip::make_address("1.2.3.4"), 0};
util::TagDecoratorFactory const tagDecoratorFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
Server server{
ctx_,
@@ -262,7 +262,7 @@ TEST_F(ServerHttpTest, OnConnectCheck)
auto const serverPort = tests::util::generateFreePort();
boost::asio::ip::tcp::endpoint const endpoint{boost::asio::ip::make_address("0.0.0.0"), serverPort};
util::TagDecoratorFactory const tagDecoratorFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
testing::StrictMock<testing::MockFunction<std::expected<void, Response>(Connection const&)>> onConnectCheck;
@@ -322,7 +322,7 @@ TEST_F(ServerHttpTest, OnConnectCheckFailed)
auto const serverPort = tests::util::generateFreePort();
boost::asio::ip::tcp::endpoint const endpoint{boost::asio::ip::make_address("0.0.0.0"), serverPort};
util::TagDecoratorFactory const tagDecoratorFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
testing::StrictMock<testing::MockFunction<std::expected<void, Response>(Connection const&)>> onConnectCheck;
@@ -381,7 +381,7 @@ TEST_F(ServerHttpTest, OnDisconnectHook)
auto const serverPort = tests::util::generateFreePort();
boost::asio::ip::tcp::endpoint const endpoint{boost::asio::ip::make_address("0.0.0.0"), serverPort};
util::TagDecoratorFactory const tagDecoratorFactory{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")}}
};
testing::StrictMock<testing::MockFunction<void(Connection const&)>> onDisconnectHookMock;

View File

@@ -53,7 +53,7 @@ struct NgSubscriptionContextTests : SyncAsioContextTest {
protected:
util::TagDecoratorFactory tagFactory_{ClioConfigDefinition{
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
}};
MockWsConnectionImpl connection_{"some ip", boost::beast::flat_buffer{}, tagFactory_};
testing::StrictMock<testing::MockFunction<bool(web::ng::Error const&, Connection const&)>> errorHandler_;

View File

@@ -67,7 +67,7 @@ namespace websocket = boost::beast::websocket;
struct ConnectionHandlerTest : prometheus::WithPrometheus, SyncAsioContextTest {
ConnectionHandlerTest(ProcessingPolicy policy, std::optional<size_t> maxParallelConnections)
: tagFactory{util::config::ClioConfigDefinition{
{"log_tag_style", config::ConfigValue{config::ConfigType::String}.defaultValue("uint")}
{"log.tag_style", config::ConfigValue{config::ConfigType::String}.defaultValue("uint")}
}}
, connectionHandler{policy, maxParallelConnections, tagFactory, std::nullopt, onDisconnectMock.AsStdFunction()}
{
@@ -103,7 +103,7 @@ struct ConnectionHandlerTest : prometheus::WithPrometheus, SyncAsioContextTest {
ConnectionHandler connectionHandler;
util::TagDecoratorFactory tagDecoratorFactory{config::ClioConfigDefinition{
{"log_tag_style", config::ConfigValue{config::ConfigType::String}.defaultValue("uint")}
{"log.tag_style", config::ConfigValue{config::ConfigType::String}.defaultValue("uint")}
}};
StrictMockHttpConnectionPtr mockHttpConnection =
std::make_unique<StrictMockHttpConnection>("1.2.3.4", beast::flat_buffer{}, tagDecoratorFactory);

View File

@@ -70,7 +70,7 @@ struct HttpConnectionTests : SyncAsioContextTest {
protected:
util::TagDecoratorFactory tagDecoratorFactory_{
ClioConfigDefinition{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("int")}}
ClioConfigDefinition{{"log.tag_style", ConfigValue{ConfigType::String}.defaultValue("int")}}
};
TestHttpServer httpServer_{ctx_, "localhost"};
HttpAsyncClient httpClient_{ctx_};

View File

@@ -85,7 +85,7 @@ struct WebWsConnectionTests : SyncAsioContextTest {
protected:
util::TagDecoratorFactory tagDecoratorFactory_{config::ClioConfigDefinition{
{"log_tag_style", config::ConfigValue{config::ConfigType::String}.defaultValue("int")}
{"log.tag_style", config::ConfigValue{config::ConfigType::String}.defaultValue("int")}
}};
TestHttpServer httpServer_{ctx_, "localhost"};
WebSocketAsyncClient wsClient_{ctx_};