Skip to content

Commit

Permalink
Merge pull request #1014 from dolthub/zachmu/logging
Browse files Browse the repository at this point in the history
Added field for connected db to logger
  • Loading branch information
zachmu authored May 16, 2022
2 parents 2c1ee84 + 9d4d7be commit de09194
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
3 changes: 0 additions & 3 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"github.com/sirupsen/logrus"
)

const ConnectionIdLogField = "connectionID"
const ConnectTimeLogKey = "connectTime"

func init() {
// V quickly checks if the logging verbosity meets a threshold.
vtlog.V = func(level glog.Level) glog.Verbose {
Expand Down
6 changes: 3 additions & 3 deletions server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -134,6 +133,7 @@ func (s *SessionManager) SetDB(conn *mysql.Conn, db string) error {
return sql.ErrDatabaseNotFound.New(db)
}

sess.SetLogger(ctx.GetLogger().WithField(sql.ConnectionDbLogField, db))
sess.SetCurrentDatabase(db)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions sql/log.go
Original file line number Diff line number Diff line change
@@ -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"
)
2 changes: 2 additions & 0 deletions sql/plan/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit de09194

Please sign in to comment.