komuw / ong

ong, is a Go http toolkit.
MIT License
16 stars 4 forks source link

ong/log: integrate with slog tests #354

Closed komuw closed 11 months ago

komuw commented 11 months ago
komuw commented 11 months ago

I think that in order to conform to testing/slogtest, we need to change tact.


func New(w io.Writer, maxMsgs int)  slog.Logger {
    buf := &bytes.Buffer{}
    jh := slog.NewJSONHandler(
        // Do not pass in `w` here, pass in a buf like bytes.Buffer
        buf,
        opts,
    )
   hdlr := handler{Handler: jh, cBuf: cbuf, logID: id.New()}
   l := slog.New(hdlr)

  return l.With(logIDFieldName, id)

func (h handler) Handle(ctx context.Context, r slog.Record) error {
    err := h.Handler.Handle(ctx, r) // this will use json-logger to log into an in memory buff
    h.cBuf.store(json.bytes.Buffer) // whatever string/byte was logged by json.logger, store it in memory
    if r.Level >= slog.LevelError {
       for item in h.cbuf{
          _, _ = h.w.Write([]byte(item))
       }
    }
}