-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
268 changed files
with
38,897 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
const ( | ||
RequestIdLogFieldKey string = "X-Request-Id" | ||
OperationLogFieldKey string = "Operation" | ||
) | ||
|
||
func GetLogger(context context.Context) *log.Entry { | ||
formatter := new(log.TextFormatter) | ||
|
||
l := &log.Logger{ | ||
Out: os.Stdout, | ||
Formatter: formatter, | ||
Hooks: make(log.LevelHooks), | ||
Level: log.DebugLevel, | ||
ExitFunc: os.Exit, | ||
// ReportCaller 会把file(调用的具体哪一行)和func(调用的函数)同时打开 | ||
ReportCaller: true, | ||
} | ||
entry := log.NewEntry(l) | ||
if v, ok := context.Value(RequestIdLogFieldKey).(string); ok { | ||
entry = entry.WithField(RequestIdLogFieldKey, v) | ||
} | ||
if v, ok := context.Value(OperationLogFieldKey).(string); ok { | ||
entry = entry.WithField(OperationLogFieldKey, v) | ||
} | ||
return entry | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func Test1(t *testing.T) { | ||
ctx := context.WithValue(context.Background(), "X-Request-Id", "xxxyyy111") | ||
log := GetLogger(ctx) | ||
log.Infof("hello, world") | ||
log.Debugf("hello, world") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.