This is an automated email from the ASF dual-hosted git repository.
laiyingchun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git
The following commit(s) were added to refs/heads/master by this push:
new 1196e9568 feat(logging): Support to specify log directory (#2021)
1196e9568 is described below
commit 1196e9568e87baea76735673b3839a42132c5e45
Author: Yingchun Lai <[email protected]>
AuthorDate: Fri May 24 15:26:47 2024 +0800
feat(logging): Support to specify log directory (#2021)
Add a new configuration 'log_dir' to indicate the directory to place the
logs especially. 'data_dir' will be used if it's empty.
```diff
[core]
+log_dir =
```
---
src/runtime/global_config.h | 8 ++++++--
src/runtime/service_api_c.cpp | 22 ++++++++++++++++------
src/utils/config_helper.h | 5 ++++-
src/utils/logging.cpp | 4 ++--
src/utils/logging_provider.h | 2 +-
5 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/src/runtime/global_config.h b/src/runtime/global_config.h
index 65d62bddd..3004bfe6f 100644
--- a/src/runtime/global_config.h
+++ b/src/runtime/global_config.h
@@ -169,8 +169,7 @@ struct service_spec
std::vector<threadpool_spec> threadpool_specs;
std::vector<service_app_spec> app_specs;
- // auto-set
- std::string dir_log;
+ std::string log_dir;
service_spec() {}
bool init();
@@ -186,6 +185,11 @@ CONFIG_FLD_STRING_LIST(toollets,
CONFIG_FLD_STRING(data_dir,
"./data",
"The default directory to place the all the data, logs,
coredump files, and etc.")
+CONFIG_FLD_STRING_BY_KEY(
+ log_dir,
+ "log_dir",
+ "",
+ "The directory to place the logs especially. 'data_dir' will be used if
it's empty.")
CONFIG_FLD(
bool,
bool,
diff --git a/src/runtime/service_api_c.cpp b/src/runtime/service_api_c.cpp
index 44fbd7096..f2195f95a 100644
--- a/src/runtime/service_api_c.cpp
+++ b/src/runtime/service_api_c.cpp
@@ -44,6 +44,7 @@
#include <gperftools/malloc_extension.h>
#endif
+#include "fmt/core.h"
#include "perf_counter/perf_counters.h"
#include "runtime/api_layer1.h"
#include "runtime/api_task.h"
@@ -53,8 +54,6 @@
#include "runtime/rpc/rpc_engine.h"
#include "runtime/rpc/rpc_host_port.h"
#include "runtime/rpc/rpc_message.h"
-#include "security/init.h"
-#include "security/negotiation_manager.h"
#include "runtime/service_app.h"
#include "runtime/service_engine.h"
#include "runtime/task/task.h"
@@ -63,6 +62,8 @@
#include "runtime/task/task_spec.h"
#include "runtime/task/task_worker.h"
#include "runtime/tool_api.h"
+#include "security/init.h"
+#include "security/negotiation_manager.h"
#include "utils/api_utilities.h"
#include "utils/command_manager.h"
#include "utils/config_api.h"
@@ -443,9 +444,18 @@ bool run(const char *config_file,
::dsn::utils::coredump::init();
- // setup log dir
- spec.dir_log = ::dsn::utils::filesystem::path_combine(cdir, "log");
- dsn::utils::filesystem::create_directory(spec.dir_log);
+ // Setup log directory.
+ // If log_dir is not set, use data_dir/log instead.
+ if (spec.log_dir.empty()) {
+ spec.log_dir = ::dsn::utils::filesystem::path_combine(spec.data_dir,
"log");
+ fmt::print(stdout, "log_dir is not set, use '{}' instead\n",
spec.log_dir);
+ }
+ // Validate log_dir.
+ if (!dsn::utils::filesystem::is_absolute_path(spec.log_dir)) {
+ fmt::print(stderr, "log_dir({}) should be set with an absolute
path\n", spec.log_dir);
+ return false;
+ }
+ dsn::utils::filesystem::create_directory(spec.log_dir);
// init tools
dsn_all.tool.reset(::dsn::utils::factory_store<::dsn::tools::tool_app>::create(
@@ -463,7 +473,7 @@ bool run(const char *config_file,
#endif
// init logging
- dsn_log_init(spec.logging_factory_name, spec.dir_log,
dsn_log_prefixed_message_func);
+ dsn_log_init(spec.logging_factory_name, spec.log_dir,
dsn_log_prefixed_message_func);
// prepare minimum necessary
::dsn::service_engine::instance().init_before_toollets(spec);
diff --git a/src/utils/config_helper.h b/src/utils/config_helper.h
index aeda3048c..1010b68ab 100644
--- a/src/utils/config_helper.h
+++ b/src/utils/config_helper.h
@@ -49,9 +49,12 @@
section, #fld, default_value ? default_value->fld : default_fld_value,
dsptr);
#define CONFIG_FLD_STRING(fld, default_fld_value, dsptr)
\
+ CONFIG_FLD_STRING_BY_KEY(fld, #fld, default_fld_value, dsptr)
+
+#define CONFIG_FLD_STRING_BY_KEY(fld, key, default_fld_value, dsptr)
\
val.fld = dsn_config_get_value_string(
\
section,
\
- #fld,
\
+ key,
\
(val.fld.length() > 0 && val.fld != std::string(default_fld_value))
\
? val.fld.c_str()
\
: (default_value ? default_value->fld.c_str() :
default_fld_value), \
diff --git a/src/utils/logging.cpp b/src/utils/logging.cpp
index 294305b3f..0f0484c76 100644
--- a/src/utils/logging.cpp
+++ b/src/utils/logging.cpp
@@ -72,7 +72,7 @@ static void log_on_sys_exit(::dsn::sys_exit_type)
}
void dsn_log_init(const std::string &logging_factory_name,
- const std::string &dir_log,
+ const std::string &log_dir,
std::function<std::string()> dsn_log_prefixed_message_func)
{
log_start_level = enum_from_string(FLAGS_logging_start_level,
LOG_LEVEL_INVALID);
@@ -86,7 +86,7 @@ void dsn_log_init(const std::string &logging_factory_name,
}
dsn::logging_provider *logger =
dsn::utils::factory_store<dsn::logging_provider>::create(
- logging_factory_name.c_str(), dsn::PROVIDER_TYPE_MAIN,
dir_log.c_str());
+ logging_factory_name.c_str(), dsn::PROVIDER_TYPE_MAIN,
log_dir.c_str());
dsn::logging_provider::set_logger(logger);
if (dsn_log_prefixed_message_func != nullptr) {
diff --git a/src/utils/logging_provider.h b/src/utils/logging_provider.h
index 5061cee49..910e418a1 100644
--- a/src/utils/logging_provider.h
+++ b/src/utils/logging_provider.h
@@ -87,5 +87,5 @@ bool register_component_provider(const char *name,
} // namespace dsn
extern void dsn_log_init(const std::string &logging_factory_name,
- const std::string &dir_log,
+ const std::string &log_dir,
std::function<std::string()>
dsn_log_prefixed_message_func);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]