[lxc-devel] [lxd/master] Forward dqlite logging

freeekanayaka on Github lxc-bot at linuxcontainers.org
Wed Aug 1 12:56:34 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 395 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180801/3e6481ca/attachment.bin>
-------------- next part --------------
From 9af246f94588075772ef8f8cb53be17e34a1659c Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Wed, 1 Aug 2018 12:42:33 +0000
Subject: [PATCH 1/2] Redirect dqlite logging to lxd logging

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/cluster/gateway.go | 41 +++++++++++++++--------------------------
 lxd/daemon.go          |  1 +
 2 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/lxd/cluster/gateway.go b/lxd/cluster/gateway.go
index f6e3c6c006..1e93f3ba4a 100644
--- a/lxd/cluster/gateway.go
+++ b/lxd/cluster/gateway.go
@@ -471,7 +471,9 @@ func (g *Gateway) init() error {
 		provider := &raftAddressProvider{db: g.db}
 		server, err := dqlite.NewServer(
 			raft.Raft(), raft.Registry(), listener,
-			dqlite.WithServerAddressProvider(provider))
+			dqlite.WithServerAddressProvider(provider),
+			dqlite.WithServerLogFunc(DqliteLog),
+		)
 		if err != nil {
 			return errors.Wrap(err, "Failed to create dqlite server")
 		}
@@ -640,31 +642,18 @@ func dqliteMemoryDial(listener net.Listener) dqlite.DialFunc {
 // performing SQL queries against the dqlite server running on this node.
 const databaseEndpoint = "/internal/database"
 
-// Redirect dqlite's logs to our own logger
-func dqliteLog(configuredLevel string) func(level, message string) {
-	return func(level, message string) {
-		if level == "TRACE" {
-			// TODO: lxd has no TRACE level, so let's map it to
-			// DEBUG. However, ignore it altogether if the
-			// configured level is not TRACE, to save some CPU
-			// (since TRACE is quite verbose in dqlite).
-			if configuredLevel != "TRACE" {
-				return
-			}
-			level = "DEBUG"
-		}
-
-		message = fmt.Sprintf("DQLite: %s", message)
-		switch level {
-		case "DEBUG":
-			logger.Debug(message)
-		case "INFO":
-			logger.Info(message)
-		case "WARN":
-			logger.Warn(message)
-		default:
-			// Ignore any other log level.
-		}
+// DqliteLog redirects dqlite's logs to our own logger
+func DqliteLog(l dqlite.LogLevel, format string, a ...interface{}) {
+	format = fmt.Sprintf("Dqlite: %s", format)
+	switch l {
+	case dqlite.LogDebug:
+		logger.Debugf(format, a...)
+	case dqlite.LogInfo:
+		logger.Infof(format, a...)
+	case dqlite.LogWarn:
+		logger.Warnf(format, a...)
+	case dqlite.LogError:
+		logger.Errorf(format, a...)
 	}
 }
 
diff --git a/lxd/daemon.go b/lxd/daemon.go
index 451cc28f8f..9eb6479216 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -478,6 +478,7 @@ func (d *Daemon) init() error {
 			dqlite.WithDialFunc(d.gateway.DialFunc()),
 			dqlite.WithContext(d.gateway.Context()),
 			dqlite.WithConnectionTimeout(d.config.DqliteSetupTimeout),
+			dqlite.WithLogFunc(cluster.DqliteLog),
 		)
 		if err == nil {
 			break

From 0e44162ba1d29a86e7d4aff6da8acecd45d05352 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Wed, 1 Aug 2018 12:52:43 +0000
Subject: [PATCH 2/2] Fix unit test regression

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/cluster/gateway_test.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/cluster/gateway_test.go b/lxd/cluster/gateway_test.go
index 48fa86c4b5..076d2ad160 100644
--- a/lxd/cluster/gateway_test.go
+++ b/lxd/cluster/gateway_test.go
@@ -52,7 +52,7 @@ func TestGateway_Single(t *testing.T) {
 
 	leader, err := gateway.LeaderAddress()
 	assert.Equal(t, "", leader)
-	assert.EqualError(t, err, "node is not clustered")
+	assert.EqualError(t, err, "Node is not clustered")
 }
 
 // If there's a network address configured, we expose the gRPC endpoint with


More information about the lxc-devel mailing list