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 23, 2023
1 parent b301f77 commit 756a6d2
Show file tree
Hide file tree
Showing 12 changed files with 1,024 additions and 40 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.

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.

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;
}
if (!inited_trpc_logger_instance_) {
std::cout<<"not inited!"<<std::endl;
return false;
}
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 ;
}
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;
return;
}
instance = &iter->second;
}
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
39 changes: 34 additions & 5 deletions trpc/util/log/printf_like.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,34 @@

#include "fmt/printf.h"

//#include "trpc/client/client_context.h"
//#include "trpc/server/server_context.h"
#include "trpc/util/log/log.h"

/// @brief printf-like log macros for tRPC-Cpp framework log
#define TRPC_PRT_DEFAULT(instance, level, formats, args...) \
do { \
const auto& __TRPC_PRINTF_LIKE_INSTANCE__ = ::trpc::LogFactory::GetInstance()->Get(); \
if (__TRPC_PRINTF_LIKE_INSTANCE__) { \
if (__TRPC_PRINTF_LIKE_INSTANCE__->ShouldLog(level)) { \
TRPC_LOG_TRY { \
std::string __TRPC_PRINTF_LIKE_MSG__ = ::trpc::Log::LogSprintf(formats, ##args); \
__TRPC_PRINTF_LIKE_INSTANCE__->LogIt(instance, level, __FILE__, __LINE__, __FUNCTION__, \
__TRPC_PRINTF_LIKE_MSG__); \
} \
TRPC_LOG_CATCH(instance) \
} \
} else { \
if (::trpc::Log::ShouldNoLog(instance, level)) { \
TRPC_LOG_TRY { \
std::string __TRPC_PRINTF_LIKE_MSG__ = ::trpc::Log::LogSprintf(formats, ##args); \
::trpc::Log::NoLog(instance, level, __FILE__, __LINE__, __FUNCTION__, \
std::string_view(__TRPC_PRINTF_LIKE_MSG__.data(), __TRPC_PRINTF_LIKE_MSG__.size())); \
} \
TRPC_LOG_CATCH(instance) \
} \
} \
} while (0)


/// @brief printf-like log macros
#define TRPC_PRT(instance, level, formats, args...) \
do { \
Expand All @@ -46,9 +70,14 @@
} \
} while (0)

#define TRPC_PRT_IF(instance, condition, level, formats, args...) \
if (condition) { \
TRPC_PRT(instance, level, formats, ##args); \
#define TRPC_PRT_IF(instance, condition, level, formats, args...) \
if (condition) { \
TRPC_PRT_DEFAULT(instance, level, formats, ##args); \
}

#define TRPC_PRT_IF_DEFAULT(instance, condition, level, formats, args...) \
if (condition) { \
TRPC_PRT(instance, level, formats, ##args); \
}

#define TRPC_PRT_EX(context, instance, level, formats, args...) \
Expand Down
24 changes: 24 additions & 0 deletions trpc/util/log/python_like.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@

#include "trpc/util/log/log.h"

/// @brief python-like style log macros for tRPC-Cpp framework log
#define TRPC_FMT_DEFAULT(instance, level, formats, args...) \
do { \
const auto& __TRPC_PYTHON_LIKE_INSTANCE__ = ::trpc::LogFactory::GetInstance()->Get(); \
if (__TRPC_PYTHON_LIKE_INSTANCE__) { \
if (__TRPC_PYTHON_LIKE_INSTANCE__->ShouldLog(level)) { \
TRPC_LOG_TRY { \
__TRPC_PYTHON_LIKE_INSTANCE__->LogIt(instance, level, __FILE__, __LINE__, __FUNCTION__, \
::trpc::Log::LogFormat(formats, ##args)); \
} \
TRPC_LOG_CATCH(instance) \
} \
} else { \
if (::trpc::Log::ShouldNoLog(instance, level)) { \
TRPC_LOG_TRY { \
::trpc::Log::NoLog(instance, level, __FILE__, __LINE__, __FUNCTION__, \
::trpc::Log::LogFormat(formats, ##args)); \
} \
TRPC_LOG_CATCH(instance) \
} \
} \
} while (0)


/// @brief python-like style log macros
#define TRPC_FMT(instance, level, formats, args...) \
do { \
Expand Down
Loading

0 comments on commit 756a6d2

Please sign in to comment.