logrus icon indicating copy to clipboard operation
logrus copied to clipboard

fix #1272

Open lwydyby opened this issue 3 years ago • 4 comments

https://github.com/sirupsen/logrus/issues/1272

lwydyby avatar Aug 11 '21 02:08 lwydyby

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

dgsb avatar Sep 12 '21 05:09 dgsb

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 https://github.com/sirupsen/logrus/issues/1272#issuecomment-917586020

lwydyby avatar Sep 12 '21 07:09 lwydyby

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

lwydyby avatar Sep 12 '21 08:09 lwydyby

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
}
```

lwydyby avatar Sep 13 '21 05:09 lwydyby