package logshipper import ( "fmt" "io" "log" "strings" ) // Logger writes each line to the console exactly as the standard log package would, and ships it to // OmniLogger at the severity the caller names. Use it where the caller knows the severity, instead of // Writer, which has to guess the severity from the wording of the line. // // The console writer must be the writer the service logs to without the shipper, such as os.Stderr, so a // line is never shipped twice. Create the Logger after the service has set its log flags and prefix, which // NewLogger copies. A nil shipper makes the Logger console-only. type Logger struct { console *log.Logger shipper *Shipper } // NewLogger returns a Logger that writes to console with the standard log flags and prefix in effect now. func NewLogger(console io.Writer, shipper *Shipper) *Logger { return &Logger{console: log.New(console, log.Prefix(), log.Flags()), shipper: shipper} } // output writes one line to the console and ships it. The call depth of 3 makes the console's file and line // flags name the caller of Debugf, Infof, and the other methods, not this file. func (l *Logger) output(sev Level, format string, args ...any) { msg := fmt.Sprintf(format, args...) _ = l.console.Output(3, msg) if l.shipper != nil { l.shipper.Ship(sev, nil, strings.TrimRight(msg, "\n")) } } // Debugf logs at debug severity. func (l *Logger) Debugf(format string, args ...any) { l.output(LevelDebug, format, args...) } // Infof logs at info severity. func (l *Logger) Infof(format string, args ...any) { l.output(LevelInfo, format, args...) } // Warnf logs at warn severity. func (l *Logger) Warnf(format string, args ...any) { l.output(LevelWarn, format, args...) } // Errorf logs at error severity. func (l *Logger) Errorf(format string, args ...any) { l.output(LevelError, format, args...) } // Criticalf logs at critical severity. func (l *Logger) Criticalf(format string, args ...any) { l.output(LevelCritical, format, args...) }