Add stdout logging support to LoggerImpl as an option

This commit is contained in:
Achim Rohn
2026-01-15 22:29:55 +01:00
parent ae015b7e3b
commit d27aa6b60a
2 changed files with 45 additions and 8 deletions
+23 -3
View File
@@ -13,10 +13,19 @@ import (
)
type LoggerImpl struct {
logger log.Logger
logger log.Logger
logToStdout bool
}
func NewLoggerImpl(name string, version string) *LoggerImpl {
type LoggerImplOption func(*LoggerImpl)
func PrintToStdout() LoggerImplOption {
return func(l *LoggerImpl) {
l.logToStdout = true
}
}
func NewLoggerImpl(name string, version string, opts ...LoggerImplOption) *LoggerImpl {
// Create an OTLP exporter
logExporter, err := otlploggrpc.New(context.Background())
if err != nil {
@@ -51,9 +60,13 @@ func NewLoggerImpl(name string, version string) *LoggerImpl {
// Create a logger
logger := provider.Logger("salezenify.logger")
return &LoggerImpl{
loggerImpl := &LoggerImpl{
logger: logger,
}
for _, option := range opts {
option(loggerImpl)
}
return loggerImpl
}
func (l *LoggerImpl) Debug(a ...any) {
@@ -63,6 +76,9 @@ func (l *LoggerImpl) Debug(a ...any) {
println(message)
return
}
if l.logToStdout {
PrintDebug(a)
}
// Use OpenTelemetry logger
ctx := context.Background()
@@ -86,6 +102,10 @@ func (l *LoggerImpl) Error(a ...any) {
return
}
if l.logToStdout {
PrintError(a)
}
// Use OpenTelemetry logger
ctx := context.Background()
message := joinStrings(a)