go-api-pkg/fluentd/logger.go
2022-12-01 04:15:16 +01:00

42 lines
746 B
Go

package fluentd
import (
"fmt"
"log"
"github.com/fluent/fluent-logger-golang/fluent"
)
type Logger struct {
fluent *fluent.Fluent
appName string
}
func NewLogger(appName, fHost string, fPort int) *Logger {
config := fluent.Config{
FluentHost: fHost,
FluentPort: fPort,
// WriteTimeout: -1,
}
fluent, err := fluent.New(config)
if err != nil {
log.Panicf("Error connecting to %s: %v", fHost, err)
}
return &Logger{fluent, appName}
}
func (l *Logger) Log(format string, v ...any) {
mapData := map[string]string{
"message": fmt.Sprintf(format, v...),
}
err := l.fluent.Post(l.appName, mapData)
if err != nil {
log.Println("Error sending log: ", err)
}
}
func (l *Logger) Close() error {
return l.fluent.Close()
}