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) {
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)))
}
+22 -2
View File
@@ -14,9 +14,18 @@ import (
type LoggerImpl struct {
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)