phuslu / log

Fastest structured logging
MIT License
672 stars 44 forks source link

[QUESTION] - How to use graylog with this library #43

Closed sujit-baniya closed 2 years ago

sujit-baniya commented 3 years ago

I'm trying to use Graylog as writer. Instead of logging as individual field log is printed as single string field.

Graylog library: https://github.com/Graylog2/go-gelf

How would I implement it for graylog

image

The code I'm using is this

gelfWriter, err := gelf.NewUDPWriter(grayLogAddr)
    if err != nil {
        panic(err)
    }
    log.DefaultLogger = log.Logger{
        Level:      log.InfoLevel,
        Caller:     0,
        TimeField:  "date",
        TimeFormat: "2006-01-02 15:04:05",
        Writer:     &log.IOWriter{gelfWriter},
    }
r := Request{
            requestId: rid,
            method:    c.Method(),
            host:      c.Hostname(),
            path:      c.Path(),
            request:   string(payload),
            response:  string(response),
            status:    c.Response().StatusCode(),
            latency:   fmt.Sprintf("%s", time.Since(start)),
        }
        log.Info().EmbedObject(&r).Msg("")
sujit-baniya commented 3 years ago

I see that it requires custom function to be implemented for log. Can you please suggest how I can define custom writer with custom function.

gelfWriter.WriteMessage(&gelf.Message{
            Short:    "Detect URL",
            Full:     "Detect URL",
            TimeUnix: float64(time.Now().Unix()),
            Level:    1,
            Extra:    ToMap(r),
        })

Works instead of

gelfWriter.Write(byteData) // it prints string
sujit-baniya commented 2 years ago

Any suggestions?

phuslu commented 2 years ago

I think https://github.com/phuslu/log#user-defined-data-structure could help