From d27aa6b60aa423f60961e5ff1b81af974e5db85a Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Thu, 15 Jan 2026 22:29:55 +0100 Subject: [PATCH] Add stdout logging support to LoggerImpl as an option --- logger.go | 27 ++++++++++++++++++++++----- open_telemetry_logger.go | 26 +++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/logger.go b/logger.go index 0416fa7..de8cd0a 100644 --- a/logger.go +++ b/logger.go @@ -9,21 +9,29 @@ var ErstellerLogger *LoggerImpl func LogDebug(message string, a ...any) { if ErstellerLogger == nil { - println(fmt.Sprintf(message, a...)) + PrintLogDebug(message, a) return } ErstellerLogger.LogDebug(message, a...) } +func PrintLogDebug(message string, a []any) { + println(fmt.Sprintf(message, a...)) +} + func Debug(a ...any) { if ErstellerLogger == nil { - stringValue := joinStrings(a) - println(stringValue) + PrintDebug(a) return } ErstellerLogger.Debug(a) } +func PrintDebug(a []any) { + stringValue := joinStrings(a) + println(stringValue) +} + func joinStrings(a []any) string { elementsToLog := []string{} @@ -35,16 +43,25 @@ func joinStrings(a []any) string { func LogError(message string, a ...any) { if ErstellerLogger == nil { - println(fmt.Sprintf("Error: %v", fmt.Sprintf(message, a...))) + PrintLogError(message, a) return } ErstellerLogger.LogError(message, a...) } +func PrintLogError(message string, a []any) { + println(fmt.Sprintf("Error: %v", fmt.Sprintf(message, a...))) +} + func Error(a ...any) { if ErstellerLogger == nil { - println(fmt.Sprint("Error: ", joinStrings(a))) + PrintError(a) + return } ErstellerLogger.Error(a) } + +func PrintError(a []any) { + println(fmt.Sprint("Error: ", joinStrings(a))) +} diff --git a/open_telemetry_logger.go b/open_telemetry_logger.go index 511e5cc..8794219 100644 --- a/open_telemetry_logger.go +++ b/open_telemetry_logger.go @@ -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)