]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Extend pdnslog to allow for structured key-value pairs
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 14 Oct 2024 11:56:39 +0000 (13:56 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 24 Oct 2024 09:23:06 +0000 (11:23 +0200)
pdns/lua-base4.cc
pdns/recursordist/docs/lua-scripting/logging.rst

index b3d5d1b90f8ba0a75e0e702d4b35fad05dd6957d..31323801ab9c43d5a6d9e2c7a1b010a3c2cbde60 100644 (file)
@@ -272,10 +272,16 @@ void BaseLua4::prepareContext() {
   d_lw->registerFunction<void (DNSRecord::*)(const std::string&)>("changeContent", [](DNSRecord& dr, const std::string& newContent) { dr.setContent(shared_ptr<DNSRecordContent>(DNSRecordContent::make(dr.d_type, 1, newContent))); });
 
   // pdnsload
-  d_lw->writeFunction("pdnslog", [](const std::string& msg, boost::optional<int> loglevel) {
-    SLOG(g_log << (Logger::Urgency)loglevel.get_value_or(Logger::Warning) << msg<<endl,
-         g_slog->withName("lua")->info(static_cast<Logr::Priority>(loglevel.get_value_or(Logr::Warning)), msg));
+  d_lw->writeFunction("pdnslog", [](const std::string& msg, boost::optional<int> loglevel, boost::optional<std::map<std::string, std::string>> values) {
+    auto log = g_slog->withName("lua");
+    if (values) {
+      for (const auto& [key, value] : *values) {
+        log = log->withValues(key, Logging::Loggable(value));
+      }
+    }
+    log->info(static_cast<Logr::Priority>(loglevel.get_value_or(Logr::Warning)), msg);
   });
+
   d_lw->writeFunction("pdnsrandom", [](boost::optional<uint32_t> maximum) {
     return maximum ? dns_random(*maximum) : dns_random_uint32();
   });
index 7591defd00c0a1c0b7a61e029fd0e511d5b0c084..1c537d864151e2984e1e483e8e0cd6fe22e66fec 100644 (file)
@@ -1,17 +1,23 @@
 Logging from the Lua scripts
 ============================
-To log messages with the main PowerDNS Recursor process, use :func:`pdnslog`.
-:func:`pdnslog` can also write out to a syslog loglevel if specified.
-Use ``pdnslog(message, pdns.loglevels.LEVEL)`` with the
-correct pdns.loglevels entry. Entries are listed in the following table:
+To log messages with the main PowerDNS :program:`Recursor` process, use :func:`pdnslog`.
+optionally specifying a syslog loglevel.
+
+.. versionchanged:: 5.2.0
+
+   Added table as optional argument.
 
 .. function:: pdnslog(message)
               pdnslog(message, level)
+              pdnslog(message, level, table)
 
-  Log ``message` at the Info level if ``level`` is not set.
+  Log ``message`` at the ``Warning`` level if ``level`` is not set.
 
-  :param str msg: The message to log
+  :param str msg: The message to log.
   :param int level: The log level to log at, see below.
+  :param table table: A table of ``key = value`` entries to add to the structured log message.
+
+The available loglevel values are listed in the following table:
 
   - All - ``pdns.loglevels.All``
   - Alert - ``pdns.loglevels.Alert``
@@ -22,3 +28,9 @@ correct pdns.loglevels entry. Entries are listed in the following table:
   - Info - ``pdns.loglevels.Info``
   - Debug - ``pdns.loglevels.Debug``
   - None - ``pdns.loglevels.None``
+
+An example logging statement:
+
+.. code-block:: Lua
+
+  pdnslog('You have been warned', pdns.loglevels.Warning, { times = 3, origin = 'documentation' })