update
This commit is contained in:
14
fluentd/config.go
Normal file
14
fluentd/config.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package fluentd
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ParseAddr(addr string) (string, int) {
|
||||
p := strings.Split(addr, ":")
|
||||
fHost := p[0]
|
||||
fPort, _ := strconv.Atoi(p[1])
|
||||
|
||||
return fHost, fPort
|
||||
}
|
||||
41
fluentd/logger.go
Normal file
41
fluentd/logger.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user