mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-16 20:01:35 +01:00
Merge pull request #11400 from heww/fix-issue-11391
fix(log): correct file and line when use logger
This commit is contained in:
commit
c0246e2130
84
src/lib/log/context_test.go
Normal file
84
src/lib/log/context_test.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright Project Harbor Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetLogger(t *testing.T) {
|
||||||
|
var (
|
||||||
|
expectedLevel = ErrorLevel.string()
|
||||||
|
expectLine = "context_test.go:32"
|
||||||
|
expectMsg = "message"
|
||||||
|
)
|
||||||
|
|
||||||
|
buf := enter()
|
||||||
|
defer exit()
|
||||||
|
|
||||||
|
G(context.TODO()).Errorf("%s", message)
|
||||||
|
|
||||||
|
str := buf.String()
|
||||||
|
if !contains(t, str, expectedLevel, expectLine, expectMsg) {
|
||||||
|
t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetLoggerWithFields(t *testing.T) {
|
||||||
|
var (
|
||||||
|
expectedLevel = ErrorLevel.string()
|
||||||
|
expectLine = "context_test.go:50"
|
||||||
|
expectMsg = "message"
|
||||||
|
)
|
||||||
|
|
||||||
|
buf := enter()
|
||||||
|
defer exit()
|
||||||
|
|
||||||
|
G(context.TODO()).WithFields(Fields{"action": "test"}).Errorf("%s", message)
|
||||||
|
|
||||||
|
str := buf.String()
|
||||||
|
if !contains(t, str, expectedLevel, expectLine, expectMsg) {
|
||||||
|
t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !contains(t, str, expectedLevel, expectLine, `action="test"`) {
|
||||||
|
t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithLogger(t *testing.T) {
|
||||||
|
var (
|
||||||
|
expectedLevel = ErrorLevel.string()
|
||||||
|
expectLine = "context_test.go:74"
|
||||||
|
expectMsg = "message"
|
||||||
|
)
|
||||||
|
|
||||||
|
buf := enter()
|
||||||
|
defer exit()
|
||||||
|
|
||||||
|
ctx := WithLogger(context.TODO(), L.WithFields(Fields{"action": "test"}))
|
||||||
|
|
||||||
|
G(ctx).WithFields(Fields{"action": "test"}).Errorf("%s", message)
|
||||||
|
|
||||||
|
str := buf.String()
|
||||||
|
if !contains(t, str, expectedLevel, expectLine, expectMsg) {
|
||||||
|
t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !contains(t, str, expectedLevel, expectLine, `action="test"`) {
|
||||||
|
t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
|
||||||
|
}
|
||||||
|
}
|
@ -25,7 +25,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger = New(os.Stdout, NewTextFormatter(), WarningLevel, 4)
|
// NOTE: the default depth for the logger is 3 so that we can get the correct file and line when use the logger to log message
|
||||||
|
var logger = New(os.Stdout, NewTextFormatter(), WarningLevel, 3)
|
||||||
|
|
||||||
const srcSeparator = "harbor" + string(os.PathSeparator) + "src"
|
const srcSeparator = "harbor" + string(os.PathSeparator) + "src"
|
||||||
|
|
||||||
@ -277,52 +278,52 @@ func (l *Logger) getLine() string {
|
|||||||
|
|
||||||
// Debug ...
|
// Debug ...
|
||||||
func Debug(v ...interface{}) {
|
func Debug(v ...interface{}) {
|
||||||
logger.Debug(v...)
|
logger.WithDepth(4).Debug(v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugf ...
|
// Debugf ...
|
||||||
func Debugf(format string, v ...interface{}) {
|
func Debugf(format string, v ...interface{}) {
|
||||||
logger.Debugf(format, v...)
|
logger.WithDepth(4).Debugf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info ...
|
// Info ...
|
||||||
func Info(v ...interface{}) {
|
func Info(v ...interface{}) {
|
||||||
logger.Info(v...)
|
logger.WithDepth(4).Info(v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infof ...
|
// Infof ...
|
||||||
func Infof(format string, v ...interface{}) {
|
func Infof(format string, v ...interface{}) {
|
||||||
logger.Infof(format, v...)
|
logger.WithDepth(4).Infof(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warning ...
|
// Warning ...
|
||||||
func Warning(v ...interface{}) {
|
func Warning(v ...interface{}) {
|
||||||
logger.Warning(v...)
|
logger.WithDepth(4).Warning(v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warningf ...
|
// Warningf ...
|
||||||
func Warningf(format string, v ...interface{}) {
|
func Warningf(format string, v ...interface{}) {
|
||||||
logger.Warningf(format, v...)
|
logger.WithDepth(4).Warningf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error ...
|
// Error ...
|
||||||
func Error(v ...interface{}) {
|
func Error(v ...interface{}) {
|
||||||
logger.Error(v...)
|
logger.WithDepth(4).Error(v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf ...
|
// Errorf ...
|
||||||
func Errorf(format string, v ...interface{}) {
|
func Errorf(format string, v ...interface{}) {
|
||||||
logger.Errorf(format, v...)
|
logger.WithDepth(4).Errorf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal ...
|
// Fatal ...
|
||||||
func Fatal(v ...interface{}) {
|
func Fatal(v ...interface{}) {
|
||||||
logger.Fatal(v...)
|
logger.WithDepth(4).Fatal(v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalf ...
|
// Fatalf ...
|
||||||
func Fatalf(format string, v ...interface{}) {
|
func Fatalf(format string, v ...interface{}) {
|
||||||
logger.Fatalf(format, v...)
|
logger.WithDepth(4).Fatalf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func line(callDepth int) string {
|
func line(callDepth int) string {
|
||||||
|
@ -201,6 +201,24 @@ func TestErrorf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultLoggerErrorf(t *testing.T) {
|
||||||
|
var (
|
||||||
|
expectedLevel = ErrorLevel.string()
|
||||||
|
expectLine = "logger_test.go:214"
|
||||||
|
expectMsg = "message"
|
||||||
|
)
|
||||||
|
|
||||||
|
buf := enter()
|
||||||
|
defer exit()
|
||||||
|
|
||||||
|
DefaultLogger().Errorf("%s", message)
|
||||||
|
|
||||||
|
str := buf.String()
|
||||||
|
if !contains(t, str, expectedLevel, expectLine, expectMsg) {
|
||||||
|
t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func enter() *bytes.Buffer {
|
func enter() *bytes.Buffer {
|
||||||
b := make([]byte, 0, 32)
|
b := make([]byte, 0, 32)
|
||||||
buf := bytes.NewBuffer(b)
|
buf := bytes.NewBuffer(b)
|
||||||
|
Loading…
Reference in New Issue
Block a user