mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-31 03:51:23 +01:00
Merge pull request #7724 from reasonerjt/logger-more-info
print more sectors of file path in logger
This commit is contained in:
commit
f2abfcb598
@ -19,12 +19,15 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var logger = New(os.Stdout, NewTextFormatter(), WarningLevel, 4)
|
||||
|
||||
const srcSeparator = "harbor" + string(os.PathSeparator) + "src"
|
||||
|
||||
func init() {
|
||||
lvl := os.Getenv("LOG_LEVEL")
|
||||
if len(lvl) == 0 {
|
||||
@ -148,7 +151,7 @@ func (l *Logger) Debugf(format string, v ...interface{}) {
|
||||
// Info ...
|
||||
func (l *Logger) Info(v ...interface{}) {
|
||||
if l.lvl <= InfoLevel {
|
||||
record := NewRecord(time.Now(), fmt.Sprint(v...), "", InfoLevel)
|
||||
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), InfoLevel)
|
||||
l.output(record)
|
||||
}
|
||||
}
|
||||
@ -156,7 +159,7 @@ func (l *Logger) Info(v ...interface{}) {
|
||||
// Infof ...
|
||||
func (l *Logger) Infof(format string, v ...interface{}) {
|
||||
if l.lvl <= InfoLevel {
|
||||
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", InfoLevel)
|
||||
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), InfoLevel)
|
||||
l.output(record)
|
||||
}
|
||||
}
|
||||
@ -164,7 +167,7 @@ func (l *Logger) Infof(format string, v ...interface{}) {
|
||||
// Warning ...
|
||||
func (l *Logger) Warning(v ...interface{}) {
|
||||
if l.lvl <= WarningLevel {
|
||||
record := NewRecord(time.Now(), fmt.Sprint(v...), "", WarningLevel)
|
||||
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), WarningLevel)
|
||||
l.output(record)
|
||||
}
|
||||
}
|
||||
@ -172,7 +175,7 @@ func (l *Logger) Warning(v ...interface{}) {
|
||||
// Warningf ...
|
||||
func (l *Logger) Warningf(format string, v ...interface{}) {
|
||||
if l.lvl <= WarningLevel {
|
||||
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), "", WarningLevel)
|
||||
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), WarningLevel)
|
||||
l.output(record)
|
||||
}
|
||||
}
|
||||
@ -268,19 +271,15 @@ func Fatalf(format string, v ...interface{}) {
|
||||
logger.Fatalf(format, v...)
|
||||
}
|
||||
|
||||
func line(calldepth int) string {
|
||||
_, file, line, ok := runtime.Caller(calldepth)
|
||||
func line(callDepth int) string {
|
||||
_, file, line, ok := runtime.Caller(callDepth)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
|
||||
for i := len(file) - 2; i > 0; i-- {
|
||||
if file[i] == os.PathSeparator {
|
||||
file = file[i+1:]
|
||||
break
|
||||
}
|
||||
l := strings.SplitN(file, srcSeparator, 2)
|
||||
if len(l) > 0 {
|
||||
file = l[1]
|
||||
}
|
||||
|
||||
return fmt.Sprintf("[%s:%d]:", file, line)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user