Skip to content
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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

fix #1272 #1273

wants to merge 7 commits into from

Conversation

lwydyby
Copy link

@lwydyby lwydyby commented Aug 11, 2021

logger_test.go Outdated
for {
func() {
defer func() {
// 处理所有异常,防止panic导致程序关闭
Copy link

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?

Copy link
Author

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

logger_test.go Outdated
return []byte(msg), nil
}

// 获取当前协程id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have deleted unnecessary Chinese comments

Remove unnecessary comments
Remove unnecessary comments
@dgsb
Copy link
Collaborator

dgsb commented Sep 12, 2021

Thanks for your contribution, but we already have the ReplaceHooks method which would perfectly solve your use case. Why don't just use it ?

@lwydyby
Copy link
Author

lwydyby commented Sep 12, 2021

Thanks for your contribution, but we already have the ReplaceHooks method which would perfectly solve your use case. Why don't just use it ?
But if this method is called in parallel, an exception will occur, as you can see for details #1272 (comment)

@lwydyby
Copy link
Author

lwydyby commented Sep 12, 2021

Thanks for your contribution, but we already have the ReplaceHooks method which would perfectly solve your use case. Why don't just use it ?

I replace the implementation ReplaceHook

…instant.If a new hook is added, the hook may not be triggered
@lwydyby
Copy link
Author

lwydyby commented Sep 13, 2021

I found another problem . Replication can indeed improve performance, but it will lead to this instant.
If a new hook is added, the hook may not be triggered
you can look this gist: https://github.com/lwydyby/logrus/blob/master/logger_test.go TestLogger_concurrentLock
and debug here

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") == "" {
               //debug in here
		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
}
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants