diff --git a/config/config.go b/config/config.go index 860c66005..a1662fe49 100644 --- a/config/config.go +++ b/config/config.go @@ -68,7 +68,7 @@ func InitConfig() { V2BotUrl = os.Getenv("V2_BOT_URL") V2BotToken = os.Getenv("V2_BOT_TOKEN") FfWebsocket = os.Getenv("FF_WEBSOCKET") == "true" - LogLevel = os.Getenv("LOG_LEVEL") + LogLevel = strings.ToUpper(os.Getenv("LOG_LEVEL")) // Add to super admins SuperAdmins = StripSuperAdmins(AdminStrings) diff --git a/go.mod b/go.mod index 8b0898986..3a073f373 100644 --- a/go.mod +++ b/go.mod @@ -61,6 +61,7 @@ require ( github.com/onsi/gomega v1.26.0 // indirect github.com/robfig/cron v1.2.0 github.com/urfave/negroni v1.0.0 // indirect + github.com/xhd2015/xgo/runtime v1.0.52 // indirect golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect gopkg.in/go-playground/assert.v1 v1.2.1 // indirect ) diff --git a/go.sum b/go.sum index d389d674c..d4ae95b46 100644 --- a/go.sum +++ b/go.sum @@ -2168,6 +2168,8 @@ github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= +github.com/xhd2015/xgo/runtime v1.0.52 h1:njcRzY3Xo2AFu/qQSC4ak9+JN7xFmaI3iEUyJxoErWM= +github.com/xhd2015/xgo/runtime v1.0.52/go.mod h1:9GBQ2SzJCzpD3T+HRN+2C0TUOGv7qIz4s0mad1xJ8Jo= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= diff --git a/routes/people.go b/routes/people.go index 1f11e5735..9a9d61f8f 100644 --- a/routes/people.go +++ b/routes/people.go @@ -15,10 +15,10 @@ func PeopleRoutes() chi.Router { peopleHandler := handlers.NewPeopleHandler(db.DB) r.Group(func(r chi.Router) { - r.Get("/", utils.TraceWithLogging(peopleHandler.GetListedPeople)) - r.Get("/search", utils.TraceWithLogging(peopleHandler.GetPeopleBySearch)) - r.Get("/posts", utils.TraceWithLogging(handlers.GetListedPosts)) - r.Get("/wanteds/assigned/{uuid}", bountyHandler.GetPersonAssignedBounties) + r.Get("/", utils.AutoLog(peopleHandler.GetListedPeople)) + r.Get("/search", utils.AutoLog(peopleHandler.GetPeopleBySearch)) + r.Get("/posts", utils.AutoLog(handlers.GetListedPosts)) + r.Get("/wanteds/assigned/{uuid}", utils.AutoLog(bountyHandler.GetPersonAssignedBounties)) r.Get("/wanteds/created/{uuid}", bountyHandler.GetPersonCreatedBounties) r.Get("/wanteds/header", handlers.GetWantedsHeader) r.Get("/short", handlers.GetPeopleShortList) diff --git a/routes/person.go b/routes/person.go index 1c98b4625..5cb45c9bd 100644 --- a/routes/person.go +++ b/routes/person.go @@ -14,7 +14,7 @@ func PersonRoutes() chi.Router { r.Group(func(r chi.Router) { r.Get("/{pubkey}", peopleHandler.GetPersonByPubkey) r.Get("/id/{id}", peopleHandler.GetPersonById) - r.Get("/uuid/{uuid}", utils.TraceWithLogging(peopleHandler.GetPersonByUuid)) + r.Get("/uuid/{uuid}", utils.AutoLog(peopleHandler.GetPersonByUuid)) r.Get("/uuid/{uuid}/assets", handlers.GetPersonAssetsByUuid) r.Get("/githubname/{github}", handlers.GetPersonByGithubName) }) diff --git a/routes/test_routes.go b/routes/test_routes.go index 5b944adf5..92a82b872 100644 --- a/routes/test_routes.go +++ b/routes/test_routes.go @@ -2,10 +2,6 @@ package routes import ( "net/http" - "runtime/trace" - "log" -// "os" - "bytes" "github.com/go-chi/chi" ) @@ -14,23 +10,7 @@ func TestRoutes() chi.Router { r := chi.NewRouter() r.Get("/internal-server-error", func(w http.ResponseWriter, r *http.Request) { - // Enable tracing - //f, err := os.Create("trace.out") - var buf bytes.Buffer - //if err != nil { - // log.Fatalf("Failed to create trace output file: %v", err) - //} - //defer f.Close() - - if err := trace.Start(&buf); err != nil { - log.Fatalf("Failed to start trace: %v", err) - } - defer func() { - trace.Stop() - log.Println("Trace Data:") - log.Println(buf.String()) - }() - //panic("Forced internal server error") + panic("Forced internal server error") }) return r diff --git a/utils/logger.go b/utils/logger.go index e32d9f06f..e4685b0d4 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -3,7 +3,7 @@ package utils import ( "log" "os" - "strings" + "github.com/stakwork/sphinx-tribes/config" ) type Logger struct { @@ -12,7 +12,6 @@ type Logger struct { errorLogger *log.Logger debugLogger *log.Logger machineLogger *log.Logger - logLevel string } var Log = Logger{ @@ -21,35 +20,34 @@ var Log = Logger{ errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile), machineLogger: log.New(os.Stdout, "MACHINE: ", log.Ldate|log.Ltime|log.Lshortfile), - logLevel: strings.ToUpper(os.Getenv("LOG_LEVEL")), } func (l *Logger) Machine(format string, v ...interface{}) { - if l.logLevel == "MACHINE" { + if config.LogLevel == "MACHINE" { l.machineLogger.Printf(format, v...) } } func (l *Logger) Debug(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" { l.debugLogger.Printf(format, v...) } } func (l *Logger) Info(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" { l.infoLogger.Printf(format, v...) } } func (l *Logger) Warning(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" { l.warningLogger.Printf(format, v...) } } func (l *Logger) Error(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" || l.logLevel == "ERROR" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" || config.LogLevel == "ERROR" { l.errorLogger.Printf(format, v...) } } diff --git a/utils/trace.go b/utils/trace.go index d7570c89c..650603613 100644 --- a/utils/trace.go +++ b/utils/trace.go @@ -1,81 +1,28 @@ package utils import ( - "bytes" - "log" + "context" "net/http" - "regexp" - "runtime/trace" + "fmt" "strings" + "github.com/xhd2015/xgo/runtime/core" + "github.com/xhd2015/xgo/runtime/trap" ) -func TraceWithLogging(next http.HandlerFunc) http.HandlerFunc { +func AutoLog(fn http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var buf bytes.Buffer - - // Start trace capturing - if err := trace.Start(&buf); err != nil { - http.Error(w, "Failed to start trace", http.StatusInternalServerError) - log.Fatalf("Failed to start trace: %v", err) - return - } - defer func() { - trace.Stop() - - // Extract and process trace output - traceOutput := buf.String() - //log.Println("Raw Trace Output (for debugging):", traceOutput) - // Split the trace output into lines - traceLines := strings.Split(traceOutput, "\n") - startedFormatting := false - - // Log each trace line with a line number and format - for _, line := range traceLines { - if !startedFormatting && strings.Contains(line, "start trace") { - startedFormatting = true - } else { - continue + trap.AddInterceptor(&trap.Interceptor{ + Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { + index := strings.Index(f.File, "sphinx-tribes") + trimmed := f.File + if index != -1 { + trimmed = f.File[index:] } + fmt.Printf("%s:%d %s\n", trimmed, f.Line, f.Name) - if line != "" { - traceFormatted := formatTrace(line) - for j, formattedLine := range traceFormatted { - log.Printf("%03d %s", j+1, formattedLine) - } - } - } - }() - - next(w, r) + return nil, nil + }, + }) + fn(w, r) } } - -func splitTrace(traceString string) []string { - // Define the regex pattern for detecting file paths ending with .go - re := regexp.MustCompile(`/([^/\s]+\.go)`) - - // Replace each match with the file path followed by a newline - formattedTrace := re.ReplaceAllString(traceString, "$0\n") - - cleanRegex := regexp.MustCompile(`[^a-zA-Z0-9\s\./:_\-@()]`) - cleanedTrace := cleanRegex.ReplaceAllString(formattedTrace, "") - - - // Split the formatted trace into lines and return as a string array - return strings.Split(strings.TrimSpace(cleanedTrace), "\n") -} -func formatTrace(input string) []string { - // Find the position where "start trace" appears - startIdx := strings.Index(input, "start trace") - if startIdx == -1 { - return nil // "start trace" not found, return nil slice - } - - // Slice input string from "start trace" onwards - traceContent := input[startIdx:] - - // Use splitTrace to split and format the trace content - return splitTrace(traceContent) -} - -