-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #1272 #1273
Open
lwydyby
wants to merge
7
commits into
sirupsen:master
Choose a base branch
from
lwydyby:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix #1272 #1273
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ca79890
add lock function
b958901
add lock function
659b3d3
Update logger_test.go
lwydyby 7207a92
Update logger_test.go
lwydyby 08bbc96
add replace hook method and remove lock method
544f676
Replication can indeed improve performance, but it will lead to this …
64181dc
fixed test
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,9 +2,15 @@ package logrus | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -70,7 +76,7 @@ func TestWarninglnNotEqualToWarning(t *testing.T) { | |
|
||
type testBufferPool struct { | ||
buffers []*bytes.Buffer | ||
get int | ||
get int | ||
} | ||
|
||
func (p *testBufferPool) Get() *bytes.Buffer { | ||
|
@@ -95,3 +101,123 @@ func TestLogger_SetBufferPool(t *testing.T) { | |
assert.Equal(t, pool.get, 1, "Logger.SetBufferPool(): The BufferPool.Get() must be called") | ||
assert.Len(t, pool.buffers, 1, "Logger.SetBufferPool(): The BufferPool.Put() must be called") | ||
} | ||
|
||
func TestLogger_concurrentLock(t *testing.T) { | ||
SetFormatter(&LogFormatter{}) | ||
go func() { | ||
for { | ||
func() { | ||
defer func() { | ||
// 处理所有异常,防止panic导致程序关闭 | ||
if p := recover(); p != nil { | ||
} | ||
}() | ||
hook := AddTraceIdHook("123") | ||
defer RemoveTraceHook(hook) | ||
Infof("test why ") | ||
}() | ||
} | ||
}() | ||
go func() { | ||
for { | ||
func() { | ||
defer func() { | ||
// 处理所有异常,防止panic导致程序关闭 | ||
if p := recover(); p != nil { | ||
} | ||
}() | ||
hook := AddTraceIdHook("1233") | ||
defer RemoveTraceHook(hook) | ||
Infof("test why 2") | ||
}() | ||
} | ||
}() | ||
time.Sleep(1 * time.Minute) | ||
} | ||
|
||
var traceLock = &sync.Mutex{} | ||
|
||
func AddTraceIdHook(traceId string) Hook { | ||
defer traceLock.Unlock() | ||
traceLock.Lock() | ||
traceHook := newTraceIdHook(traceId) | ||
if StandardLogger().Hooks == nil { | ||
hooks := new(LevelHooks) | ||
StandardLogger().ReplaceHooks(*hooks) | ||
} | ||
AddHook(traceHook) | ||
return traceHook | ||
} | ||
|
||
func RemoveTraceHook(hook Hook) { | ||
allHooks := StandardLogger().Hooks | ||
func() { | ||
defer Unlock() | ||
Lock() | ||
for key, hooks := range allHooks { | ||
replaceHooks := hooks | ||
for index, h := range hooks { | ||
if h == hook { | ||
replaceHooks = append(hooks[:index], hooks[index:]...) | ||
break | ||
} | ||
} | ||
allHooks[key] = replaceHooks | ||
} | ||
}() | ||
|
||
StandardLogger().ReplaceHooks(allHooks) | ||
} | ||
|
||
type TraceIdHook struct { | ||
TraceId string | ||
GID uint64 | ||
} | ||
|
||
func newTraceIdHook(traceId string) Hook { | ||
return &TraceIdHook{ | ||
TraceId: traceId, | ||
GID: getGID(), | ||
} | ||
} | ||
|
||
func (t TraceIdHook) Levels() []Level { | ||
return AllLevels | ||
} | ||
|
||
func (t TraceIdHook) Fire(entry *Entry) error { | ||
if getGID() == t.GID { | ||
entry.Context = context.WithValue(context.Background(), "trace_id", t.TraceId) | ||
} | ||
return nil | ||
} | ||
|
||
type LogFormatter struct{} | ||
|
||
//格式详情 | ||
func (s *LogFormatter) Format(entry *Entry) ([]byte, error) { | ||
timestamp := time.Now().Format("2006-01-02 15:04:05") | ||
var file string | ||
var line int | ||
if entry.Caller != nil { | ||
file = filepath.Base(entry.Caller.File) | ||
line = entry.Caller.Line | ||
} | ||
level := entry.Level.String() | ||
if entry.Context == nil || entry.Context.Value("trace_id") == "" { | ||
uuid := "NO UUID" | ||
entry.Context = context.WithValue(context.Background(), "trace_id", uuid) | ||
} | ||
msg := fmt.Sprintf("%-15s [%-3d] [%-5s] [%s] %s:%d %s\n", timestamp, getGID(), level, entry.Context.Value("trace_id"), file, line, entry.Message) | ||
return []byte(msg), nil | ||
} | ||
|
||
// 获取当前协程id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have deleted unnecessary Chinese comments |
||
func getGID() uint64 { | ||
b := make([]byte, 64) | ||
b = b[:runtime.Stack(b, false)] | ||
b = bytes.TrimPrefix(b, []byte("goroutine ")) | ||
b = b[:bytes.IndexByte(b, ' ')] | ||
n, _ := strconv.ParseUint(string(b), 10, 64) | ||
return n | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this comment says?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sry I have deleted unnecessary Chinese comments