package logger import "github.com/sirupsen/logrus" type Level int const ( TraceLevel Level = -1 + iota DebugLevel InfoLevel WarnLevel ErrorLevel PanicLevel FatalLevel ) // Logger is a generic logging interface, similar to logrus's [logrus.ValueLogger]. // It is used in Styx for logging, so that users can plug in their own logging implementations. type Logger interface { SetLevel(Level) GetLevel() Level Trace(...any) Tracef(string, ...any) Debug(...any) Debugf(string, ...any) Info(...any) Infof(string, ...any) Warn(...any) Warnf(string, ...any) Error(...any) Errorf(string, ...any) Panic(...any) Panicf(string, ...any) Fatal(...any) Fatalf(string, ...any) } type Structured interface { Logger // Err adds an error to the log entry and returns the new logger. Err(error) Structured // Value returns a new logger with the specified Value added to the log entry. Value(string, any) Structured // Values returns a new logger with the specified Values added to the log entry. Values(Values) Structured } type Values map[string]any // Alias. type V = Values // StandardLog is the logger used by the package-level exported functions. var StandardLog = NewStandardLogger() // SetLogger sets the logger used by the package-level exported functions. func SetLogger(logger Structured) { StandardLog = logger } // Get returns the logger used by the package-level exported functions. func Get() Structured { return StandardLog } type standardLogger struct { *logrus.Logger } // NewStandardLogger returns a new Structured logger that wraps the standard logrus logger. func NewStandardLogger() Structured { return standardLogger{logrus.StandardLogger()} } type standardLoggerEntry struct { standardLogger *logrus.Entry } func SetLevel(level Level) { StandardLog.SetLevel(level) } func (l standardLogger) SetLevel(level Level) { switch level { case TraceLevel: l.Logger.SetLevel(logrus.TraceLevel) case DebugLevel: l.Logger.SetLevel(logrus.DebugLevel) case InfoLevel: l.Logger.SetLevel(logrus.InfoLevel) case WarnLevel: l.Logger.SetLevel(logrus.WarnLevel) case ErrorLevel: l.Logger.SetLevel(logrus.ErrorLevel) case PanicLevel: l.Logger.SetLevel(logrus.PanicLevel) case FatalLevel: l.Logger.SetLevel(logrus.FatalLevel) } } func GetLevel() Level { return StandardLog.GetLevel() } func (l standardLogger) GetLevel() Level { switch l.Logger.GetLevel() { case logrus.TraceLevel: return TraceLevel case logrus.DebugLevel: return DebugLevel case logrus.InfoLevel: return InfoLevel case logrus.WarnLevel: return WarnLevel case logrus.ErrorLevel: return ErrorLevel case logrus.PanicLevel: return PanicLevel case logrus.FatalLevel: return FatalLevel default: return InfoLevel } } func Err(err error) Structured { return StandardLog.Err(err) } func (l standardLogger) Err(err error) Structured { return standardLoggerEntry{l, l.Logger.WithError(err)} } func Value(key string, value any) Structured { return StandardLog.Value(key, value) } func (l standardLogger) Value(key string, value any) Structured { return standardLoggerEntry{l, l.Logger.WithField(key, value)} } func (l standardLogger) Values(Values Values) Structured { return standardLoggerEntry{l, l.Logger.WithFields(logrus.Fields(Values))} } func (l standardLoggerEntry) Err(err error) Structured { return standardLoggerEntry{l.standardLogger, l.Entry.WithError(err)} } func (l standardLoggerEntry) Value(key string, value any) Structured { return standardLoggerEntry{l.standardLogger, l.Entry.WithField(key, value)} } func (l standardLoggerEntry) Values(Values Values) Structured { return standardLoggerEntry{l.standardLogger, l.Entry.WithFields(logrus.Fields(Values))} } // Trace logs a message at level Trace on the standard logger. func Trace(args ...any) { StandardLog.Trace(args...) } // Tracef logs a message at level Trace on the standard logger. func Tracef(format string, args ...any) { StandardLog.Tracef(format, args...) } // Debug logs a message at level Debug on the standard logger. func Debug(args ...any) { StandardLog.Debug(args...) } // Debugf logs a message at level Debug on the standard logger. func Debugf(format string, args ...any) { StandardLog.Debugf(format, args...) } // Info logs a message at level Info on the standard logger. func Info(args ...any) { StandardLog.Info(args...) } // Infof logs a message at level Info on the standard logger. func Infof(format string, args ...any) { StandardLog.Infof(format, args...) } // Warn logs a message at level Warn on the standard logger. func Warn(args ...any) { StandardLog.Warn(args...) } // Warnf logs a message at level Warn on the standard logger. func Warnf(format string, args ...any) { StandardLog.Warnf(format, args...) } // Error logs a message at level Error on the standard logger. func Error(args ...any) { StandardLog.Error(args...) } // Errorf logs a message at level Error on the standard logger. func Errorf(format string, args ...any) { StandardLog.Errorf(format, args...) } // Panic logs a message at level Panic on the standard logger. func Panic(args ...any) { StandardLog.Panic(args...) } // Panicf logs a message at level Panic on the standard logger. func Panicf(format string, args ...any) { StandardLog.Panicf(format, args...) } // Fatal logs a message at level Fatal on the standard logger then the process will exit. func Fatal(args ...any) { StandardLog.Fatal(args...) } // Fatalf logs a message at level Fatal on the standard logger then the process will exit. func Fatalf(format string, args ...any) { StandardLog.Fatalf(format, args...) }