mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor Router Logger (#17308)
Make router logger more friendly, show the related function name/file/line. [BREAKING] This PR substantially changes the logging format of the router logger. If you use this logging for monitoring e.g. fail2ban you will need to update this to match the new format.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -72,12 +73,16 @@ var (
|
||||
wayTooLong = ColorBytes(BgMagenta)
|
||||
)
|
||||
|
||||
// ColoredTime adds colors for time on log
|
||||
// ColoredTime converts the provided time to a ColoredValue for logging. The duration is always formatted in milliseconds.
|
||||
func ColoredTime(duration time.Duration) *ColoredValue {
|
||||
// the output of duration in Millisecond is more readable:
|
||||
// * before: "100.1ms" "100.1μs" "100.1s"
|
||||
// * better: "100.1ms" "0.1ms" "100100.0ms", readers can compare the values at first glance.
|
||||
str := fmt.Sprintf("%.1fms", float64(duration.Microseconds())/1000)
|
||||
for i, k := range durations {
|
||||
if duration < k {
|
||||
return NewColoredValueBytes(duration, &durationColors[i])
|
||||
return NewColoredValueBytes(str, &durationColors[i])
|
||||
}
|
||||
}
|
||||
return NewColoredValueBytes(duration, &wayTooLong)
|
||||
return NewColoredValueBytes(str, &wayTooLong)
|
||||
}
|
||||
|
@@ -288,4 +288,8 @@ func (l *LoggerAsWriter) Log(msg string) {
|
||||
func init() {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
prefix = strings.TrimSuffix(filename, "modules/log/log.go")
|
||||
if prefix == filename {
|
||||
// in case the source code file is moved, we can not trim the suffix, the code above should also be updated.
|
||||
panic("unable to detect correct package prefix, please update file: " + filename)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user