mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 05:18:25 +00:00 
			
		
		
		
	Deprecated gopid in log (#32932)
This commit is contained in:
		@@ -13,7 +13,6 @@ import (
 | 
				
			|||||||
type Event struct {
 | 
					type Event struct {
 | 
				
			||||||
	Time time.Time
 | 
						Time time.Time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	GoroutinePid string
 | 
					 | 
				
			||||||
	Caller   string
 | 
						Caller   string
 | 
				
			||||||
	Filename string
 | 
						Filename string
 | 
				
			||||||
	Line     int
 | 
						Line     int
 | 
				
			||||||
@@ -218,18 +217,17 @@ func EventFormatTextMessage(mode *WriterMode, event *Event, msgFormat string, ms
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if flags&Lgopid == Lgopid {
 | 
						if flags&Lgopid == Lgopid {
 | 
				
			||||||
		if event.GoroutinePid != "" {
 | 
							deprecatedGoroutinePid := "no-gopid" // use a dummy value to avoid breaking the log format
 | 
				
			||||||
		buf = append(buf, '[')
 | 
							buf = append(buf, '[')
 | 
				
			||||||
		if mode.Colorize {
 | 
							if mode.Colorize {
 | 
				
			||||||
			buf = append(buf, ColorBytes(FgHiYellow)...)
 | 
								buf = append(buf, ColorBytes(FgHiYellow)...)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
			buf = append(buf, event.GoroutinePid...)
 | 
							buf = append(buf, deprecatedGoroutinePid...)
 | 
				
			||||||
		if mode.Colorize {
 | 
							if mode.Colorize {
 | 
				
			||||||
			buf = append(buf, resetBytes...)
 | 
								buf = append(buf, resetBytes...)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		buf = append(buf, ']', ' ')
 | 
							buf = append(buf, ']', ' ')
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	buf = append(buf, msg...)
 | 
						buf = append(buf, msg...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if event.Stacktrace != "" && mode.StacktraceLevel <= event.Level {
 | 
						if event.Stacktrace != "" && mode.StacktraceLevel <= event.Level {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,14 +28,13 @@ func TestEventFormatTextMessage(t *testing.T) {
 | 
				
			|||||||
			Caller:     "caller",
 | 
								Caller:     "caller",
 | 
				
			||||||
			Filename:   "filename",
 | 
								Filename:   "filename",
 | 
				
			||||||
			Line:       123,
 | 
								Line:       123,
 | 
				
			||||||
			GoroutinePid: "pid",
 | 
					 | 
				
			||||||
			Level:      ERROR,
 | 
								Level:      ERROR,
 | 
				
			||||||
			Stacktrace: "stacktrace",
 | 
								Stacktrace: "stacktrace",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
 | 
							"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(t, `[PREFIX] 2020/01/02 03:04:05.000000 filename:123:caller [E] [pid] msg format: arg0 arg1
 | 
						assert.Equal(t, `[PREFIX] 2020/01/02 03:04:05.000000 filename:123:caller [E] [no-gopid] msg format: arg0 arg1
 | 
				
			||||||
	stacktrace
 | 
						stacktrace
 | 
				
			||||||
 | 
					
 | 
				
			||||||
`, string(res))
 | 
					`, string(res))
 | 
				
			||||||
@@ -46,12 +45,11 @@ func TestEventFormatTextMessage(t *testing.T) {
 | 
				
			|||||||
			Caller:     "caller",
 | 
								Caller:     "caller",
 | 
				
			||||||
			Filename:   "filename",
 | 
								Filename:   "filename",
 | 
				
			||||||
			Line:       123,
 | 
								Line:       123,
 | 
				
			||||||
			GoroutinePid: "pid",
 | 
					 | 
				
			||||||
			Level:      ERROR,
 | 
								Level:      ERROR,
 | 
				
			||||||
			Stacktrace: "stacktrace",
 | 
								Stacktrace: "stacktrace",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
 | 
							"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(t, "[PREFIX] \x1b[36m2020/01/02 03:04:05.000000 \x1b[0m\x1b[32mfilename:123:\x1b[32mcaller\x1b[0m \x1b[1;31m[E]\x1b[0m [\x1b[93mpid\x1b[0m] msg format: arg0 \x1b[34marg1\x1b[0m\n\tstacktrace\n\n", string(res))
 | 
						assert.Equal(t, "[PREFIX] \x1b[36m2020/01/02 03:04:05.000000 \x1b[0m\x1b[32mfilename:123:\x1b[32mcaller\x1b[0m \x1b[1;31m[E]\x1b[0m [\x1b[93mno-gopid\x1b[0m] msg format: arg0 \x1b[34marg1\x1b[0m\n\tstacktrace\n\n", string(res))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,7 +30,7 @@ const (
 | 
				
			|||||||
	LUTC                              // if Ldate or Ltime is set, use UTC rather than the local time zone
 | 
						LUTC                              // if Ldate or Ltime is set, use UTC rather than the local time zone
 | 
				
			||||||
	Llevelinitial                     // Initial character of the provided level in brackets, eg. [I] for info
 | 
						Llevelinitial                     // Initial character of the provided level in brackets, eg. [I] for info
 | 
				
			||||||
	Llevel                            // Provided level in brackets [INFO]
 | 
						Llevel                            // Provided level in brackets [INFO]
 | 
				
			||||||
	Lgopid                            // the Goroutine-PID of the context
 | 
						Lgopid                            // the Goroutine-PID of the context, deprecated and it is always a const value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Lmedfile  = Lshortfile | Llongfile                                    // last 20 characters of the filename
 | 
						Lmedfile  = Lshortfile | Llongfile                                    // last 20 characters of the filename
 | 
				
			||||||
	LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial // default
 | 
						LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial // default
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,19 +0,0 @@
 | 
				
			|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
 | 
					 | 
				
			||||||
// SPDX-License-Identifier: MIT
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
package log
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import "unsafe"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//go:linkname runtime_getProfLabel runtime/pprof.runtime_getProfLabel
 | 
					 | 
				
			||||||
func runtime_getProfLabel() unsafe.Pointer //nolint
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type labelMap map[string]string
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func getGoroutineLabels() map[string]string {
 | 
					 | 
				
			||||||
	l := (*labelMap)(runtime_getProfLabel())
 | 
					 | 
				
			||||||
	if l == nil {
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return *l
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,33 +0,0 @@
 | 
				
			|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
 | 
					 | 
				
			||||||
// SPDX-License-Identifier: MIT
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
package log
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"runtime/pprof"
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func Test_getGoroutineLabels(t *testing.T) {
 | 
					 | 
				
			||||||
	pprof.Do(context.Background(), pprof.Labels(), func(ctx context.Context) {
 | 
					 | 
				
			||||||
		currentLabels := getGoroutineLabels()
 | 
					 | 
				
			||||||
		pprof.ForLabels(ctx, func(key, value string) bool {
 | 
					 | 
				
			||||||
			assert.EqualValues(t, value, currentLabels[key])
 | 
					 | 
				
			||||||
			return true
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		pprof.Do(ctx, pprof.Labels("Test_getGoroutineLabels", "Test_getGoroutineLabels_child1"), func(ctx context.Context) {
 | 
					 | 
				
			||||||
			currentLabels := getGoroutineLabels()
 | 
					 | 
				
			||||||
			pprof.ForLabels(ctx, func(key, value string) bool {
 | 
					 | 
				
			||||||
				assert.EqualValues(t, value, currentLabels[key])
 | 
					 | 
				
			||||||
				return true
 | 
					 | 
				
			||||||
			})
 | 
					 | 
				
			||||||
			if assert.NotNil(t, currentLabels) {
 | 
					 | 
				
			||||||
				assert.EqualValues(t, "Test_getGoroutineLabels_child1", currentLabels["Test_getGoroutineLabels"])
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -200,11 +200,6 @@ func (l *LoggerImpl) Log(skip int, level Level, format string, logArgs ...any) {
 | 
				
			|||||||
		event.Stacktrace = Stack(skip + 1)
 | 
							event.Stacktrace = Stack(skip + 1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	labels := getGoroutineLabels()
 | 
					 | 
				
			||||||
	if labels != nil {
 | 
					 | 
				
			||||||
		event.GoroutinePid = labels["pid"]
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// get a simple text message without color
 | 
						// get a simple text message without color
 | 
				
			||||||
	msgArgs := make([]any, len(logArgs))
 | 
						msgArgs := make([]any, len(logArgs))
 | 
				
			||||||
	copy(msgArgs, logArgs)
 | 
						copy(msgArgs, logArgs)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user