-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve the format of --debug logs (#1559)
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
- Loading branch information
Showing
5 changed files
with
623 additions
and
6 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
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,50 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// TextFormatter formats logs into text. | ||
type TextFormatter struct{} | ||
|
||
// logEntrySeperator is the separator between log entries. | ||
const logEntrySeperator = "\n\n" // two empty lines | ||
|
||
// Format renders a single log entry. | ||
func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
var buf bytes.Buffer | ||
|
||
timestamp := entry.Time.Format(time.RFC3339Nano) | ||
levelText := strings.ToUpper(entry.Level.String()) | ||
fmt.Fprintf(&buf, "[%s][%s]: %s\n", timestamp, levelText, entry.Message) | ||
// print data fields | ||
if len(entry.Data) > 0 { | ||
buf.WriteString("[Data]:\n") | ||
for k, v := range entry.Data { | ||
fmt.Fprintf(&buf, " %s=%v\n", k, v) | ||
} | ||
} | ||
|
||
buf.WriteString(logEntrySeperator) | ||
return buf.Bytes(), nil | ||
} |
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,99 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestTextFormatter_Format(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
f *TextFormatter | ||
entry *logrus.Entry | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "debug log entry", | ||
f: &TextFormatter{}, | ||
entry: &logrus.Entry{ | ||
Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), | ||
Level: logrus.DebugLevel, | ||
Message: "test debug", | ||
Data: logrus.Fields{}, | ||
}, | ||
want: []byte("[2024-12-01T23:30:01.000000055Z][DEBUG]: test debug\n\n\n"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "info log entry", | ||
f: &TextFormatter{}, | ||
entry: &logrus.Entry{ | ||
Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), | ||
Level: logrus.InfoLevel, | ||
Message: "test info", | ||
Data: logrus.Fields{}, | ||
}, | ||
want: []byte("[2024-12-01T23:30:01.000000055Z][INFO]: test info\n\n\n"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "warning log entry with data fields", | ||
f: &TextFormatter{}, | ||
entry: &logrus.Entry{ | ||
Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), | ||
Level: logrus.WarnLevel, | ||
Message: "test warning with fields", | ||
Data: logrus.Fields{ | ||
"testkey": "testval", | ||
}, | ||
}, | ||
want: []byte("[2024-12-01T23:30:01.000000055Z][WARNING]: test warning with fields\n[Data]:\n testkey=testval\n\n\n"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "error log entry with data fields", | ||
f: &TextFormatter{}, | ||
entry: &logrus.Entry{ | ||
Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), | ||
Level: logrus.ErrorLevel, | ||
Message: "test warning with fields", | ||
Data: logrus.Fields{ | ||
"testkey": 123, | ||
}, | ||
}, | ||
want: []byte("[2024-12-01T23:30:01.000000055Z][ERROR]: test warning with fields\n[Data]:\n testkey=123\n\n\n"), | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.f.Format(tt.entry) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("TextFormatter.Format() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("TextFormatter.Format() = %s, want %s", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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.