-
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.
github.com/coopnorge/go-logger/adapter/datadog provides a simple logging adapter for Datadog to ensure that logs from Datadog are properly formatted by go-logger before ingestion into Datadog.
- Loading branch information
Showing
6 changed files
with
243 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package datadog | ||
|
||
import ( | ||
"strings" | ||
|
||
coopLogger "github.com/coopnorge/go-logger" | ||
) | ||
|
||
// Logger is a logging adapter between gopkg.in/DataDog/dd-trace-go.v1/ddtrace | ||
// an go-logger, do not create this directly, use NewLogger() | ||
type Logger struct { | ||
instance *coopLogger.Logger | ||
} | ||
|
||
// NewLogger creates a new Gorm logger that passes message to go-logger | ||
// | ||
// To inject the logger into ddtrace use | ||
// | ||
// package main | ||
// | ||
// import ( | ||
// datadogLogger "github.com/coopnorge/go-logger/adapter/datadog" | ||
// | ||
// "gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
// ) | ||
// | ||
// func main() { | ||
// ddtrace.UseLogger(datadogLogger.NewLogger(datadogLogger.WithGlobalLogger())) | ||
// } | ||
func NewLogger(opts ...LoggerOption) *Logger { | ||
logger := &Logger{} | ||
for _, opt := range opts { | ||
opt.Apply(logger) | ||
} | ||
if logger.instance == nil { | ||
return nil | ||
} | ||
return logger | ||
} | ||
|
||
// Log writes statements to the log | ||
func (l *Logger) Log(msg string) { | ||
// Logs from gopkg.in/DataDog/dd-trace-go.v1/ddtrace will contain keywords | ||
// specifying the level of the log. | ||
if strings.Contains(msg, "DEBUG") { | ||
l.instance.Debug(msg) | ||
return | ||
} | ||
if strings.Contains(msg, "INFO") { | ||
l.instance.Info(msg) | ||
return | ||
} | ||
if strings.Contains(msg, "WARN") { | ||
l.instance.Warn(msg) | ||
return | ||
} | ||
if strings.Contains(msg, "ERROR") { | ||
l.instance.Error(msg) | ||
return | ||
} | ||
l.instance.Info(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,29 @@ | ||
package datadog | ||
|
||
import ( | ||
"testing" | ||
|
||
coopLogger "github.com/coopnorge/go-logger" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
) | ||
|
||
func TestSetup(_ *testing.T) { | ||
ddtrace.UseLogger(NewLogger(WithGlobalLogger())) | ||
} | ||
|
||
func TestGlobalLogger(_ *testing.T) { | ||
logger := NewLogger(WithGlobalLogger()) | ||
logger.Log("Datadog Tracer v1.63.0 DEBUG This is a test") | ||
logger.Log("Datadog Tracer v1.63.0 INFO This is a test") | ||
logger.Log("Datadog Tracer v1.63.0 WARN This is a test") | ||
logger.Log("Datadog Tracer v1.63.0 ERROR This is a test") | ||
} | ||
|
||
func TestCustomLogger(_ *testing.T) { | ||
logger := NewLogger(WithLogger(coopLogger.New())) | ||
logger.Log("Datadog Tracer v1.63.0 DEBUG This is a test") | ||
logger.Log("Datadog Tracer v1.63.0 INFO This is a test") | ||
logger.Log("Datadog Tracer v1.63.0 WARN This is a test") | ||
logger.Log("Datadog Tracer v1.63.0 ERROR This is a test") | ||
} |
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,32 @@ | ||
package datadog | ||
|
||
import ( | ||
coopLogger "github.com/coopnorge/go-logger" | ||
) | ||
|
||
// LoggerOption defines an applicator interface | ||
type LoggerOption interface { //nolint:all | ||
Apply(l *Logger) | ||
} | ||
|
||
// LoggerOptionFunc defines a function which modifies a logger | ||
type LoggerOptionFunc func(l *Logger) //nolint:all | ||
|
||
// Apply redirects a function call to the function receiver | ||
func (lof LoggerOptionFunc) Apply(l *Logger) { | ||
lof(l) | ||
} | ||
|
||
// WithGlobalLogger configures Grom to use our global logger | ||
func WithGlobalLogger() LoggerOption { | ||
return LoggerOptionFunc(func(l *Logger) { | ||
l.instance = coopLogger.Global() | ||
}) | ||
} | ||
|
||
// WithLogger configures Grom to use a logger instance | ||
func WithLogger(logger *coopLogger.Logger) LoggerOption { | ||
return LoggerOptionFunc(func(l *Logger) { | ||
l.instance = logger | ||
}) | ||
} |
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