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
+22 -5
View File
@@ -9,21 +9,29 @@ var ErstellerLogger *LoggerImpl
func LogDebug(message string, a ...any) { func LogDebug(message string, a ...any) {
if ErstellerLogger == nil { if ErstellerLogger == nil {
println(fmt.Sprintf(message, a...)) PrintLogDebug(message, a)
return return
} }
ErstellerLogger.LogDebug(message, a...) ErstellerLogger.LogDebug(message, a...)
} }
func PrintLogDebug(message string, a []any) {
println(fmt.Sprintf(message, a...))
}
func Debug(a ...any) { func Debug(a ...any) {
if ErstellerLogger == nil { if ErstellerLogger == nil {
stringValue := joinStrings(a) PrintDebug(a)
println(stringValue)
return return
} }
ErstellerLogger.Debug(a) ErstellerLogger.Debug(a)
} }
func PrintDebug(a []any) {
stringValue := joinStrings(a)
println(stringValue)
}
func joinStrings(a []any) string { func joinStrings(a []any) string {
elementsToLog := []string{} elementsToLog := []string{}
@@ -35,16 +43,25 @@ func joinStrings(a []any) string {
func LogError(message string, a ...any) { func LogError(message string, a ...any) {
if ErstellerLogger == nil { if ErstellerLogger == nil {
println(fmt.Sprintf("Error: %v", fmt.Sprintf(message, a...))) PrintLogError(message, a)
return return
} }
ErstellerLogger.LogError(message, a...) ErstellerLogger.LogError(message, a...)
} }
func PrintLogError(message string, a []any) {
println(fmt.Sprintf("Error: %v", fmt.Sprintf(message, a...)))
}
func Error(a ...any) { func Error(a ...any) {
if ErstellerLogger == nil { if ErstellerLogger == nil {
println(fmt.Sprint("Error: ", joinStrings(a))) PrintError(a)
return
} }
ErstellerLogger.Error(a) ErstellerLogger.Error(a)
} }
func PrintError(a []any) {
println(fmt.Sprint("Error: ", joinStrings(a)))
}
+22 -2
View File
@@ -14,9 +14,18 @@ import (
type LoggerImpl struct { 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 // Create an OTLP exporter
logExporter, err := otlploggrpc.New(context.Background()) logExporter, err := otlploggrpc.New(context.Background())
if err != nil { if err != nil {
@@ -51,9 +60,13 @@ func NewLoggerImpl(name string, version string) *LoggerImpl {
// Create a logger // Create a logger
logger := provider.Logger("salezenify.logger") logger := provider.Logger("salezenify.logger")
return &LoggerImpl{ loggerImpl := &LoggerImpl{
logger: logger, logger: logger,
} }
for _, option := range opts {
option(loggerImpl)
}
return loggerImpl
} }
func (l *LoggerImpl) Debug(a ...any) { func (l *LoggerImpl) Debug(a ...any) {
@@ -63,6 +76,9 @@ func (l *LoggerImpl) Debug(a ...any) {
println(message) println(message)
return return
} }
if l.logToStdout {
PrintDebug(a)
}
// Use OpenTelemetry logger // Use OpenTelemetry logger
ctx := context.Background() ctx := context.Background()
@@ -86,6 +102,10 @@ func (l *LoggerImpl) Error(a ...any) {
return return
} }
if l.logToStdout {
PrintError(a)
}
// Use OpenTelemetry logger // Use OpenTelemetry logger
ctx := context.Background() ctx := context.Background()
message := joinStrings(a) message := joinStrings(a)