Skip to content

Commit

Permalink
[Enhancement](be-logger) Support custom date time format functionalit…
Browse files Browse the repository at this point in the history
…y in be log. (apache#40347)

Add these configurations to control it.

```
// 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}");
```
  • Loading branch information
kaka11chen committed Sep 23, 2024
1 parent 61aa9cb commit b40eb45
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions be/src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ DEFINE_Strings(sys_log_verbose_modules, "");
DEFINE_Int32(sys_log_verbose_level, "10");
// 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 @@ -255,6 +255,12 @@ DECLARE_Strings(sys_log_verbose_modules);
DECLARE_Int32(sys_log_verbose_level);
// 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
47 changes: 46 additions & 1 deletion be/src/common/logconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,47 @@ static bool iequals(const std::string& a, const std::string& b) {
return true;
}

// 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];

// 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');
s << ' ';
s << l.filename << ':' << l.line_number << "]";
}

bool init_glog(const char* basename) {
std::lock_guard<std::mutex> logging_lock(logging_mutex);

Expand Down Expand Up @@ -139,7 +180,11 @@ bool init_glog(const char* basename) {
}
}

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

0 comments on commit b40eb45

Please sign in to comment.