Repository navigation
feat: pkg/util/log Enhancement (sub task of pkg/util Enhancement #833) #858
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,224 @@ | ||
| package log | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "reflect" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| "gopkg.in/gookit/color.v1" | ||
| ) | ||
|
|
||
| func TestCliLoggerFormatter_Format(t *testing.T) { | ||
| emptyEntry := &logrus.Entry{} | ||
| entry := &logrus.Entry{ | ||
| Level: logrus.ErrorLevel, | ||
| Message: "hi log", | ||
| } | ||
| emptyFormatter := &CliLoggerFormatter{} | ||
| formatter := &CliLoggerFormatter{ | ||
| prefix: "pp", | ||
| showType: "error", | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| formatter *CliLoggerFormatter | ||
| entry *logrus.Entry | ||
| want []byte | ||
| wantErr bool | ||
| }{ | ||
| {"base", emptyFormatter, emptyEntry, createBufferForCliLoggerFormatter(t, emptyFormatter, emptyEntry).Bytes(), false}, | ||
| {"base debug", formatter, entry, createBufferForCliLoggerFormatter2(t, formatter, entry).Bytes(), false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // logrus.SetLevel(logrus.DebugLevel) | ||
| got, err := tt.formatter.Format(tt.entry) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("CliLoggerFormatter.Format() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("CliLoggerFormatter.Format() = \n%v, want \n%v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCliLoggerFormatter_levelPrintRender(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| formatter *CliLoggerFormatter | ||
| want *CliLoggerFormatter | ||
| }{ | ||
| // TODO: Add test cases. | ||
| {"base", &CliLoggerFormatter{}, &CliLoggerFormatter{}}, | ||
| {"base debug", | ||
| &CliLoggerFormatter{showType: "debug"}, | ||
| &CliLoggerFormatter{ | ||
| level: logrus.DebugLevel, | ||
| showType: "debug", | ||
| formatLevelName: color.Blue.Render(DEBUG), | ||
| prefix: color.Blue.Render(normal.Debug)}, | ||
| }, | ||
| {"base info", | ||
| &CliLoggerFormatter{showType: "info"}, | ||
| &CliLoggerFormatter{ | ||
| level: logrus.InfoLevel, | ||
| showType: "info", | ||
| formatLevelName: color.FgLightBlue.Render(INFO), | ||
| prefix: color.FgLightBlue.Render(normal.Info)}, | ||
| }, | ||
| {"base warn", | ||
| &CliLoggerFormatter{showType: "warn"}, | ||
| &CliLoggerFormatter{ | ||
| level: logrus.WarnLevel, | ||
| showType: "warn", | ||
| formatLevelName: color.Yellow.Render(WARN), | ||
| prefix: color.Yellow.Render(normal.Warn)}, | ||
| }, | ||
| {"base error", | ||
| &CliLoggerFormatter{showType: "error"}, | ||
| &CliLoggerFormatter{ | ||
| level: logrus.ErrorLevel, | ||
| showType: "error", | ||
| formatLevelName: color.BgRed.Render(ERROR), | ||
| prefix: color.Red.Render(normal.Error)}, | ||
| }, | ||
| {"base fatal", | ||
| &CliLoggerFormatter{showType: "fatal"}, | ||
| &CliLoggerFormatter{ | ||
| level: logrus.FatalLevel, | ||
| showType: "fatal", | ||
| formatLevelName: color.BgRed.Render(FATAL), | ||
| prefix: color.Red.Render(normal.Fatal)}, | ||
| }, | ||
| {"base success", | ||
| &CliLoggerFormatter{showType: "success"}, | ||
| &CliLoggerFormatter{ | ||
| level: logrus.InfoLevel, | ||
| showType: "success", | ||
| formatLevelName: color.Green.Render(SUCCESS), | ||
| prefix: color.Green.Render(normal.Success)}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tt.formatter.levelPrintRender() | ||
| if !reflect.DeepEqual(tt.formatter, tt.want) { | ||
| t.Errorf("levelPrintRender = \n%v", tt.formatter) | ||
| t.Errorf("want = \n%v", tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSeparatorFormatter_Format(t *testing.T) { | ||
| b := createBufferForSeparatorFormatter(t) | ||
| tests := []struct { | ||
| name string | ||
| s *SeparatorFormatter | ||
| entry *logrus.Entry | ||
| want []byte | ||
| wantErr bool | ||
| }{ | ||
| // TODO: Add test cases. | ||
| {"base", &SeparatorFormatter{}, &logrus.Entry{}, b.Bytes(), false}, | ||
| {"base Entry with buffer", &SeparatorFormatter{}, &logrus.Entry{Buffer: &bytes.Buffer{}}, b.Bytes(), false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| s := &SeparatorFormatter{} | ||
| got, err := s.Format(tt.entry) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("SeparatorFormatter.Format() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("SeparatorFormatter.Format() = \n%v", string(got)) | ||
| t.Errorf("want = \n%v", tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func createBufferForSeparatorFormatter(t *testing.T) *bytes.Buffer { | ||
| var b *bytes.Buffer = &bytes.Buffer{} | ||
| entry := &logrus.Entry{} | ||
| timestamp := entry.Time.Format("2006-01-02 15:04:05") | ||
| newLog := fmt.Sprintf("%s %s %s %s\n", | ||
| timestamp, | ||
| color.Blue.Render(normal.Info), | ||
| color.Blue.Render(INFO), | ||
| color.Blue.Render(fmt.Sprintf("%s %s %s", "-------------------- [ ", entry.Message, " ] --------------------"))) | ||
|
|
||
| _, err := b.WriteString(newLog) | ||
| if err != nil { | ||
| t.Error(err) | ||
| return nil | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func createBufferForCliLoggerFormatter(t *testing.T, m *CliLoggerFormatter, entry *logrus.Entry) *bytes.Buffer { | ||
| var b *bytes.Buffer = &bytes.Buffer{} | ||
| m.levelPrintRender() | ||
|
|
||
| timestamp := entry.Time.Format("2006-01-02 15:04:05") | ||
|
|
||
| newLog := fmt.Sprintf("%s %s %s %s\n", timestamp, m.prefix, m.formatLevelName, entry.Message) | ||
|
|
||
| _, err := b.WriteString(newLog) | ||
| if err != nil { | ||
| t.Error(err) | ||
| return nil | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func createBufferForCliLoggerFormatter2(t *testing.T, m *CliLoggerFormatter, entry *logrus.Entry) *bytes.Buffer { | ||
| var b *bytes.Buffer = &bytes.Buffer{} | ||
| m.levelPrintRender() | ||
|
|
||
| timestamp := entry.Time.Format("2006-01-02 15:04:05") | ||
|
|
||
| entry.Message = addCallStackIgnoreLogrus(entry.Message) | ||
|
|
||
| newLog := fmt.Sprintf("%s %s %s %s\n", timestamp, m.prefix, m.formatLevelName, entry.Message) | ||
|
|
||
| _, err := b.WriteString(newLog) | ||
| if err != nil { | ||
| t.Error(err) | ||
| return nil | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func Test_addCallStackIgnoreLogrus(t *testing.T) { | ||
| rawMsg := "hi log" | ||
| tests := []struct { | ||
| name string | ||
| rawMessage string | ||
| want string | ||
| }{ | ||
| // TODO: Add test cases. | ||
| {"base", rawMsg, wantMsgOfAddCallStackIgnoreLogrus(rawMsg)}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := addCallStackIgnoreLogrus(tt.rawMessage); got != tt.want { | ||
| t.Errorf("addCallStackIgnoreLogrus() = \n%v, \nwant = %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func wantMsgOfAddCallStackIgnoreLogrus(stackMessage string) string { | ||
| retMsg := stackMessage | ||
| i := 10 | ||
| _, file, line, _ := runtime.Caller(i) | ||
| retMsg = retMsg + "\n -- " + file + fmt.Sprintf(" %d", line) | ||
| return retMsg | ||
| } | ||
This file contains hidden or 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,40 @@ | ||
| package log | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestSymbols_String(t *testing.T) { | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| symbols Symbols | ||
| want string | ||
| }{ | ||
| // TODO: Add test cases. | ||
| {"base", Symbols{}, "Debug: Info: Success: Warning: Error: Fatal: "}, | ||
| {"base", Symbols{Debug: "Debug"}, "Debug: Debug Info: Success: Warning: Error: Fatal: "}, | ||
| {"base", Symbols{ | ||
| Debug: Symbol("λ"), | ||
| Info: Symbol("ℹ"), | ||
| Success: Symbol("✔"), | ||
| Warning: Symbol("⚠"), | ||
| Error: Symbol("!!"), | ||
| Fatal: Symbol("✖"), | ||
| }, "Debug: λ Info: ℹ Success: ✔ Warning: ⚠ Error: !! Fatal: ✖"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| s := Symbols{ | ||
| Debug: tt.symbols.Debug, | ||
| Info: tt.symbols.Info, | ||
| Warning: tt.symbols.Warning, | ||
| Warn: tt.symbols.Warn, | ||
| Error: tt.symbols.Error, | ||
| Fatal: tt.symbols.Fatal, | ||
| Success: tt.symbols.Success, | ||
| } | ||
| if got := s.String(); got != tt.want { | ||
| t.Errorf("\nSymbols.String() = %v, \nwant = %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.