diff --git a/api/envoy/config/bootstrap/v3/bootstrap.proto b/api/envoy/config/bootstrap/v3/bootstrap.proto index f171068aaeedc..43e3a33a3f158 100644 --- a/api/envoy/config/bootstrap/v3/bootstrap.proto +++ b/api/envoy/config/bootstrap/v3/bootstrap.proto @@ -110,6 +110,11 @@ message Bootstrap { // support all the format flags specified in the :option:`--log-format` // command line options section, except for the ``%v`` and ``%_`` flags. google.protobuf.Struct json_format = 1; + + // Flush application log in a format defined by a string. The text format + // can support all the format flags specified in the :option:`--log-format` + // command line option section. + string text_format = 2; } } diff --git a/changelogs/current.yaml b/changelogs/current.yaml index 123aa4ab1f556..ee520e6999ced 100644 --- a/changelogs/current.yaml +++ b/changelogs/current.yaml @@ -292,6 +292,11 @@ new_features: Added bootstrap option :ref:`application_log_format ` to enable setting application log format as JSON structure. +- area: application_logs + change: | + Added bootstrap option + :ref:`application_log_format ` + to enable setting application log text format from config. - area: ext_proc change: | added new field ``filter_metadata getAllConfigFiles() { + setupTestDirectory(); + return {"text_application_logs.yaml"}; + } +}; + TEST_P(ValidationServerTest, Validate) { EXPECT_TRUE(validateConfig(options_, Network::Address::InstanceConstSharedPtr(), component_factory_, Thread::threadFactoryForTest(), @@ -324,6 +341,30 @@ INSTANTIATE_TEST_SUITE_P( ::testing::ValuesIn( JsonApplicationLogsValidationServerForbiddenFlag_Test::getAllConfigFiles())); +TEST_P(TextApplicationLogsValidationServerTest, TextApplicationLogs) { + Thread::MutexBasicLockable access_log_lock; + Stats::IsolatedStoreImpl stats_store; + DangerousDeprecatedTestTime time_system; + ValidationInstance server(options_, time_system.timeSystem(), + Network::Address::InstanceConstSharedPtr(), stats_store, + access_log_lock, component_factory_, Thread::threadFactoryForTest(), + Filesystem::fileSystemForTest()); + + Envoy::Logger::Registry::setLogLevel(spdlog::level::info); + MockLogSink sink(Envoy::Logger::Registry::getSink()); + EXPECT_CALL(sink, log(_, _)).WillOnce(Invoke([](auto msg, auto& log) { + EXPECT_THAT(msg, HasSubstr("[lvl: info][msg: hello]")); + EXPECT_EQ(log.logger_name, "misc"); + })); + + ENVOY_LOG_MISC(info, "hello"); + server.shutdown(); +} + +INSTANTIATE_TEST_SUITE_P( + AllConfigs, TextApplicationLogsValidationServerTest, + ::testing::ValuesIn(TextApplicationLogsValidationServerTest::getAllConfigFiles())); + } // namespace } // namespace Server } // namespace Envoy diff --git a/test/server/config_validation/test_data/text_application_logs.yaml b/test/server/config_validation/test_data/text_application_logs.yaml new file mode 100644 index 0000000000000..f598e8ffcde55 --- /dev/null +++ b/test/server/config_validation/test_data/text_application_logs.yaml @@ -0,0 +1,10 @@ +--- +application_log_config: + log_format: + text_format: "[lvl: %l][msg: %v]" + +admin: + address: + socket_address: + address: 0.0.0.0 + port_value: 9000 diff --git a/test/server/server_test.cc b/test/server/server_test.cc index c47df5d57c57f..40da51f18fbcf 100644 --- a/test/server/server_test.cc +++ b/test/server/server_test.cc @@ -1660,6 +1660,19 @@ TEST_P(ServerInstanceImplTest, JsonApplicationLogFailWithForbiddenFlag_) { "setJsonLogFormat error: INVALID_ARGUMENT: Usage of %_ is unavailable for JSON log formats"); } +TEST_P(ServerInstanceImplTest, TextApplicationLog) { + EXPECT_NO_THROW(initialize("test/server/test_data/server/text_application_log.yaml")); + + Envoy::Logger::Registry::setLogLevel(spdlog::level::info); + MockLogSink sink(Envoy::Logger::Registry::getSink()); + EXPECT_CALL(sink, log(_, _)).WillOnce(Invoke([](auto msg, auto& log) { + EXPECT_THAT(msg, HasSubstr("[lvl: info][msg: hello]")); + EXPECT_EQ(log.logger_name, "misc"); + })); + + ENVOY_LOG_MISC(info, "hello"); +} + } // namespace } // namespace Server } // namespace Envoy diff --git a/test/server/test_data/server/text_application_log.yaml b/test/server/test_data/server/text_application_log.yaml new file mode 100644 index 0000000000000..f598e8ffcde55 --- /dev/null +++ b/test/server/test_data/server/text_application_log.yaml @@ -0,0 +1,10 @@ +--- +application_log_config: + log_format: + text_format: "[lvl: %l][msg: %v]" + +admin: + address: + socket_address: + address: 0.0.0.0 + port_value: 9000 diff --git a/test/server/utils_test.cc b/test/server/utils_test.cc index 7576e41522c1b..ea05b4836af23 100644 --- a/test/server/utils_test.cc +++ b/test/server/utils_test.cc @@ -69,6 +69,12 @@ TEST(UtilsTest, MaybeSetApplicationLogFormat) { EXPECT_NO_THROW(Utility::maybeSetApplicationLogFormat(log_config)); } + { + envoy::config::bootstrap::v3::Bootstrap::ApplicationLogConfig log_config; + log_config.mutable_log_format()->mutable_text_format(); + EXPECT_NO_THROW(Utility::maybeSetApplicationLogFormat(log_config)); + } + { envoy::config::bootstrap::v3::Bootstrap::ApplicationLogConfig log_config; auto* format = log_config.mutable_log_format()->mutable_json_format();