-
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
204 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,28 @@ | ||
package datadog | ||
|
||
|
||
import ( | ||
coopLogger "github.com/coopnorge/go-logger" | ||
) | ||
|
||
// Logger is a logging adapter between Datadog 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 | ||
func NewLogger(opts ...LoggerOption) *Logger { | ||
logger := &Logger{} | ||
for _, opt := range opts { | ||
opt.Apply(logger) | ||
} | ||
if logger.instance == nil { | ||
return nil | ||
} | ||
return logger | ||
} | ||
|
||
func (l *Logger) Log(msg string) { | ||
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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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("This is a test") | ||
} | ||
|
||
|
||
func TestCustomLogger(_ *testing.T) { | ||
logger := NewLogger(WithLogger(coopLogger.New())) | ||
logger.Log("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
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