Skip to content

Commit c5265fc

Browse files
Merge pull request #103 from kuudori/HYPERFLEET-908
HYPERFLEET-908 - feat: change default log format from text to JSON
2 parents 9517112 + 4b4825c commit c5265fc

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

pkg/logger/logger.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const (
3737
type LogFormat int
3838

3939
const (
40-
// FormatText is human-readable text format (default for development)
40+
// FormatText outputs logs in human-readable text format
4141
FormatText LogFormat = iota
42-
// FormatJSON is structured JSON format (recommended for production)
42+
// FormatJSON outputs logs in JSON format for structured logging
4343
FormatJSON
4444
)
4545

@@ -102,7 +102,7 @@ func DefaultConfig() *LogConfig {
102102
}
103103
return &LogConfig{
104104
Level: LevelInfo,
105-
Format: FormatText,
105+
Format: FormatJSON,
106106
Output: os.Stdout,
107107
Component: "sentinel",
108108
Version: "dev",
@@ -154,7 +154,7 @@ func ParseLogFormat(format string) (LogFormat, error) {
154154
case "json":
155155
return FormatJSON, nil
156156
default:
157-
return FormatText, fmt.Errorf("unknown log format: %s (valid: text, json)", format)
157+
return FormatJSON, fmt.Errorf("unknown log format: %s (valid: text, json)", format)
158158
}
159159
}
160160

@@ -555,6 +555,7 @@ func (n *noopLogger) Fatal(ctx context.Context, message string) {
555555
fmt.Fprintf(os.Stderr, "FATAL: %s\n", message)
556556
os.Exit(1)
557557
}
558+
558559
func (n *noopLogger) Fatalf(ctx context.Context, format string, args ...interface{}) {
559560
fmt.Fprintf(os.Stderr, "FATAL: %s\n", fmt.Sprintf(format, args...))
560561
os.Exit(1)

pkg/logger/logger_test.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func TestParseLogFormat(t *testing.T) {
7474
{"json uppercase", "JSON", FormatJSON, false},
7575
{"json mixed case", "Json", FormatJSON, false},
7676
{"with whitespace", " json ", FormatJSON, false},
77-
{"invalid format", "xml", FormatText, true},
78-
{"empty string", "", FormatText, true},
77+
{"invalid format", "xml", FormatJSON, true},
78+
{"empty string", "", FormatJSON, true},
7979
}
8080

8181
for _, tt := range tests {
@@ -89,9 +89,9 @@ func TestParseLogFormat(t *testing.T) {
8989
if err != nil {
9090
t.Errorf("unexpected error for input %q: %v", tt.input, err)
9191
}
92-
if format != tt.expected {
93-
t.Errorf("expected %v, got %v", tt.expected, format)
94-
}
92+
}
93+
if format != tt.expected {
94+
t.Errorf("expected %v, got %v", tt.expected, format)
9595
}
9696
})
9797
}
@@ -595,8 +595,8 @@ func TestDefaultConfig(t *testing.T) {
595595
if cfg.Level != LevelInfo {
596596
t.Errorf("expected default level to be Info, got %v", cfg.Level)
597597
}
598-
if cfg.Format != FormatText {
599-
t.Errorf("expected default format to be Text, got %v", cfg.Format)
598+
if cfg.Format != FormatJSON {
599+
t.Errorf("expected default format to be JSON, got %v", cfg.Format)
600600
}
601601
if cfg.Output != os.Stdout {
602602
t.Error("expected default output to be stdout")
@@ -809,3 +809,16 @@ func TestLoggerErrorWithStackTrace(t *testing.T) {
809809
}
810810
})
811811
}
812+
813+
func TestDefaultFormatIsJSON(t *testing.T) {
814+
var buf bytes.Buffer
815+
cfg := DefaultConfig()
816+
cfg.Output = &buf
817+
log := NewHyperFleetLoggerWithConfig(cfg)
818+
819+
log.Info(context.Background(), "test")
820+
821+
if !json.Valid(buf.Bytes()) {
822+
t.Errorf("expected default format output to be valid JSON, got: %s", buf.String())
823+
}
824+
}

0 commit comments

Comments
 (0)