This is an automated email from the ASF dual-hosted git repository.

laiyingchun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new 013e2e16b feat(logging): Make it possible to print logs to stderr when 
using screen_logger (#2079)
013e2e16b is described below

commit 013e2e16b4c1926d7e00d93e7961ccd350bf4fd5
Author: Yingchun Lai <[email protected]>
AuthorDate: Mon Jul 22 11:23:22 2024 +0800

    feat(logging): Make it possible to print logs to stderr when using 
screen_logger (#2079)
    
    If using `screen_logger`, the logs are only printed to stdout.
    
    This patch introduces a new config
    `[tools.screen_logger]stderr_start_level_on_stdout`
    to define the lowest level of log messages to be copied to
    stderr in addition to stdout. This is useful for testing.
    
    A test using `ASSERT_DEATH` is added to check the logs are printed
    to stderr indeed.
---
 src/utils/logging_provider.h        |  3 +++
 src/utils/simple_logger.cpp         | 27 +++++++++++++++++++++------
 src/utils/simple_logger.h           |  7 +++----
 src/utils/test/fmt_logging_test.cpp |  4 +++-
 4 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/src/utils/logging_provider.h b/src/utils/logging_provider.h
index 8adec4743..ec6bec53d 100644
--- a/src/utils/logging_provider.h
+++ b/src/utils/logging_provider.h
@@ -71,7 +71,10 @@ protected:
 
     static logging_provider *create_default_instance();
 
+    logging_provider(log_level_t stderr_start_level) : 
_stderr_start_level(stderr_start_level) {}
+
     std::vector<std::unique_ptr<command_deregister>> _cmds;
+    const log_level_t _stderr_start_level;
 };
 
 void set_log_prefixed_message_func(std::function<std::string()> func);
diff --git a/src/utils/simple_logger.cpp b/src/utils/simple_logger.cpp
index 86145d4a8..6e56cbbca 100644
--- a/src/utils/simple_logger.cpp
+++ b/src/utils/simple_logger.cpp
@@ -79,6 +79,15 @@ DSN_DEFINE_uint64(
 DSN_DEFINE_validator(max_number_of_log_files_on_disk,
                      [](int32_t value) -> bool { return value > 0; });
 
+DSN_DEFINE_string(tools.screen_logger,
+                  stderr_start_level_on_stdout,
+                  "LOG_LEVEL_WARNING",
+                  "The lowest level of log messages to be copied to stderr in 
addition to stdout");
+DSN_DEFINE_validator(stderr_start_level_on_stdout, [](const char *value) -> 
bool {
+    const auto level = enum_from_string(value, LOG_LEVEL_INVALID);
+    return LOG_LEVEL_DEBUG <= level && level <= LOG_LEVEL_FATAL;
+});
+
 DSN_DEFINE_string(
     tools.simple_logger,
     stderr_start_level,
@@ -169,9 +178,15 @@ inline void process_fatal_log(log_level_t log_level)
 
 } // anonymous namespace
 
+screen_logger::screen_logger(const char *, const char *)
+    : logging_provider(enum_from_string(FLAGS_stderr_start_level_on_stdout, 
LOG_LEVEL_INVALID)),
+      _short_header(true)
+{
+}
+
 void screen_logger::print_header(log_level_t log_level)
 {
-    ::dsn::tools::print_header(stdout, LOG_LEVEL_COUNT, log_level);
+    ::dsn::tools::print_header(stdout, _stderr_start_level, log_level);
 }
 
 void screen_logger::print_long_header(const char *file,
@@ -180,12 +195,12 @@ void screen_logger::print_long_header(const char *file,
                                       log_level_t log_level)
 {
     ::dsn::tools::print_long_header(
-        stdout, file, function, line, _short_header, LOG_LEVEL_COUNT, 
log_level);
+        stdout, file, function, line, _short_header, _stderr_start_level, 
log_level);
 }
 
 void screen_logger::print_body(const char *body, log_level_t log_level)
 {
-    ::dsn::tools::print_body(stdout, body, LOG_LEVEL_COUNT, log_level);
+    ::dsn::tools::print_body(stdout, body, _stderr_start_level, log_level);
 }
 
 void screen_logger::log(
@@ -206,10 +221,10 @@ void screen_logger::log(
 void screen_logger::flush() { ::fflush(stdout); }
 
 simple_logger::simple_logger(const char *log_dir, const char *role_name)
-    : _log_dir(std::string(log_dir)),
+    : logging_provider(enum_from_string(FLAGS_stderr_start_level, 
LOG_LEVEL_INVALID)),
+      _log_dir(std::string(log_dir)),
       _log(nullptr),
-      _file_bytes(0),
-      _stderr_start_level(enum_from_string(FLAGS_stderr_start_level, 
LOG_LEVEL_INVALID))
+      _file_bytes(0)
 {
     // Use 'role_name' if it is specified, otherwise use 'base_name'.
     const std::string symlink_name(
diff --git a/src/utils/simple_logger.h b/src/utils/simple_logger.h
index 7fe8d79f8..5437c0fe6 100644
--- a/src/utils/simple_logger.h
+++ b/src/utils/simple_logger.h
@@ -44,7 +44,7 @@ namespace tools {
 class screen_logger : public logging_provider
 {
 public:
-    explicit screen_logger(const char *, const char *) : _short_header(true) {}
+    explicit screen_logger(const char *, const char *);
     ~screen_logger() override = default;
 
     void log(const char *file,
@@ -56,12 +56,12 @@ public:
     virtual void flush();
 
 private:
-    static void print_header(log_level_t log_level);
+    void print_header(log_level_t log_level);
     void print_long_header(const char *file,
                            const char *function,
                            const int line,
                            log_level_t log_level);
-    static void print_body(const char *body, log_level_t log_level);
+    void print_body(const char *body, log_level_t log_level);
 
     ::dsn::utils::ex_lock_nr _lock;
     const bool _short_header;
@@ -119,7 +119,6 @@ private:
     FILE *_log;
     // The byte size of the current log file.
     uint64_t _file_bytes;
-    const log_level_t _stderr_start_level;
 };
 } // namespace tools
 } // namespace dsn
diff --git a/src/utils/test/fmt_logging_test.cpp 
b/src/utils/test/fmt_logging_test.cpp
index 95e7d0dfd..20017ddec 100644
--- a/src/utils/test/fmt_logging_test.cpp
+++ b/src/utils/test/fmt_logging_test.cpp
@@ -27,13 +27,14 @@
 #include <fmt/core.h>
 #include <memory>
 
+#include "absl/strings/string_view.h"
 #include "common/gpid.h"
 #include "common/replication.codes.h"
 #include "gtest/gtest.h"
 #include "runtime/task/task_code.h"
 #include "utils/error_code.h"
 #include "utils/errors.h"
-#include "absl/strings/string_view.h"
+#include "utils/fmt_logging.h"
 
 namespace dsn {
 namespace replication {
@@ -47,6 +48,7 @@ TEST(fmt_logging, basic)
     ASSERT_EQ(fmt::format("{}", LPC_REPLICATION_LOW), "LPC_REPLICATION_LOW");
     ASSERT_EQ(absl::string_view("yes"), "yes");
     ASSERT_EQ(fmt::format("{}", absl::string_view("yes\0yes")), "yes\0yes");
+    ASSERT_DEATH(CHECK(false, "CHECK false in test"), "CHECK false in test");
 }
 
 } // namespace replication


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to