forked from microsoft/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pass contexts to checkBadConn * Add ContextLogger interface and optional implementation * ContextLogger unit tests and renaming * Add LogRetry config param * Categorize uncategorized log messages * Normalize Logger's use of prefixes * Make log_test compatible with Go 1.8 - 1.12 * Synchronize testLogger to prevent post-test logging * Show actual caller of testLogger Print functions * Remove t.Helper calls because go 1.8 doesn't support them * Update TestTypeSizesFromQuery to defer StopLogging
- Loading branch information
Showing
23 changed files
with
723 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,71 @@ | ||
package mssql | ||
|
||
import ( | ||
"log" | ||
"context" | ||
|
||
"github.com/denisenkom/go-mssqldb/msdsn" | ||
) | ||
|
||
const ( | ||
logErrors = uint64(msdsn.LogErrors) | ||
logMessages = uint64(msdsn.LogMessages) | ||
logRows = uint64(msdsn.LogRows) | ||
logSQL = uint64(msdsn.LogSQL) | ||
logParams = uint64(msdsn.LogParams) | ||
logTransaction = uint64(msdsn.LogTransaction) | ||
logDebug = uint64(msdsn.LogDebug) | ||
logRetries = uint64(msdsn.LogRetries) | ||
) | ||
|
||
// Logger is an interface you can implement to have the go-msqldb | ||
// driver automatically log detailed information on your behalf | ||
type Logger interface { | ||
Printf(format string, v ...interface{}) | ||
Println(v ...interface{}) | ||
} | ||
|
||
// ContextLogger is an interface that provides more information | ||
// than Logger and lets you log messages yourself. This gives you | ||
// more information to log (e.g. trace IDs in the context), more | ||
// control over the logging activity (e.g. log it, trace it, or | ||
// log and trace it, depending on the class of message), and lets | ||
// you log in exactly the format you want. | ||
type ContextLogger interface { | ||
Log(ctx context.Context, category msdsn.Log, msg string) | ||
} | ||
|
||
// optionalLogger implements the ContextLogger interface with | ||
// a default "do nothing" behavior that can be overridden by an | ||
// optional ContextLogger supplied by the user. | ||
type optionalLogger struct { | ||
logger Logger | ||
logger ContextLogger | ||
} | ||
|
||
func (o optionalLogger) Printf(format string, v ...interface{}) { | ||
if o.logger != nil { | ||
o.logger.Printf(format, v...) | ||
} else { | ||
log.Printf(format, v...) | ||
// Log does nothing unless the user has specified an optional | ||
// ContextLogger to override the "do nothing" default behavior. | ||
func (o optionalLogger) Log(ctx context.Context, category msdsn.Log, msg string) { | ||
if nil != o.logger { | ||
o.logger.Log(ctx, category, msg) | ||
} | ||
} | ||
|
||
func (o optionalLogger) Println(v ...interface{}) { | ||
if o.logger != nil { | ||
o.logger.Println(v...) | ||
} else { | ||
log.Println(v...) | ||
// loggerAdapter converts Logger interfaces into ContextLogger | ||
// interfaces. It provides backwards compatibility. | ||
type loggerAdapter struct { | ||
logger Logger | ||
} | ||
|
||
// Log passes the message to the underlying Logger interface's | ||
// Println function, emulating the orignal Logger behavior. | ||
func (la loggerAdapter) Log(_ context.Context, category msdsn.Log, msg string) { | ||
|
||
// Add prefix for certain categories | ||
switch category { | ||
case msdsn.LogErrors: | ||
msg = "ERROR: " + msg | ||
case msdsn.LogRetries: | ||
msg = "RETRY: " + msg | ||
} | ||
|
||
la.logger.Println(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// +build go1.13 | ||
|
||
package mssql | ||
|
||
import ( | ||
"io" | ||
"log" | ||
) | ||
|
||
func currentLogWriter() io.Writer { | ||
return log.Writer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// +build !go1.13 | ||
|
||
package mssql | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
func currentLogWriter() io.Writer { | ||
// There is no function for getting the current writer in versions of | ||
// Go older than 1.13, so we just return the default writer. | ||
return os.Stderr | ||
} |
Oops, something went wrong.