phuslu / log

Fastest structured logging
MIT License
672 stars 44 forks source link

feat: add BoolRaw for tsv format #60

Closed rfyiamcool closed 1 year ago

rfyiamcool commented 1 year ago

why

Add BoolRaw() method in tsv.go. Use the BoolRaw method to output a true or false string. Or I directly changed the Bool() method, output bool's string, such as true, false ???

// Bool append the b as a bool to the entry.
func (e *TSVEntry) Bool(b bool) *TSVEntry {
    if b {
        e.buf = append(e.buf, "true"...)
        e.buf = append(e.buf, e.sep)
        // e.buf = append(e.buf, '1', e.sep) 
    } else {
        // e.buf = append(e.buf, '0', e.sep)
        e.buf = append(e.buf, "false")
        e.buf = append(e.buf, e.sep)
    }
    return e
}

test result

test code

func TestTSVLogger(t *testing.T) {
    logger := TSVLogger{}

    logger.New().
        Timestamp().
        TimestampMS().
        Caller(1).
        Bool(true).
        Bool(false).
        BoolRaw(true).
        BoolRaw(false).Msg()
}

output:

1683102054  1683102054878   tsv_test.go:15  1   0   true    false
phuslu commented 1 year ago

Thanks, almost LGTM but I think BoolRaw is a bit confusing, Could we name it with a more appropriate one? E.g. RawBool, Boolean, BoolString, which one is your prefer or please give your advice.

phuslu commented 1 year ago

Moreover, I suggest use b = append(b, 't', 'r', 'u', 'e', e.sep) to reduce cycles. refer to https://godbolt.org/z/W5a5ovK5E

rfyiamcool commented 1 year ago

@phuslu done. 😁


func TestTSVLogger(t *testing.T) {
    logger := TSVLogger{}

    logger.New().
        Timestamp().
        TimestampMS().
        Caller(1).
        Bool(true).
        Bool(false).
        BoolString(true).
        BoolString(false).
        Msg()
}

Running tool: /usr/local/go/bin/go test -timeout 120s -run ^TestTSVLogger$ github.com/phuslu/log -v -count=1

=== RUN   TestTSVLogger
1683118198  1683118198173   tsv_test.go:15  1   0   true    false
rfyiamcool commented 1 year ago

Moreover, I suggest use b = append(b, 't', 'r', 'u', 'e', e.sep) to reduce cycles. refer to https://godbolt.org/z/W5a5ovK5E

👍

phuslu commented 1 year ago

thanks, merged and tagged v1.0.86