e3.2.1 Format type text changed to txt for consistency and clarity.
This commit is contained in:
@ -126,7 +126,7 @@ func logWithContext(ctx context.Context, logger *log.Logger, level string, msg s
|
|||||||
|
|
||||||
## Output Formats
|
## Output Formats
|
||||||
|
|
||||||
### Text Format (Human-Readable)
|
### Txt Format (Human-Readable)
|
||||||
|
|
||||||
Default format for development and debugging:
|
Default format for development and debugging:
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ Default format for development and debugging:
|
|||||||
2024-01-15T10:30:45.234567890Z WARN Rate limit approaching user_id=42 requests=95 limit=100
|
2024-01-15T10:30:45.234567890Z WARN Rate limit approaching user_id=42 requests=95 limit=100
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: The text format does not add quotes around string values containing spaces. This ensures predictability for simple, space-delimited parsing tools. For logs where maintaining the integrity of such values is critical, `json` format is recommended.
|
Note: The txt format does not add quotes around string values containing spaces. This ensures predictability for simple, space-delimited parsing tools. For logs where maintaining the integrity of such values is critical, `json` format is recommended.
|
||||||
|
|
||||||
Configuration:
|
Configuration:
|
||||||
```go
|
```go
|
||||||
|
|||||||
14
format.go
14
format.go
@ -32,7 +32,7 @@ func (s *serializer) reset() {
|
|||||||
s.buf = s.buf[:0]
|
s.buf = s.buf[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// serialize converts log entries to the configured format, JSON, raw, or (default) text.
|
// serialize converts log entries to the configured format, JSON, raw, or (default) txt.
|
||||||
func (s *serializer) serialize(format string, flags int64, timestamp time.Time, level int64, trace string, args []any) []byte {
|
func (s *serializer) serialize(format string, flags int64, timestamp time.Time, level int64, trace string, args []any) []byte {
|
||||||
s.reset()
|
s.reset()
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func (s *serializer) serialize(format string, flags int64, timestamp time.Time,
|
|||||||
if format == "json" {
|
if format == "json" {
|
||||||
return s.serializeJSON(flags, timestamp, level, trace, args)
|
return s.serializeJSON(flags, timestamp, level, trace, args)
|
||||||
}
|
}
|
||||||
return s.serializeText(flags, timestamp, level, trace, args)
|
return s.serializeTxt(flags, timestamp, level, trace, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// serializeRaw formats args as space-separated strings without metadata or newline.
|
// serializeRaw formats args as space-separated strings without metadata or newline.
|
||||||
@ -177,8 +177,8 @@ func (s *serializer) serializeJSON(flags int64, timestamp time.Time, level int64
|
|||||||
return s.buf
|
return s.buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// serializeText formats log entries as plain text (time, level, trace, fields).
|
// serializeTxt formats log entries as plain txt (time, level, trace, fields).
|
||||||
func (s *serializer) serializeText(flags int64, timestamp time.Time, level int64, trace string, args []any) []byte {
|
func (s *serializer) serializeTxt(flags int64, timestamp time.Time, level int64, trace string, args []any) []byte {
|
||||||
needsSpace := false
|
needsSpace := false
|
||||||
|
|
||||||
if flags&FlagShowTimestamp != 0 {
|
if flags&FlagShowTimestamp != 0 {
|
||||||
@ -206,7 +206,7 @@ func (s *serializer) serializeText(flags int64, timestamp time.Time, level int64
|
|||||||
if needsSpace {
|
if needsSpace {
|
||||||
s.buf = append(s.buf, ' ')
|
s.buf = append(s.buf, ' ')
|
||||||
}
|
}
|
||||||
s.writeTextValue(arg)
|
s.writeTxtValue(arg)
|
||||||
needsSpace = true
|
needsSpace = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,8 +214,8 @@ func (s *serializer) serializeText(flags int64, timestamp time.Time, level int64
|
|||||||
return s.buf
|
return s.buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeTextValue converts any value to its text representation.
|
// writeTxtValue converts any value to its txt representation.
|
||||||
func (s *serializer) writeTextValue(v any) {
|
func (s *serializer) writeTxtValue(v any) {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case string:
|
case string:
|
||||||
s.buf = append(s.buf, val...)
|
s.buf = append(s.buf, val...)
|
||||||
|
|||||||
@ -16,7 +16,7 @@ func TestSerializer(t *testing.T) {
|
|||||||
s := newSerializer()
|
s := newSerializer()
|
||||||
timestamp := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
|
timestamp := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
t.Run("text format", func(t *testing.T) {
|
t.Run("txt format", func(t *testing.T) {
|
||||||
data := s.serialize("txt", FlagDefault, timestamp, LevelInfo, "", []any{"test message", 123})
|
data := s.serialize("txt", FlagDefault, timestamp, LevelInfo, "", []any{"test message", 123})
|
||||||
str := string(data)
|
str := string(data)
|
||||||
|
|
||||||
|
|||||||
@ -179,7 +179,7 @@ func TestLoggerFormats(t *testing.T) {
|
|||||||
check func(t *testing.T, content string)
|
check func(t *testing.T, content string)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "text format",
|
name: "txt format",
|
||||||
format: "txt",
|
format: "txt",
|
||||||
check: func(t *testing.T, content string) {
|
check: func(t *testing.T, content string) {
|
||||||
assert.Contains(t, content, "INFO test message")
|
assert.Contains(t, content, "INFO test message")
|
||||||
|
|||||||
Reference in New Issue
Block a user