Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement](be-logger) Support custom date time format functionality in be log. #41402

Open
wants to merge 1 commit into
base: branch-3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions be/src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ DEFINE_Int32(sys_log_verbose_level, "10");
DEFINE_Int32(sys_log_verbose_flags_v, "-1");
// log buffer level
DEFINE_String(log_buffer_level, "");
// log enable custom date time format
DEFINE_Bool(sys_log_enable_custom_date_time_format, "false");
// log custom date time format (https://en.cppreference.com/w/cpp/io/manip/put_time)
DEFINE_String(sys_log_custom_date_time_format, "%Y-%m-%d %H:%M:%S");
// log custom date time milliseconds format (fmt::format)
DEFINE_String(sys_log_custom_date_time_ms_format, ",{:03d}");

// number of threads available to serve backend execution requests
DEFINE_Int32(be_service_threads, "64");
Expand Down
6 changes: 6 additions & 0 deletions be/src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ DECLARE_Int32(sys_log_verbose_level);
DECLARE_Int32(sys_log_verbose_flags_v);
// log buffer level
DECLARE_String(log_buffer_level);
// log enable custom date time format
DECLARE_Bool(sys_log_enable_custom_date_time_format);
// log custom date time format (https://en.cppreference.com/w/cpp/io/manip/put_time)
DECLARE_String(sys_log_custom_date_time_format);
// log custom date time milliseconds format (fmt::format)
DECLARE_String(sys_log_custom_date_time_ms_format);

// number of threads available to serve backend execution requests
DECLARE_Int32(be_service_threads);
Expand Down
61 changes: 44 additions & 17 deletions be/src/common/logconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,40 @@ static bool iequals(const std::string& a, const std::string& b) {
return true;
}

void custom_prefix(std::ostream& s, const google::LogMessageInfo& l, void*) {
// Add prefix "RuntimeLogger ".
s << "RuntimeLogger ";
// Same as in fe.log
// The following is same as default log format. eg:
// I20240605 15:25:15.677153 1763151 wal_manager.cpp:481] msg...
// if custom_date_time_format = false, same format as in be.log
// The following is same as default log format. eg:
// I20240605 15:25:15.677153 1763151 wal_manager.cpp:481] msg...
template <bool add_runtime_logger_prefix = false, bool custom_date_time_format = false>
void custom_prefix(std::ostream& s, const google::LogMessageInfo& l, void* arg) {
if constexpr (add_runtime_logger_prefix) {
// Add prefix "RuntimeLogger ".
s << "RuntimeLogger ";
}
s << l.severity[0];
s << std::setw(4) << 1900 + l.time.year();
s << std::setw(2) << 1 + l.time.month();
s << std::setw(2) << l.time.day();
s << ' ';
s << std::setw(2) << l.time.hour() << ':';
s << std::setw(2) << l.time.min() << ':';
s << std::setw(2) << l.time.sec() << ".";
s << std::setw(6) << l.time.usec();

// Add a space if custom_date_time_format.
if constexpr (custom_date_time_format) {
s << ' ';
}

std::tm tm_time = {};
tm_time.tm_year = l.time.year();
tm_time.tm_mon = l.time.month();
tm_time.tm_mday = l.time.day();
tm_time.tm_hour = l.time.hour();
tm_time.tm_min = l.time.min();
tm_time.tm_sec = l.time.sec();

if constexpr (custom_date_time_format) {
s << std::put_time(&tm_time, config::sys_log_custom_date_time_format.c_str());
if (!config::sys_log_custom_date_time_ms_format.empty()) {
s << fmt::format(config::sys_log_custom_date_time_ms_format, l.time.usec() / 1000);
}
} else {
s << std::put_time(&tm_time, "%Y%m%d %H:%M:%S");
s << "." << std::setw(6) << l.time.usec();
}

s << ' ';
s << std::setfill(' ') << std::setw(5);
s << l.thread_id << std::setfill('0');
Expand Down Expand Up @@ -173,10 +192,18 @@ bool init_glog(const char* basename) {
}

if (log_to_console) {
// Only add prefix if log output to stderr
google::InitGoogleLogging(basename, &custom_prefix);
// Add prefix if log output to stderr
if (config::sys_log_enable_custom_date_time_format) {
google::InitGoogleLogging(basename, &custom_prefix<true, true>);
} else {
google::InitGoogleLogging(basename, &custom_prefix<true, false>);
}
} else {
google::InitGoogleLogging(basename);
if (config::sys_log_enable_custom_date_time_format) {
google::InitGoogleLogging(basename, &custom_prefix<false, true>);
} else {
google::InitGoogleLogging(basename);
}
}

logging_initialized = true;
Expand Down
Loading