// Package logx is a small leveled logger. torly avoids the standard log // package so that the output format is identical on every target TinyGo // supports and nothing depends on log flags or output redirection. package logx import ( "fmt" "os" "sync" "time" ) // Level is a logging threshold. type Level int const ( LevelError Level = iota LevelWarn LevelInfo LevelDebug ) var ( mu sync.Mutex level = LevelInfo ) // SetLevel sets the threshold from a name. func SetLevel(s string) { switch s { case "error": level = LevelError case "warn", "warning": level = LevelWarn case "debug": level = LevelDebug default: level = LevelInfo } } func logf(l Level, tag, format string, args ...any) { if l > level { return } mu.Lock() defer mu.Unlock() ts := time.Now().Format("2006-01-02T15:04:05Z07:00") fmt.Fprintf(os.Stderr, "%s %s %s\n", ts, tag, fmt.Sprintf(format, args...)) } // Errorf logs at error level. func Errorf(format string, args ...any) { logf(LevelError, "ERROR", format, args...) } // Warnf logs at warn level. func Warnf(format string, args ...any) { logf(LevelWarn, "WARN", format, args...) } // Infof logs at info level. func Infof(format string, args ...any) { logf(LevelInfo, "INFO", format, args...) } // Debugf logs at debug level. func Debugf(format string, args ...any) { logf(LevelDebug, "DEBUG", format, args...) }