Skip to content

Commit

Permalink
Feature: Optimize local framework logging output
Browse files Browse the repository at this point in the history
  • Loading branch information
peggiezhu committed Oct 24, 2023
1 parent b301f77 commit b2b4c1d
Show file tree
Hide file tree
Showing 15 changed files with 1,031 additions and 47 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ English | [中文](README.zh_CN.md)
* [custom metrics plugin](./en/custom_metrics.md)
* [prometheus](./en/prometheus_metrics.md)
* Logging
* [local logging tools](./en/local_logging.md)
* [custom logging plugin](./en/custom_logging.md)
* [cls](https://github.com/trpc-ecosystem/cpp-logging-cls/blob/main/README.md)
* Tracing
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* [开发自定义metrics插件](./zh/custom_metrics.md)
* [prometheus](./zh/prometheus_metrics.md)
* Logging插件
* [本地日志工具](./zh/local_logging.md)
* [开发自定义logging插件](./zh/custom_logging.md)
* [cls](https://github.com/trpc-ecosystem/cpp-logging-cls/blob/main/README.zh_CN.md)
* Tracing插件
Expand Down
403 changes: 403 additions & 0 deletions docs/en/local_logging.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/en/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You can use it:

Conventional framework performance test data is only performance data in relatively simple scenarios, and does not represent good performance in real and different business scenarios. Considering that Tencent has many different business scenarios, the requirements for framework performance are also different, for example:

- business accesses gateway scenarios: the feature is that the business logic is light, hign qps, large number of connections (long/short connections), heavy network io operations, and asynchronous programming is often used for programming;
- business accesses gateway scenarios: the feature is that the business logic is light, high qps, large number of connections (long/short connections), heavy network io operations, and asynchronous programming is often used for programming;
- recommendation/search scenarios: the feature is that the business logic is heavy, the qps is not large, each request needs to be calculated in parallel, pay attention to the long tail delay, and the programming often use synchronous programming;
- game business scenarios: the feature is that logic very complex and has stateful, large qps, single-threaded programming, and use synchronous programming;
- storage scenarios: the feature is that the business logic is light, large qps, low latency requirement, heavy network io and disk io operations, and asynchronous programming is often used for programming;
Expand Down
Binary file added docs/images/log_design.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
403 changes: 403 additions & 0 deletions docs/zh/local_logging.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions trpc/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Config : public Plugin {
~Config() override = default;

/// @brief Pulls file configuration.
/// @param config_name - For tconf, pass in the configuration file name; for rainbow, pass in the group_name.
/// @param config_name - For rainbow, pass in the group_name.
/// @param config - The content of the obtained configuration.
/// @param params - For tconf, this parameter is not required; for rainbow, pass in the file name.
/// @param params - For rainbow, pass in the file name.
/// @return - true: Loading successful.
/// false: Loading failed.
virtual bool PullFileConfig(const std::string& config_name, std::string* config, const std::any& params) = 0;
Expand Down
8 changes: 4 additions & 4 deletions trpc/config/default/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ ProviderPtr GetProvider(const std::string& instance_name) {
} // namespace detail

LoadOptions WithCodec(const std::string& name) {
return LoadOptions([name = std::move(name)](DefaultConfigPtr& config) {
return LoadOptions([name](DefaultConfigPtr& config) {

Check warning on line 81 in trpc/config/default/loader.cc

View check run for this annotation

Codecov / codecov/patch

trpc/config/default/loader.cc#L81

Added line #L81 was not covered by tests
config::CodecPtr codec = CodecFactory::GetInstance()->Get(name);
TRPC_ASSERT(codec != nullptr && "Codec not found!");
(*config).SetCodec(codec);
config->SetCodec(codec);
});
}

LoadOptions WithProvider(const std::string& name) {
return LoadOptions([name = std::move(name)](DefaultConfigPtr& config) {
return LoadOptions([name](DefaultConfigPtr& config) {

Check warning on line 89 in trpc/config/default/loader.cc

View check run for this annotation

Codecov / codecov/patch

trpc/config/default/loader.cc#L89

Added line #L89 was not covered by tests
config::ProviderPtr provider = ProviderFactory::GetInstance()->Get(name);
TRPC_ASSERT(provider != nullptr && "Provider not found!");
(*config).SetProvider(provider);
config->SetProvider(provider);
});
}

Expand Down
44 changes: 36 additions & 8 deletions trpc/util/log/default/default_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,51 @@ bool DefaultLog::ShouldLog(const char* instance_name, Level level) const {
return level >= instance.config.min_level;
}

bool DefaultLog::ShouldLog(Level level) const {
if (!inited_) {
std::cerr << "DefaultLog not inited" << std::endl;
return false;

Check warning on line 59 in trpc/util/log/default/default_log.cc

View check run for this annotation

Codecov / codecov/patch

trpc/util/log/default/default_log.cc#L59

Added line #L59 was not covered by tests
}
if (!inited_trpc_logger_instance_) {
std::cout<<"not inited!"<<std::endl;
return false;

Check warning on line 63 in trpc/util/log/default/default_log.cc

View check run for this annotation

Codecov / codecov/patch

trpc/util/log/default/default_log.cc#L63

Added line #L63 was not covered by tests
}
std::cout<<"level: "<<level<<std::endl;
std::cout<<"trpc_logger_instance_.config.min_level: "<<trpc_logger_instance_.config.min_level<<std::endl;
return level >= trpc_logger_instance_.config.min_level;
}

void DefaultLog::LogIt(const char* instance_name, Level level, const char* filename_in, int line_in,
const char* funcname_in, std::string_view msg,
const std::unordered_map<uint32_t, std::any>& filter_data) const {
if (!inited_) {
std::cerr << "DefaultLog not inited" << std::endl;
return;
}
auto iter = instances_.find(instance_name);
if (iter == instances_.end()) {
std::cerr << "DefaultLog instance" << instance_name << " does not exit" << std::endl;
return;

const DefaultLog::Logger* instance = nullptr;
// It is preferred if it is the output of the tRPC-Cpp framework log
if (!strcmp(instance_name, kTrpcLogCacheStringDefault)) {
if (inited_trpc_logger_instance_ == false) {
std::cerr << "DefaultLog instance:" << kTrpcLogCacheStringDefault << "does not exit" << std::endl;
return ;

Check warning on line 83 in trpc/util/log/default/default_log.cc

View check run for this annotation

Codecov / codecov/patch

trpc/util/log/default/default_log.cc#L83

Added line #L83 was not covered by tests
}
instance = &trpc_logger_instance_;
} else {
auto iter = instances_.find(instance_name);
if (iter == instances_.end()) {
std::cerr << "DefaultLog instance: " << instance_name << " does not exit" << std::endl;

Check warning on line 89 in trpc/util/log/default/default_log.cc

View check run for this annotation

Codecov / codecov/patch

trpc/util/log/default/default_log.cc#L87-L89

Added lines #L87 - L89 were not covered by tests
return;
}
instance = &iter->second;

Check warning on line 92 in trpc/util/log/default/default_log.cc

View check run for this annotation

Codecov / codecov/patch

trpc/util/log/default/default_log.cc#L92

Added line #L92 was not covered by tests
}
const auto& instance = iter->second;

if (instance.logger) {
instance.logger->log(spdlog::source_loc{filename_in, line_in, funcname_in}, SpdLevel(level), msg);
if (instance->logger) {
instance->logger->log(spdlog::source_loc{filename_in, line_in, funcname_in}, SpdLevel(level), msg);
}

// Output to a remote plugin (if available)
for (const auto& sink : instance.raw_sinks) {
for (const auto& sink : instance->raw_sinks) {
sink->Log(level, filename_in, line_in, funcname_in, msg, filter_data);
}
}
Expand Down Expand Up @@ -108,6 +133,9 @@ std::pair<Log::Level, bool> DefaultLog::SetLevel(const char* instance_name, Leve

auto old = static_cast<Log::Level>(instance.config.min_level);
instance.config.min_level = static_cast<unsigned int>(level);
if (!strcmp(instance_name, kTrpcLogCacheStringDefault)) {
trpc_logger_instance_.config.min_level = static_cast<unsigned int>(level);
}
return std::make_pair(old, true);
}

Expand Down
17 changes: 17 additions & 0 deletions trpc/util/log/default/default_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class DefaultLog : public Log {
/// @return true/false
bool ShouldLog(const char* instance_name, Level level) const override;

/// @brief Determine whether the log level of the tRPC-Cpp framework instance meets the requirements for printing this log.
/// @param level Log instance level
/// @return true/false
bool ShouldLog(Level level) const override;

/// @brief Output log to a sink instance.
void LogIt(const char* instance_name, Level level, const char* filename_in, int line_in, const char* funcname_in,
std::string_view msg, const std::unordered_map<uint32_t, std::any>& filter_data = {}) const override;
Expand Down Expand Up @@ -108,6 +113,11 @@ class DefaultLog : public Log {
// Add the new sink to the logger's sinks
instance.logger->sinks().push_back(sink->SpdSink());

// The tRPC-Cpp framework logging instance has been configured
if (!strcmp(logger_name, "default")) {
inited_trpc_logger_instance_ = true;
trpc_logger_instance_ = instance;
}
return true;
}

Expand Down Expand Up @@ -148,6 +158,13 @@ class DefaultLog : public Log {
// Initialization flags
bool inited_{false};

// Whether the tRPC-Cpp framework logging instance is configured
// If false, all framework logging will be logged to the console
bool inited_trpc_logger_instance_{false};

// tRPC-Cpp framework logger instance
DefaultLog::Logger trpc_logger_instance_;

// Collection of log instances
std::unordered_map<std::string, Logger> instances_;
};
Expand Down
5 changes: 5 additions & 0 deletions trpc/util/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class Log : public RefCounted<Log> {
/// @private For internal use purpose only.
virtual bool ShouldLog(const char* instance_name, Level level) const = 0;

/// @brief Determine whether the log level of the tRPC-Cpp framework instance meets the requirements for printing this log.
/// @param level Log instance level
/// @return true/false
virtual bool ShouldLog(Level level) const = 0;

/// @brief Output log to a sink instance.
/// @param instance_name Log instance name
/// @param level Log instance level
Expand Down
45 changes: 31 additions & 14 deletions trpc/util/log/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ constexpr char kTrpcLogCacheStringDefault[] = "default";
#define TRPC_LOGGER_PRT_ERROR(instance, format, args...) TRPC_PRT(instance, ::trpc::Log::error, format, ##args)
#define TRPC_LOGGER_PRT_CRITICAL(instance, format, args...) TRPC_PRT(instance, ::trpc::Log::critical, format, ##args)

/// @brief printf-like style log macros with conditions.
/// @brief printf-like style log macros with conditions for tRPC-Cpp framework
#define TRPC_LOGGER_PRT_TRACE_IF(instance, condition, format, args...) \
TRPC_PRT_IF(instance, condition, ::trpc::Log::trace, format, ##args)
#define TRPC_LOGGER_PRT_DEBUG_IF(instance, condition, format, args...) \
Expand All @@ -167,6 +167,7 @@ constexpr char kTrpcLogCacheStringDefault[] = "default";
#define TRPC_LOGGER_PRT_CRITICAL_IF(instance, condition, format, args...) \
TRPC_PRT_IF(instance, condition, ::trpc::Log::critical, format, ##args)

/// @brief printf-like style log macros with conditions
#define TRPC_LOGGER_PRT_TRACE_EX(context, instance, format, args...) \
TRPC_PRT_EX(context, instance, ::trpc::Log::trace, format, ##args)
#define TRPC_LOGGER_PRT_DEBUG_EX(context, instance, format, args...) \
Expand All @@ -180,6 +181,7 @@ constexpr char kTrpcLogCacheStringDefault[] = "default";
#define TRPC_LOGGER_PRT_CRITICAL_EX(context, instance, format, args...) \
TRPC_PRT_EX(context, instance, ::trpc::Log::critical, format, ##args)

/// @brief printf-like style log macros with conditions and context
#define TRPC_LOGGER_PRT_TRACE_IF_EX(context, instance, condition, format, args...) \
TRPC_PRT_IF_EX(context, instance, condition, ::trpc::Log::trace, format, ##args)
#define TRPC_LOGGER_PRT_DEBUG_IF_EX(context, instance, condition, format, args...) \
Expand All @@ -194,36 +196,44 @@ constexpr char kTrpcLogCacheStringDefault[] = "default";
TRPC_PRT_IF_EX(context, instance, condition, ::trpc::Log::critical, format, ##args)

/// @brief Log will be output to the "default" logger instance provided by the framework "default" plugin.
#define TRPC_PRT_TRACE(format, args...) TRPC_LOGGER_PRT_TRACE(::trpc::log::kTrpcLogCacheStringDefault, format, ##args)
#define TRPC_PRT_DEBUG(format, args...) TRPC_LOGGER_PRT_DEBUG(::trpc::log::kTrpcLogCacheStringDefault, format, ##args)
#define TRPC_PRT_INFO(format, args...) TRPC_LOGGER_PRT_INFO(::trpc::log::kTrpcLogCacheStringDefault, format, ##args)
#define TRPC_PRT_WARN(format, args...) TRPC_LOGGER_PRT_WARN(::trpc::log::kTrpcLogCacheStringDefault, format, ##args)
#define TRPC_PRT_ERROR(format, args...) TRPC_LOGGER_PRT_ERROR(::trpc::log::kTrpcLogCacheStringDefault, format, ##args)
#define TRPC_PRT_TRACE(format, args...) TRPC_PRT_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, ::trpc::Log::trace, format, ##args)
#define TRPC_PRT_DEBUG(format, args...) TRPC_PRT_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, ::trpc::Log::debug, format, ##args)
#define TRPC_PRT_INFO(format, args...) TRPC_PRT_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, ::trpc::Log::info, format, ##args)
#define TRPC_PRT_WARN(format, args...) TRPC_PRT_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, ::trpc::Log::warn, format, ##args)
#define TRPC_PRT_ERROR(format, args...) TRPC_PRT_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, ::trpc::Log::error, format, ##args)
#define TRPC_PRT_CRITICAL(format, args...) \
TRPC_LOGGER_PRT_CRITICAL(::trpc::log::kTrpcLogCacheStringDefault, format, ##args)
TRPC_PRT_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, ::trpc::Log::critical, format, ##args)

/// @brief Log macro for the framework default log with conditions.
#define TRPC_PRT_TRACE_IF(condition, format, args...) \
TRPC_LOGGER_PRT_TRACE_IF(::trpc::log::kTrpcLogCacheStringDefault, condition, format, ##args)
TRPC_PRT_IF_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, condition, ::trpc::Log::trace, format, ##args)
#define TRPC_PRT_DEBUG_IF(condition, format, args...) \
TRPC_LOGGER_PRT_DEBUG_IF(::trpc::log::kTrpcLogCacheStringDefault, condition, format, ##args)
TRPC_PRT_IF_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, condition, ::trpc::Log::debug, format, ##args)
#define TRPC_PRT_INFO_IF(condition, format, args...) \
TRPC_LOGGER_PRT_INFO_IF(::trpc::log::kTrpcLogCacheStringDefault, condition, format, ##args)
TRPC_PRT_IF_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, condition, ::trpc::Log::info, format, ##args)
#define TRPC_PRT_WARN_IF(condition, format, args...) \
TRPC_LOGGER_PRT_WARN_IF(::trpc::log::kTrpcLogCacheStringDefault, condition, format, ##args)
TRPC_PRT_IF_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, condition, ::trpc::Log::warn, format, ##args)
#define TRPC_PRT_ERROR_IF(condition, format, args...) \
TRPC_LOGGER_PRT_ERROR_IF(::trpc::log::kTrpcLogCacheStringDefault, condition, format, ##args)
TRPC_PRT_IF_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, condition, ::trpc::Log::error, format, ##args)
#define TRPC_PRT_CRITICAL_IF(condition, format, args...) \
TRPC_LOGGER_PRT_CRITICAL_IF(::trpc::log::kTrpcLogCacheStringDefault, condition, format, ##args)
TRPC_PRT_IF_DEFAULT(::trpc::log::kTrpcLogCacheStringDefault, condition, ::trpc::Log::critical, format, ##args)

/// @brief stream-like log macros
/// @brief stream-like log macros for tRPC-Cpp framework
#define TRPC_LOG_TRACE(msg) TRPC_LOG_MSG(::trpc::Log::trace, msg)
#define TRPC_LOG_DEBUG(msg) TRPC_LOG_MSG(::trpc::Log::debug, msg)
#define TRPC_LOG_INFO(msg) TRPC_LOG_MSG(::trpc::Log::info, msg)
#define TRPC_LOG_WARN(msg) TRPC_LOG_MSG(::trpc::Log::warn, msg)
#define TRPC_LOG_ERROR(msg) TRPC_LOG_MSG(::trpc::Log::error, msg)
#define TRPC_LOG_CRITICAL(msg) TRPC_LOG_MSG(::trpc::Log::critical, msg)

/// @brief stream-like log macros
#define TRPC_LOGGER_TRACE(instance, msg) TRPC_LOGGER_MSG(::trpc::Log::trace, instance, msg)
#define TRPC_LOGGER_DEBUG(instance, msg) TRPC_LOGGER_MSG(::trpc::Log::debug, instance, msg)
#define TRPC_LOGGER_INFO(instance, msg) TRPC_LOGGER_MSG(::trpc::Log::info, instance, msg)
#define TRPC_LOGGER_WARN(instance, msg) TRPC_LOGGER_MSG(::trpc::Log::warn, instance, msg)
#define TRPC_LOGGER_ERROR(instance, msg) TRPC_LOGGER_MSG(::trpc::Log::error, instance, msg)
#define TRPC_LOGGER_CRITICAL(instance, msg) TRPC_LOGGER_MSG(::trpc::Log::critical, instance, msg)

/// @brief stream-like style log macros with conditions
#define TRPC_LOG_TRACE_IF(condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::trace, condition, msg)
#define TRPC_LOG_DEBUG_IF(condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::debug, condition, msg)
Expand All @@ -232,6 +242,13 @@ constexpr char kTrpcLogCacheStringDefault[] = "default";
#define TRPC_LOG_ERROR_IF(condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::error, condition, msg)
#define TRPC_LOG_CRITICAL_IF(condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::critical, condition, msg)

#define TRPC_LOGGER_TRACE_IF(instance, condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::trace, instance, condition, msg)
#define TRPC_LOGGER_DEBUG_IF(instance, condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::debug, instance, condition, msg)
#define TRPC_LOGGER_INFO_IF(instance, condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::info, instance, condition, msg)
#define TRPC_LOGGER_WARN_IF(instance, condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::warn, instance, condition, msg)
#define TRPC_LOGGER_ERROR_IF(instance, condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::error, instance, condition, msg)
#define TRPC_LOGGER_CRITICAL_IF(instance, condition, msg) TRPC_LOG_MSG_IF(::trpc::Log::critical, instance, condition, msg)

/// @brief logger instances can be specified to customize the log output.
/// @note Use case: Separate business logs from framework logs,
/// Different business logs specify different loggers.
Expand Down
Loading

0 comments on commit b2b4c1d

Please sign in to comment.