]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Early priority filter for structured logging
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 20 Jun 2022 11:42:02 +0000 (13:42 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 20 Jun 2022 11:42:02 +0000 (13:42 +0200)
pdns/recursordist/rec-main.cc

index acf842f6fb84562c05d7598ed4c67a1189a6e348..78bb749ab416fe6b726133b9c23457a2296380af 100644 (file)
@@ -97,6 +97,7 @@ static std::atomic<uint32_t> s_counter;
 int g_argc;
 char** g_argv;
 static string s_structured_logger_backend;
+static Logger::Urgency s_logUrgency;
 
 /* without reuseport, all listeners share the same sockets */
 deferredAdd_t g_deferredAdds;
@@ -886,6 +887,10 @@ static const char* toTimestampStringMilli(const struct timeval& tv, char* buf, s
 #ifdef HAVE_SYSTEMD
 static void loggerSDBackend(const Logging::Entry& entry)
 {
+  Logger::Urgency u = entry.d_priority ? Logger::Urgency(entry.d_priority) : Logger::Info;
+  if (u > s_logUrgency) {
+    return;
+  }
   // We need to keep the string in mem until sd_journal_sendv has ben called
   vector<string> strings;
   auto appendKeyAndVal = [&strings](const string& k, const string& v) {
@@ -923,6 +928,10 @@ static void loggerBackend(const Logging::Entry& entry)
 {
   static thread_local std::stringstream buf;
 
+  Logger::Urgency u = entry.d_priority ? Logger::Urgency(entry.d_priority) : Logger::Info;
+  if (u > s_logUrgency) {
+    return;
+  }
   buf.str("");
   buf << "msg=" << std::quoted(entry.message);
   if (entry.error) {
@@ -945,7 +954,7 @@ static void loggerBackend(const Logging::Entry& entry)
     buf << " ";
     buf << v.first << "=" << std::quoted(v.second);
   }
-  Logger::Urgency u = entry.d_priority ? Logger::Urgency(entry.d_priority) : Logger::Info;
+
   g_log << u << buf.str() << endl;
 }
 
@@ -2784,17 +2793,17 @@ int main(int argc, char** argv)
 
     // Pick up options given on command line to setup logging asap.
     g_quiet = ::arg().mustDo("quiet");
-    Logger::Urgency logUrgency = (Logger::Urgency)::arg().asNum("loglevel");
+    s_logUrgency = (Logger::Urgency)::arg().asNum("loglevel");
     g_slogStructured = ::arg().mustDo("structured-logging");
     s_structured_logger_backend = ::arg()["structured-logging-backend"];
 
-    if (logUrgency < Logger::Error)
-      logUrgency = Logger::Error;
-    if (!g_quiet && logUrgency < Logger::Info) { // Logger::Info=6, Logger::Debug=7
-      logUrgency = Logger::Info; // if you do --quiet=no, you need Info to also see the query log
+    if (s_logUrgency < Logger::Error)
+      s_logUrgency = Logger::Error;
+    if (!g_quiet && s_logUrgency < Logger::Info) { // Logger::Info=6, Logger::Debug=7
+      s_logUrgency = Logger::Info; // if you do --quiet=no, you need Info to also see the query log
     }
-    g_log.setLoglevel(logUrgency);
-    g_log.toConsole(logUrgency);
+    g_log.setLoglevel(s_logUrgency);
+    g_log.toConsole(s_logUrgency);
 
     string configname = ::arg()["config-dir"] + "/recursor.conf";
     if (::arg()["config-name"] != "") {
@@ -2854,16 +2863,16 @@ int main(int argc, char** argv)
     ::arg().parse(argc, argv);
 
     g_quiet = ::arg().mustDo("quiet");
-    logUrgency = (Logger::Urgency)::arg().asNum("loglevel");
+    s_logUrgency = (Logger::Urgency)::arg().asNum("loglevel");
     g_slogStructured = ::arg().mustDo("structured-logging");
 
-    if (logUrgency < Logger::Error)
-      logUrgency = Logger::Error;
-    if (!g_quiet && logUrgency < Logger::Info) { // Logger::Info=6, Logger::Debug=7
-      logUrgency = Logger::Info; // if you do --quiet=no, you need Info to also see the query log
+    if (s_logUrgency < Logger::Error)
+      s_logUrgency = Logger::Error;
+    if (!g_quiet && s_logUrgency < Logger::Info) { // Logger::Info=6, Logger::Debug=7
+      s_logUrgency = Logger::Info; // if you do --quiet=no, you need Info to also see the query log
     }
-    g_log.setLoglevel(logUrgency);
-    g_log.toConsole(logUrgency);
+    g_log.setLoglevel(s_logUrgency);
+    g_log.toConsole(s_logUrgency);
 
     if (!::arg()["chroot"].empty() && !::arg()["api-config-dir"].empty()) {
       SLOG(g_log << Logger::Error << "Using chroot and enabling the API is not possible" << endl,