package main import ( "fmt" "io" "log" "os" "strings" "repos.gfranz.ch/Gabriel20xx/shared/backend/logshipper" ) // omniShipper mirrors the server's console lines to OmniLogger. It is always set: with no OMNILOGGER_URL or // OMNILOGGER_INGEST_TOKEN it is disabled and ships nothing, until main hands it ProjectConfigurator's // pushed token (see omniShipper.Follow in server.go). var omniShipper = newOmniLoggerShipper() // newOmniLoggerShipper returns a shipper for OmniLogger. OMNILOGGER_URL and OMNILOGGER_INGEST_TOKEN are // optional initial values; ProjectConfigurator normally supplies the token at runtime. func newOmniLoggerShipper() *logshipper.Shipper { return logshipper.New(logshipper.Config{ BaseURL: strings.TrimSpace(os.Getenv("OMNILOGGER_URL")), IngestToken: strings.TrimSpace(os.Getenv("OMNILOGGER_INGEST_TOKEN")), Service: "collabtable", }) } // stdoutLog and stderrLog are the Loggers behind the helpers below. Each writes its line to its console stream // exactly as the fmt function it replaced did, and ships the line to OmniLogger at the severity the caller names. var ( stdoutLog = newConsoleLogger(os.Stdout, omniShipper) stderrLog = newConsoleLogger(os.Stderr, omniShipper) ) // newConsoleLogger returns a Logger for console. NewLogger copies the standard logger's flags, and these helper // lines have never carried a timestamp, so the flags are cleared only while the Logger is made. The standard // logger keeps its own flags. func newConsoleLogger(console io.Writer, shipper *logshipper.Shipper) *logshipper.Logger { previous := log.Flags() log.SetFlags(0) defer log.SetFlags(previous) return logshipper.NewLogger(console, shipper) } // logPrintf, logPrintln, logErrorf and logErrorln write to stdout or stderr exactly as the fmt functions // they wrap, and also ship the line to OmniLogger when shipping is enabled, at the severity the caller // names. The console stream is the one the helper always used, whatever the severity. func logPrintf(sev logshipper.Level, format string, args ...any) { logLine(stdoutLog, sev, fmt.Sprintf(format, args...)) } func logPrintln(sev logshipper.Level, args ...any) { logLine(stdoutLog, sev, fmt.Sprintln(args...)) } func logErrorf(sev logshipper.Level, format string, args ...any) { logLine(stderrLog, sev, fmt.Sprintf(format, args...)) } func logErrorln(sev logshipper.Level, args ...any) { logLine(stderrLog, sev, fmt.Sprintln(args...)) } // logLine writes msg to l's console and ships it at sev. msg keeps the newline its fmt call added, so the console // line is the same bytes as before. Levels outside the five OmniLogger knows are reported as errors. func logLine(l *logshipper.Logger, sev logshipper.Level, msg string) { switch sev { case logshipper.LevelDebug: l.Debugf("%s", msg) case logshipper.LevelInfo: l.Infof("%s", msg) case logshipper.LevelWarn: l.Warnf("%s", msg) case logshipper.LevelCritical: l.Criticalf("%s", msg) default: l.Errorf("%s", msg) } } // exitAfterFlush sends the buffered log lines to OmniLogger and then exits. Use it instead of os.Exit after a // line that must reach OmniLogger, such as a critical startup failure, which os.Exit would otherwise lose. func exitAfterFlush(code int) { omniShipper.Flush() os.Exit(code) }