From 50554967153ed0b874d97d46a5f97b2cc3346e32 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Mon, 16 May 2022 12:39:41 -0700 Subject: [PATCH 1/2] Added field for connected db to logger --- log.go | 7 +++++-- server/context.go | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/log.go b/log.go index 2e88bd1ac0..28869e79c4 100644 --- a/log.go +++ b/log.go @@ -20,8 +20,11 @@ import ( "github.com/sirupsen/logrus" ) -const ConnectionIdLogField = "connectionID" -const ConnectTimeLogKey = "connectTime" +const ( + ConnectionIdLogField = "connectionID" + ConnectionDbLogField = "connectionDb" + ConnectTimeLogKey = "connectTime" +) func init() { // V quickly checks if the logging verbosity meets a threshold. diff --git a/server/context.go b/server/context.go index 1a04d9ad99..7aa3a5fffe 100644 --- a/server/context.go +++ b/server/context.go @@ -134,6 +134,7 @@ func (s *SessionManager) SetDB(conn *mysql.Conn, db string) error { return sql.ErrDatabaseNotFound.New(db) } + sess.SetLogger(ctx.GetLogger().WithField(sqle.ConnectionDbLogField, db)) sess.SetCurrentDatabase(db) return nil } From 9d4d7bee09c4f25f2bc499c12631c009bf0ffb79 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Mon, 16 May 2022 12:46:26 -0700 Subject: [PATCH 2/2] Moved some log constants around so we can set them in USE --- log.go | 6 ------ server/context.go | 7 +++---- server/handler.go | 4 ++-- sql/log.go | 21 +++++++++++++++++++++ sql/plan/use.go | 2 ++ 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100755 sql/log.go diff --git a/log.go b/log.go index 28869e79c4..426e76d083 100644 --- a/log.go +++ b/log.go @@ -20,12 +20,6 @@ import ( "github.com/sirupsen/logrus" ) -const ( - ConnectionIdLogField = "connectionID" - ConnectionDbLogField = "connectionDb" - ConnectTimeLogKey = "connectTime" -) - func init() { // V quickly checks if the logging verbosity meets a threshold. vtlog.V = func(level glog.Level) glog.Verbose { diff --git a/server/context.go b/server/context.go index 7aa3a5fffe..0756b6b306 100644 --- a/server/context.go +++ b/server/context.go @@ -23,7 +23,6 @@ import ( "github.com/opentracing/opentracing-go" "github.com/sirupsen/logrus" - sqle "github.com/dolthub/go-mysql-server" "github.com/dolthub/go-mysql-server/sql" "github.com/dolthub/go-mysql-server/sql/grant_tables" ) @@ -116,8 +115,8 @@ func (s *SessionManager) NewSession(ctx context.Context, conn *mysql.Conn) error } s.sessions[conn.ConnectionID].session.SetLogger( - logger.WithField(sqle.ConnectionIdLogField, conn.ConnectionID). - WithField(sqle.ConnectTimeLogKey, time.Now()), + logger.WithField(sql.ConnectionIdLogField, conn.ConnectionID). + WithField(sql.ConnectTimeLogKey, time.Now()), ) return err @@ -134,7 +133,7 @@ func (s *SessionManager) SetDB(conn *mysql.Conn, db string) error { return sql.ErrDatabaseNotFound.New(db) } - sess.SetLogger(ctx.GetLogger().WithField(sqle.ConnectionDbLogField, db)) + sess.SetLogger(ctx.GetLogger().WithField(sql.ConnectionDbLogField, db)) sess.SetCurrentDatabase(db) return nil } diff --git a/server/handler.go b/server/handler.go index f1a15564dc..f5cc626cb5 100644 --- a/server/handler.go +++ b/server/handler.go @@ -88,7 +88,7 @@ func (h *Handler) NewConnection(c *mysql.Conn) { } c.DisableClientMultiStatements = h.disableMultiStmts - logrus.WithField(sqle.ConnectionIdLogField, c.ConnectionID).WithField("DisableClientMultiStatements", c.DisableClientMultiStatements).Infof("NewConnection") + logrus.WithField(sql.ConnectionIdLogField, c.ConnectionID).WithField("DisableClientMultiStatements", c.DisableClientMultiStatements).Infof("NewConnection") } func (h *Handler) ComInitDB(c *mysql.Conn, schemaName string) error { @@ -149,7 +149,7 @@ func (h *Handler) ConnectionClosed(c *mysql.Conn) { defer h.e.CloseSession(ctx) - logrus.WithField(sqle.ConnectionIdLogField, c.ConnectionID).Infof("ConnectionClosed") + logrus.WithField(sql.ConnectionIdLogField, c.ConnectionID).Infof("ConnectionClosed") } func (h *Handler) ComMultiQuery( diff --git a/sql/log.go b/sql/log.go new file mode 100755 index 0000000000..5b29d56f63 --- /dev/null +++ b/sql/log.go @@ -0,0 +1,21 @@ +// Copyright 2022 Dolthub, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sql + +const ( + ConnectionIdLogField = "connectionID" + ConnectionDbLogField = "connectionDb" + ConnectTimeLogKey = "connectTime" +) diff --git a/sql/plan/use.go b/sql/plan/use.go index 800bd2e710..c85413e8f9 100644 --- a/sql/plan/use.go +++ b/sql/plan/use.go @@ -68,6 +68,8 @@ func (u *Use) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) { } ctx.SetCurrentDatabase(dbName) + ctx.SetLogger(ctx.GetLogger().WithField(sql.ConnectionDbLogField, dbName)) + return sql.RowsToRowIter(), nil }