From: Otto Moerbeek Date: Mon, 20 Jun 2022 11:42:02 +0000 (+0200) Subject: Early priority filter for structured logging X-Git-Tag: auth-4.8.0-alpha0~51^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1e5eef5ea76a07a2832526951828c4b92d943935;p=thirdparty%2Fpdns.git Early priority filter for structured logging --- diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index acf842f6fb..78bb749ab4 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -97,6 +97,7 @@ static std::atomic 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 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,