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, error) {
	config := fluent.Config{
		FluentHost: fHost,
		FluentPort: fPort,
		// WriteTimeout: -1,
	}
	fluent, err := fluent.New(config)
	if err != nil {
		return nil, err
	}

	return &Logger{fluent, appName}, nil
}

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()
}