if (timeFormat) {
config.d_structuredLoggingTimeFormat = *timeFormat;
}
+ config.d_structuredLoggingUseServerID = settings.structured.set_instance_from_server_id;
});
}
bool d_snmpEnabled{false};
bool d_snmpTrapsEnabled{false};
bool d_structuredLogging{false};
+ bool d_structuredLoggingUseServerID{false};
};
/* this part of the configuration can be updated at runtime via
}
appendKeyAndVal("LEVEL", std::to_string(entry.level));
appendKeyAndVal("PRIORITY", std::to_string(entry.d_priority));
+ if (dnsdist::configuration::getImmutableConfiguration().d_structuredLoggingUseServerID) {
+ appendKeyAndVal("INSTANCE", dnsdist::configuration::getCurrentRuntimeConfiguration().d_server_id);
+ }
if (entry.name) {
appendKeyAndVal("SUBSYSTEM", entry.name.value());
}
json.emplace("priority", std::to_string(entry.d_priority));
}
+ if (dnsdist::configuration::getImmutableConfiguration().d_structuredLoggingUseServerID) {
+ json.emplace("instance", dnsdist::configuration::getCurrentRuntimeConfiguration().d_server_id);
+ }
+
for (auto const& value : entry.values) {
json.emplace(value.first, value.second);
}
if (entry.d_priority != 0) {
buf << " prio=" << std::quoted(Logr::Logger::toString(entry.d_priority));
}
+ if (dnsdist::configuration::getImmutableConfiguration().d_structuredLoggingUseServerID) {
+ buf << " instance=" << std::quoted(dnsdist::configuration::getCurrentRuntimeConfiguration().d_server_id);
+ }
std::array<char, 64> timebuf{};
buf << " ts=" << std::quoted(convertTime(entry.d_timestamp, timebuf));
getLogger("setVerboseLogDestination")->error(Logr::Error, e.what(), "Error while opening the verbose logging destination file", "path", Logging::Loggable(dest)));
}
});
- luaCtx.writeFunction("setStructuredLogging", [](bool enable, std::optional<LuaAssociativeTable<std::string>> options) {
+ luaCtx.writeFunction("setStructuredLogging", [](bool enable, std::optional<LuaAssociativeTable<boost::variant<std::string, bool>>> options) {
std::optional<dnsdist::configuration::TimeFormat> format{};
std::string timeFormat;
std::string backend;
+ bool useServerID{false};
if (options) {
if (getOptionalValue<std::string>(options, "timeFormat", timeFormat) == 1) {
if (timeFormat == "numeric") {
}
}
getOptionalValue<std::string>(options, "backend", backend);
+ getOptionalValue<bool>(options, "setInstanceFromServerID", useServerID);
checkAllParametersConsumed("setStructuredLogging", options);
}
- dnsdist::configuration::updateImmutableConfiguration([enable, &backend, format](dnsdist::configuration::ImmutableConfiguration& config) {
+ dnsdist::configuration::updateImmutableConfiguration([enable, &backend, format, useServerID](dnsdist::configuration::ImmutableConfiguration& config) {
if (enable && !backend.empty()) {
config.d_loggingBackend = backend;
}
config.d_structuredLoggingTimeFormat = *format;
}
config.d_structuredLogging = enable;
+ config.d_structuredLoggingUseServerID = useServerID;
});
});
default: ""
version_added: "2.1.0"
description: |
- The backend used for structured logging output. See :doc:`../advanced/structured-logging-dictionary` for more details. Available backends are:
+ The backend used for structured logging output. See :doc:`../advanced/structured-logging-dictionary` for more details. Available backends are:
- * ``default``: use the traditional logging system to output structured logging information.
- * ``systemd-journal``: use ``systemd-journal``. When using this backend, provide ``-o verbose`` or simular output option to ``journalctl`` to view the full information.
- * ``json``: JSON objects are written to the standard error stream.
+ * ``default``: use the traditional logging system to output structured logging information.
+ * ``systemd-journal``: use ``systemd-journal``. When using this backend, provide ``-o verbose`` or simular output option to ``journalctl`` to view the full information.
+ * ``json``: JSON objects are written to the standard error stream.
+ - name: "set_instance_from_server_id"
+ type: "bool"
+ default: "false"
+ description: |
+ Add a field "instance" to each log line with the value of ``general.server_id``.
+ version_added: 2.1.0
logging:
description: "Logging settings"
.. versionchanged:: 2.1.0
The ``backend`` option has been added.
+ The ``setInstanceFromServerID`` option has been added
The ``levelPrefix`` option has no longer any effect because it was confusing. The log level is now always logged as ``level`` and the syslog priority, if any, as ``priority`` in all backends except the default one where it is named ``prio``
Set whether log messages should be in structured-logging format. This is turned off by default. See :doc:`../advanced/structured-logging-dictionary` for more details.
* ``backend``: string - The backend used for structured logging output, see below. Added in 2.1.0.
* ``timeFormat=format``: string - Set the time format. Supported values are ``ISO8601`` and ``numeric``. Default is ``numeric``.
* ``levelPrefix=prefix``: string - Set the prefix for the log level. Default is ``prio``. No longer supported as of 2.1.0.
+ * ``setInstanceFromServerID=false``: bool - Add the "instance" field with the value of the server ID (set with :func:`setServerID`) to each log line. Added in 2.1.0.
Available backends:
def testOK(self):
pass
+
+
+class TestStructuredLoggingDefaultBackendWithInstanceFromYaml(
+ TestStructuredLoggingDefaultBackendFromYaml
+):
+ _yaml_config_template = """---
+general:
+ server_id: "foobar"
+
+binds:
+ - listen_address: "127.0.0.1:%d"
+ protocol: Do53
+
+backends:
+ - address: "127.0.0.1:%d"
+ protocol: Do53
+
+logging:
+ structured:
+ enabled: true
+ set_instance_from_server_id: true
+"""
+ _checkConfigExpectedOutputPrefix = b'msg="Configuration OK" subsystem="setup"'
+
+
+class TestStructuredLoggingJSONBackendWithInstanceFromYaml(
+ TestStructuredLoggingJSONBackendFromYaml
+):
+ _yaml_config_template = """---
+general:
+ server_id: "foobar"
+
+binds:
+ - listen_address: "127.0.0.1:%d"
+ protocol: Do53
+
+backends:
+ - address: "127.0.0.1:%d"
+ protocol: Do53
+
+logging:
+ structured:
+ enabled: true
+ backend: "json"
+ set_instance_from_server_id: true
+"""
+ _checkConfigExpectedOutputPrefix = (
+ b'{"instance": "foobar", "level": "0", "msg": "Configuration OK", "path":'
+ )
+
+
+class TestStructuredLoggingDefaultBackendWithInstanceFromLua(
+ TestStructuredLoggingDefaultBackendFromLua
+):
+ _config_template = """
+setServerID("foobar")
+setStructuredLogging(true, {setInstanceFromServerID=true})
+
+newServer{address="127.0.0.1:%d"}
+"""
+ _checkConfigExpectedOutputPrefix = b'msg="Configuration OK" subsystem="setup" level="0" prio="Info" instance="foobar" ts='
+
+
+class TestStructuredLoggingJSONBackendWithInstanceFromLua(
+ TestStructuredLoggingJSONBackendFromLua
+):
+
+ _config_template = """
+setServerID("foobar")
+setStructuredLogging(true, {backend="json", setInstanceFromServerID=true})
+
+newServer{address="127.0.0.1:%d"}
+"""
+ _checkConfigExpectedOutputPrefix = (
+ b'{"instance": "foobar", "level": "0", "msg": "Configuration OK", "path":'
+ )