From c261d3a7c0122c7a2a8ebe51a16b53e5239f1455 Mon Sep 17 00:00:00 2001 From: raychen <815315825@qq.com> Date: Fri, 17 Nov 2023 14:34:33 +0800 Subject: [PATCH] Optimize: Modify log module --- trpc/common/config/BUILD | 5 +- trpc/common/config/default_log_conf.cc | 15 ---- trpc/common/config/default_log_conf.h | 12 +-- trpc/common/config/default_log_conf_parser.cc | 26 +++++- trpc/common/config/default_log_conf_parser.h | 22 ++++- trpc/common/config/local_file_sink_conf.cc | 41 ++++------ trpc/common/config/local_file_sink_conf.h | 4 +- .../config/local_file_sink_conf_parser.h | 24 +----- trpc/common/config/stdout_sink_conf.cc | 27 ++----- trpc/common/config/stdout_sink_conf_parser.h | 19 ----- trpc/log/logging.h | 7 +- trpc/util/log/default/BUILD | 1 + trpc/util/log/default/default_log.cc | 81 +++++++++++-------- trpc/util/log/default/default_log.h | 27 +++---- .../sinks/local_file/local_file_sink.cc | 4 +- trpc/util/log/logging.h | 4 +- 16 files changed, 149 insertions(+), 170 deletions(-) diff --git a/trpc/common/config/BUILD b/trpc/common/config/BUILD index 0f59c646..028ce9f8 100644 --- a/trpc/common/config/BUILD +++ b/trpc/common/config/BUILD @@ -56,15 +56,16 @@ cc_library( deps = [ ":config_helper", ":default_log_conf_h", - ":default_log_conf_parse", + ":default_log_conf_parser", ], ) cc_library( - name = "default_log_conf_parse", + name = "default_log_conf_parser", srcs = ["default_log_conf_parser.cc"], hdrs = ["default_log_conf_parser.h"], deps = [ + ":default_value", ":config_helper", ":default_log_conf_h", "@com_github_jbeder_yaml_cpp//:yaml-cpp", diff --git a/trpc/common/config/default_log_conf.cc b/trpc/common/config/default_log_conf.cc index b8e4b066..2c14d584 100644 --- a/trpc/common/config/default_log_conf.cc +++ b/trpc/common/config/default_log_conf.cc @@ -24,21 +24,6 @@ void DefaultLogConfig::Display() const { } } -template <> -bool GetLoggerConfig(std::string_view logger_name, - trpc::DefaultLogConfig::LoggerInstance& config) { - YAML::Node logger_node; - // The node is obtained by yaml parsing a single Logger - if (!GetLoggerNode(logger_name, logger_node)) { - std::cerr << "Get DefaultLogNode err or logger not found, " << " logger_name: " << logger_name << std::endl; - return false; - } - // Convert node into a LoggerInstance config object - YAML::convert c; - c.decode(logger_node, config); - return true; -} - bool GetDefaultLogConfig(trpc::DefaultLogConfig& config) { YAML::Node default_log_node; // yaml gets node by parsing DefaultLog diff --git a/trpc/common/config/default_log_conf.h b/trpc/common/config/default_log_conf.h index 5516a0ed..a421c32e 100644 --- a/trpc/common/config/default_log_conf.h +++ b/trpc/common/config/default_log_conf.h @@ -36,10 +36,10 @@ struct DefaultLogConfig { /// @brief Print out the logger configuration. void Display() const { - std::cout << "name:" << name << std::endl; - std::cout << "min_level:" << min_level << std::endl; - std::cout << "format:" << format << std::endl; - std::cout << "mode:" << mode << std::endl; + std::cout << "name: " << name << std::endl; + std::cout << "min_level: " << min_level << std::endl; + std::cout << "format: " << format << std::endl; + std::cout << "mode: " << mode << " ===> 1: sync 2: async 3: overrun_oldest" << std::endl; } }; @@ -50,10 +50,6 @@ struct DefaultLogConfig { void Display() const; }; -/// @brief Get the configuration for a single logger from the default plugin configuration -template -bool GetLoggerConfig(std::string_view logger_name, Config& config); - /// @brief Get node config for all loggers under default plugin bool GetDefaultLogConfig(trpc::DefaultLogConfig& config); diff --git a/trpc/common/config/default_log_conf_parser.cc b/trpc/common/config/default_log_conf_parser.cc index 017caf35..007b91ed 100644 --- a/trpc/common/config/default_log_conf_parser.cc +++ b/trpc/common/config/default_log_conf_parser.cc @@ -16,7 +16,10 @@ namespace YAML { bool GetDefaultLogNode(YAML::Node& default_log_node) { - if (!trpc::ConfigHelper::GetInstance()->GetConfig({"plugins", "log", "default"}, default_log_node)) return false; + if (!trpc::ConfigHelper::GetInstance()->GetConfig({"plugins", "log", trpc::kTrpcLogCacheStringDefault}, + default_log_node)) { + return false; + } return true; } @@ -34,4 +37,25 @@ bool GetLoggerNode(std::string_view logger_name, YAML::Node& logger_node) { return true; } +bool GetDefaultLoggerSinkNode(std::string_view logger_name, std::string_view sink_type, std::string_view sink_name, + YAML::Node& sink_log_node) { + YAML::Node logger_node, sinks_node; + // Check if logger is configured + if (!GetLoggerNode(logger_name, logger_node)) { + std::cerr << "Get loggerNode err, logger_name: " << logger_name << std::endl; + return false; + } + // Check if sinks are configured + if (!trpc::ConfigHelper::GetNode(logger_node, {sink_type.data()}, sinks_node)) { + std::cerr << logger_name << " -> " << sink_type << " not found!" << std::endl; + return false; + } + // Check if sink_log is configured + if (!trpc::ConfigHelper::GetNode(sinks_node, {sink_name.data()}, sink_log_node)) { + std::cerr << logger_name << " -> " << sink_type << " -> " << sink_name << " not found!" << std::endl; + return false; + } + return true; +} + } // namespace YAML diff --git a/trpc/common/config/default_log_conf_parser.h b/trpc/common/config/default_log_conf_parser.h index ec9d42c9..640818c0 100644 --- a/trpc/common/config/default_log_conf_parser.h +++ b/trpc/common/config/default_log_conf_parser.h @@ -22,15 +22,35 @@ #include "trpc/common/config/config_helper.h" #include "trpc/common/config/default_log_conf.h" +#include "trpc/common/config/default_value.h" namespace YAML { -/// @brief Get yaml configuration under defalut plugin. +/// @brief Get yaml configuration under default plugin. bool GetDefaultLogNode(YAML::Node& default_log_node); /// @brief Get the yaml configuration under the logger. bool GetLoggerNode(std::string_view logger_name, YAML::Node& logger_node); +/// @brief Get the yaml of sinks configuration under the default logger. +bool GetDefaultLoggerSinkNode(std::string_view logger_name, std::string_view sink_type, std::string_view sink_name, + YAML::Node& sink_log_node); + +/// @brief Get the configuration for the logger sink based on the logger name +template +bool GetDefaultLoggerSinkConfig(std::string_view logger_name, std::string_view sink_type, std::string_view sink_name, + SinkConfig& config) { + YAML::Node sink_log_node; + // Parse a single Logger through yaml to get nodes in local_file + if (!GetDefaultLoggerSinkNode(logger_name, sink_type, sink_name, sink_log_node)) { + return false; + } + // Convert node to sink config + YAML::convert sink_conf; + sink_conf.decode(sink_log_node, config); + return true; +} + template <> struct convert { static YAML::Node encode(const trpc::DefaultLogConfig::LoggerInstance& config) { diff --git a/trpc/common/config/local_file_sink_conf.cc b/trpc/common/config/local_file_sink_conf.cc index 11f92033..f7fc4b0f 100644 --- a/trpc/common/config/local_file_sink_conf.cc +++ b/trpc/common/config/local_file_sink_conf.cc @@ -14,6 +14,7 @@ #include #include +#include "trpc/common/config/default_log_conf_parser.h" #include "trpc/common/config/local_file_sink_conf.h" #include "trpc/common/config/local_file_sink_conf_parser.h" @@ -21,32 +22,18 @@ namespace trpc { - /// @brief Get the configuration for the logger local file based on the logger name - template <> - bool GetLoggerConfig(std::string_view logger_name, LocalFileSinkConfig& config) { - YAML::Node local_file_node; - // Parse a single Logger through yaml to get nodes in local_file - if (!YAML::GetLocalFileNode(logger_name, local_file_node)) { - return false; - } - // Convert node to LocalFileSinkConfig - YAML::convert c; - c.decode(local_file_node, config); - return true; - } - - /// @brief Print the configuration - void LocalFileSinkConfig::Display() const { - std::cout << "format:" << format << std::endl; - std::cout << "eol:" << format << std::endl; - std::cout << "filename:" << format << std::endl; - std::cout << "roll_type:" << format << std::endl; - std::cout << "reserve_count:" << format << std::endl; - std::cout << "roll_size by size:" << format << std::endl; - std::cout << "rotation_hour by day:" << format << std::endl; - std::cout << "rotation_minute by day:" << format << std::endl; - std::cout << "remove_timout_file_switch by day:" << format << std::endl; - std::cout << "hour_interval by hour:" << format << std::endl; - } +/// @brief Print the configuration +void LocalFileSinkConfig::Display() const { + std::cout << "format: " << format << std::endl; + std::cout << "eol: " << format << std::endl; + std::cout << "filename: " << format << std::endl; + std::cout << "roll_type: " << format << std::endl; + std::cout << "reserve_count: " << format << std::endl; + std::cout << "roll_size by size: " << format << std::endl; + std::cout << "rotation_hour by day: " << format << std::endl; + std::cout << "rotation_minute by day: " << format << std::endl; + std::cout << "remove_timeout_file_switch by day: " << format << std::endl; + std::cout << "hour_interval by hour: " << format << std::endl; +} } // namespace trpc diff --git a/trpc/common/config/local_file_sink_conf.h b/trpc/common/config/local_file_sink_conf.h index f87e804d..e6fc1230 100644 --- a/trpc/common/config/local_file_sink_conf.h +++ b/trpc/common/config/local_file_sink_conf.h @@ -34,12 +34,12 @@ struct LocalFileSinkConfig { unsigned int reserve_count{10}; /// @brief Scroll log size unsigned int roll_size{1024 * 1024 * 100}; - /// @brief represents the time of day, as specified by rotation_hour:rotation_minute + /// @brief Represents the time of day, as specified by rotation_hour:rotation_minute unsigned int rotation_hour{0}; /// @brief Cut by minutes unsigned int rotation_minute{0}; /// @brief In split by day mode, remove the identity of obsolete files, not by default - bool remove_timout_file_switch{false}; + bool remove_timeout_file_switch{false}; /// @brief Represents an hourly interval in hours, default interval is one hour unsigned int hour_interval{1}; diff --git a/trpc/common/config/local_file_sink_conf_parser.h b/trpc/common/config/local_file_sink_conf_parser.h index 158e0f2a..ae811ee0 100644 --- a/trpc/common/config/local_file_sink_conf_parser.h +++ b/trpc/common/config/local_file_sink_conf_parser.h @@ -19,24 +19,6 @@ namespace YAML { -inline bool GetLocalFileNode(std::string_view logger_name, YAML::Node& local_file_node) { - YAML::Node logger_node, sinks_node; - // Check if logger is configured - if (!GetLoggerNode(logger_name, logger_node)) { - return false; - } - // Check if sinks are configured - if (!trpc::ConfigHelper::GetNode(logger_node, {"sinks"}, sinks_node)) { - std::cerr<< "sink not found!" < struct convert { static YAML::Node encode(const trpc::LocalFileSinkConfig& config) { @@ -49,7 +31,7 @@ struct convert { node["roll_size"] = config.roll_size; node["rotation_hour"] = config.rotation_hour; node["rotation_minute"] = config.rotation_minute; - node["remove_timout_file_switch"] = config.remove_timout_file_switch; + node["remove_timeout_file_switch"] = config.remove_timeout_file_switch; node["hour_interval"] = config.hour_interval; return node; } @@ -79,8 +61,8 @@ struct convert { if (node["rotation_minute"]) { config.rotation_minute = node["rotation_minute"].as(); } - if (node["remove_timout_file_switch"]) { - config.remove_timout_file_switch = node["remove_timout_file_switch"].as(); + if (node["remove_timeout_file_switch"]) { + config.remove_timeout_file_switch = node["remove_timeout_file_switch"].as(); } if (node["hour_interval"]) { config.hour_interval = node["hour_interval"].as(); diff --git a/trpc/common/config/stdout_sink_conf.cc b/trpc/common/config/stdout_sink_conf.cc index ae084e0a..cc34da2d 100644 --- a/trpc/common/config/stdout_sink_conf.cc +++ b/trpc/common/config/stdout_sink_conf.cc @@ -14,6 +14,7 @@ #include #include +#include "trpc/common/config/default_log_conf_parser.h" #include "trpc/common/config/stdout_sink_conf.h" #include "trpc/common/config/stdout_sink_conf_parser.h" @@ -21,25 +22,11 @@ namespace trpc { - /// @brief Get the configuration for the logger local file based on the logger name - template <> - bool GetLoggerConfig(std::string_view logger_name, StdoutSinkConfig& config) { - YAML::Node stdout_node; - // Parse a single Logger through yaml to get nodes in local_file - if (!YAML::GetStdoutNode(logger_name, stdout_node)) { - return false; - } - // Convert node to LocalFileConfig - YAML::convert c; - c.decode(stdout_node, config); - return true; - } - - /// @brief Print the configuration - void StdoutSinkConfig::Display() const { - std::cout << "format:" << format << std::endl; - std::cout << "eol:" << eol << std::endl; - std::cout << "stderr_level:" << stderr_level << std::endl; - } +/// @brief Print the configuration +void StdoutSinkConfig::Display() const { + std::cout << "format: " << format << std::endl; + std::cout << "eol: " << eol << std::endl; + std::cout << "stderr_level: " << stderr_level << std::endl; +} } // namespace trpc diff --git a/trpc/common/config/stdout_sink_conf_parser.h b/trpc/common/config/stdout_sink_conf_parser.h index e538bbbc..36c286bc 100644 --- a/trpc/common/config/stdout_sink_conf_parser.h +++ b/trpc/common/config/stdout_sink_conf_parser.h @@ -19,25 +19,6 @@ namespace YAML { -bool GetStdoutNode(std::string_view logger_name, YAML::Node& stdout_node) { - YAML::Node logger_node, sinks_node; - // Check if logger is configured - if (!GetLoggerNode(logger_name, logger_node)) { - std::cerr << "Get loggerNode err: " << "logger_name" << logger_name << std::endl; - return false; - } - // Check if sinks are configured - if (!trpc::ConfigHelper::GetNode(logger_node, {"sinks"}, sinks_node)) { - std::cerr << "sink not found!" << std::endl; - return false; - } - // Check if local_file is configured - if (!trpc::ConfigHelper::GetNode(sinks_node, {"stdout"}, stdout_node)) { - return false; - } - return true; -} - template <> struct convert { static YAML::Node encode(const trpc::StdoutSinkConfig& config) { diff --git a/trpc/log/logging.h b/trpc/log/logging.h index 2981a2ee..a72ae35f 100644 --- a/trpc/log/logging.h +++ b/trpc/log/logging.h @@ -13,6 +13,8 @@ #pragma once +#include "spdlog/pattern_formatter.h" + #include "trpc/common/plugin.h" #include "trpc/util/log/log.h" @@ -22,7 +24,7 @@ class Logging : public Plugin { public: /// @brief Gets the logger name. /// @return Under which logger the remote logging plugin is mounted. - virtual std::string LoggerName() const = 0; + virtual const std::string& LoggerName() const = 0; /// @brief Gets the plugin type /// @return plugin type @@ -36,6 +38,9 @@ class Logging : public Plugin { /// @param msg Log message virtual void Log(const Log::Level level, const char* filename_in, int line_in, const char* funcname_in, std::string_view msg, const std::unordered_map& extend_fields_msg) = 0; + + /// @brief Get the pointer of sink instance of spdlog + virtual spdlog::sink_ptr SpdSink() const { return nullptr; } }; using LoggingPtr = RefPtr; diff --git a/trpc/util/log/default/BUILD b/trpc/util/log/default/BUILD index 004ad8fb..05c4ccc3 100644 --- a/trpc/util/log/default/BUILD +++ b/trpc/util/log/default/BUILD @@ -13,6 +13,7 @@ cc_library( hdrs = ["default_log.h"], deps = [ "//trpc/common/config:default_log_conf", + "//trpc/common/config:default_value", "//trpc/common/config:local_file_sink_conf", "//trpc/common/config:stdout_sink_conf", "//trpc/log:logging", diff --git a/trpc/util/log/default/default_log.cc b/trpc/util/log/default/default_log.cc index f4de24c8..0b053fd5 100644 --- a/trpc/util/log/default/default_log.cc +++ b/trpc/util/log/default/default_log.cc @@ -15,16 +15,16 @@ #include +#include "trpc/common/config/default_value.h" #include "trpc/common/config/local_file_sink_conf.h" +#include "trpc/common/config/local_file_sink_conf_parser.h" #include "trpc/common/config/stdout_sink_conf.h" +#include "trpc/common/config/stdout_sink_conf_parser.h" #include "trpc/util/log/default/sinks/local_file/local_file_sink.h" #include "trpc/util/log/default/sinks/stdout/stdout_sink.h" namespace trpc { -// Constant "default" -constexpr char kTrpcLogCacheStringDefault[] = "default"; - void DefaultLog::Start() { spdlog::flush_every(std::chrono::milliseconds{50}); } void DefaultLog::Stop() { @@ -39,14 +39,14 @@ void DefaultLog::Stop() { } bool DefaultLog::ShouldLog(const char* instance_name, Level level) const { - if (!inited_) { - std::cerr << "DefaultLog not inited" << std::endl; + if (!initted_) { + std::cerr << "DefaultLog not initted" << std::endl; return false; } auto iter = instances_.find(instance_name); if (iter == instances_.end()) { - std::cerr << "DefaultLog instance" << instance_name << " does not exit" << std::endl; + std::cerr << "DefaultLog instance " << instance_name << " does not exit" << std::endl; return false; } auto& instance = iter->second; @@ -54,12 +54,12 @@ bool DefaultLog::ShouldLog(const char* instance_name, Level level) const { } bool DefaultLog::ShouldLog(Level level) const { - if (!inited_) { - std::cerr << "DefaultLog not inited" << std::endl; + if (!initted_) { + std::cerr << "DefaultLog not initted" << std::endl; return false; } - if (!inited_trpc_logger_instance_) { - std::cout<<"not inited!"<= trpc_logger_instance_.config.min_level; @@ -68,17 +68,17 @@ bool DefaultLog::ShouldLog(Level level) const { 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& filter_data) const { - if (!inited_) { - std::cerr << "DefaultLog not inited" << std::endl; + if (!initted_) { + std::cerr << "DefaultLog not initted" << 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 ; + if (initted_trpc_logger_instance_ == false) { + std::cerr << "DefaultLog instance: " << kTrpcLogCacheStringDefault << " does not exit" << std::endl; + return; } instance = &trpc_logger_instance_; } else { @@ -101,14 +101,14 @@ void DefaultLog::LogIt(const char* instance_name, Level level, const char* filen } std::pair DefaultLog::GetLevel(const char* instance_name) const { - if (!inited_) { - std::cerr << "DefaultLog not inited" << std::endl; + if (!initted_) { + std::cerr << "DefaultLog not initted" << std::endl; return std::make_pair(Level::info, false); } auto iter = instances_.find(instance_name); if (iter == instances_.end()) { - std::cerr << "DefaultLog instance" << instance_name << " does not exit" << std::endl; + std::cerr << "DefaultLog instance " << instance_name << " does not exit" << std::endl; return std::make_pair(Level::info, false); } auto& instance = iter->second; @@ -117,14 +117,14 @@ std::pair DefaultLog::GetLevel(const char* instance_name) cons } std::pair DefaultLog::SetLevel(const char* instance_name, Level level) { - if (!inited_) { - std::cerr << "DefaultLog not inited" << std::endl; + if (!initted_) { + std::cerr << "DefaultLog not initted" << std::endl; return std::make_pair(Level::info, false); } auto iter = instances_.find(instance_name); if (iter == instances_.end()) { - std::cerr << "DefaultLog instance" << instance_name << " does not exit" << std::endl; + std::cerr << "DefaultLog instance " << instance_name << " does not exit" << std::endl; return std::make_pair(Level::info, false); } auto& instance = iter->second; @@ -138,14 +138,16 @@ std::pair DefaultLog::SetLevel(const char* instance_name, Leve } int DefaultLog::Init() { - if (inited_) { + if (initted_) { std::cerr << "DefaultLog already init!" << std::endl; return -1; } // Get all logger instances configured for the "default" plugin DefaultLogConfig config; - if (!GetDefaultLogConfig(config)) return -1; + if (!GetDefaultLogConfig(config)) { + return -1; + } // "config.instances" stands for the logger instance, // users can configure multiple loggers to separate business logs from framework logs. @@ -154,17 +156,20 @@ int DefaultLog::Init() { RegisterInstance(conf.name.c_str(), Logger{conf}); // Create the spdlog logger - if (!createSpdLogger(conf.name.c_str())) { - std::cerr << "create SpdLogger failed!" - << "name: " << conf.name.c_str() << std::endl; + if (!CreateSpdLogger(conf.name.c_str())) { + std::cerr << "create SpdLogger failed! name: " << conf.name.c_str() << std::endl; return -1; } // Init sinks into the spdlog's Logger if exist - if (!InitSink(conf.name.c_str())) return -1; - if (!InitSink(conf.name.c_str())) return -1; + if (!InitSink(conf.name.c_str(), "local_file")) { + return -1; + } + if (!InitSink(conf.name.c_str(), "stdout")) { + return -1; + } } - inited_ = true; + initted_ = true; return 0; } @@ -173,8 +178,14 @@ bool DefaultLog::RegisterRawSink(const LoggingPtr& raw_sink) { std::cerr << "raw_sink is nullptr!" << std::endl; return false; } - auto& instance = instances_[raw_sink->LoggerName()]; - instance.raw_sinks.push_back(raw_sink); + const std::string& logger_name = raw_sink->LoggerName(); + auto& instance = instances_[logger_name]; + auto spd_sink = raw_sink->SpdSink(); + if (spd_sink) { + instance.logger->sinks().push_back(spd_sink); + } else { + instance.raw_sinks.push_back(raw_sink); + } return true; } @@ -216,7 +227,7 @@ DefaultLog::Logger* DefaultLog::GetLoggerInstance(const std::string& instance_na } } -bool DefaultLog::createSpdLogger(const char* logger_name) { +bool DefaultLog::CreateSpdLogger(const char* logger_name) { auto& instance = instances_[logger_name]; auto& conf = instance.config; @@ -244,12 +255,12 @@ bool DefaultLog::createSpdLogger(const char* logger_name) { } int DefaultLog::Destroy() { - if (!inited_) { - std::cerr << "DefaultLog not inited" << std::endl; + if (!initted_) { + std::cerr << "DefaultLog not initted" << std::endl; exit(-1); } - inited_ = false; + initted_ = false; for (auto& instance : instances_) { for (auto& sink : instance.second.raw_sinks) { sink->Destroy(); diff --git a/trpc/util/log/default/default_log.h b/trpc/util/log/default/default_log.h index 2be9b3d5..1526ea31 100644 --- a/trpc/util/log/default/default_log.h +++ b/trpc/util/log/default/default_log.h @@ -25,6 +25,8 @@ #include "spdlog/spdlog.h" #include "trpc/common/config/default_log_conf.h" +#include "trpc/common/config/default_log_conf_parser.h" +#include "trpc/common/config/default_value.h" #include "trpc/log/logging.h" #include "trpc/util/log/log.h" @@ -76,7 +78,8 @@ 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. + /// @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; @@ -101,32 +104,28 @@ class DefaultLog : public Log { /// @return bool success/failure /// @private For internal use purpose only. template - bool InitSink(const char* logger_name) { + bool InitSink(const char* logger_name, const char* sink_name) { auto& instance = instances_[logger_name]; auto& conf = instance.config; // Get local_file configuration SinkConfig sink_config; // configuration does not exist and returns true - if (!GetLoggerConfig(conf.name, sink_config)) return true; + if (!YAML::GetDefaultLoggerSinkConfig(conf.name, "sinks", sink_name, sink_config)) { + return true; + } auto sink = MakeRefCounted(); sink->Init(sink_config); // 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; + if (!strcmp(logger_name, kTrpcLogCacheStringDefault)) { + initted_trpc_logger_instance_ = true; trpc_logger_instance_ = instance; } return true; } - /// @brief Registering the remote sink to spdlog's Logger - /// @param logger_name The logger_name to init - /// @return bool success/failure - /// @private For internal use purpose only. - bool InitRawSink(const char* logger_name); - /// @brief Users need to call this interface to complete the registration of /// the remote plugin before the initialization of the trpc-cpp plugin /// @param raw_sink The LoggingPtr object associated with the raw sink @@ -149,18 +148,18 @@ class DefaultLog : public Log { } // Create an output logger for spdlog - bool createSpdLogger(const char* logger_name); + bool CreateSpdLogger(const char* logger_name); private: // Default queue length static constexpr size_t kThreadPoolQueueSize = 100000; // Initialization flags - bool inited_{false}; + bool initted_{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}; + bool initted_trpc_logger_instance_{false}; // tRPC-Cpp framework logger instance DefaultLog::Logger trpc_logger_instance_; diff --git a/trpc/util/log/default/sinks/local_file/local_file_sink.cc b/trpc/util/log/default/sinks/local_file/local_file_sink.cc index 16f943cb..e6c23104 100644 --- a/trpc/util/log/default/sinks/local_file/local_file_sink.cc +++ b/trpc/util/log/default/sinks/local_file/local_file_sink.cc @@ -27,7 +27,7 @@ int LocalFileSink::Init(const LocalFileSinkConfig& config) { } else if (config_.roll_type == "by_day") { sink_ = std::make_shared(config_.filename, config_.rotation_hour, config_.rotation_minute, false, config_.reserve_count); - if (config_.remove_timout_file_switch) { + if (config_.remove_timeout_file_switch) { RemoveTimeoutDailyLogFile(config_.filename, config_.reserve_count); } } else if (config_.roll_type == "by_hour") { @@ -82,7 +82,7 @@ bool LocalFileSink::RemoveTimeoutDailyLogFile(const std::string& file_name, int if (timeout_flag) { int ret = spdlog::details::os::remove_if_exists(file.first); if (ret == 0) { - std::cout << "delete timeout daily log succ, filename:" << file.first << std::endl; + std::cout << "delete timeout daily log succ, filename: " << file.first << std::endl; } } } diff --git a/trpc/util/log/logging.h b/trpc/util/log/logging.h index 5d5e585c..0ad1b435 100644 --- a/trpc/util/log/logging.h +++ b/trpc/util/log/logging.h @@ -18,6 +18,7 @@ #include #include +#include "trpc/common/config/default_value.h" #include "trpc/util/log/log.h" #include "trpc/util/log/printf_like.h" #include "trpc/util/log/python_like.h" @@ -39,8 +40,7 @@ void Destroy(); ///// For example, if min_level=1 (debug) and info is the level, then true is returned. bool IsLogOn(const char* instance_name, trpc::Log::Level level); -// Constant "default" -constexpr char kTrpcLogCacheStringDefault[] = "default"; +using trpc::kTrpcLogCacheStringDefault; } // namespace trpc::log