simplelog/custom_logger.go

46 lines
935 B
Go
Raw Normal View History

2024-05-14 04:43:47 +00:00
package simplelog
import (
"log"
"os"
"github.com/mattn/go-isatty"
)
var (
relpServerURL = os.Getenv("LOGGER_RELP_URL")
webhookURL = os.Getenv("LOGGER_WEBHOOK_URL")
)
type CustomLogger struct {
handlers []Handler
}
func init() {
slog.SetDefaultLogger(NewCustomLogger())
}
2024-05-14 04:43:47 +00:00
func NewCustomLogger() *CustomLogger {
cl := &CustomLogger{}
if isatty.IsTerminal(os.Stdout.Fd()) {
cl.handlers = append(cl.handlers, NewConsoleHandler())
} else {
cl.handlers = append(cl.handlers, NewJSONHandler())
}
if relpServerURL != "" {
handler, err := NewRELPHandler(relpServerURL)
if err != nil {
log.Fatalf("Failed to initialize RELP handler: %v", err)
}
cl.handlers = append(cl.handlers, handler)
}
if webhookURL != "" {
handler, err := NewWebhookHandler(webhookURL)
if err != nil {
log.Fatalf("Failed to initialize Webhook handler: %v", err)
}
cl.handlers = append(cl.handlers, handler)
}
return cl
}