Emit writes the entry to the segment writer. We are using this particular syntax to avoid allocating an intermediate interface value. This allows exactly zero non-amortized allocations in all cases.
(ctx context.Context, l *Logger, entry T)
| 24 | // We are using this particular syntax to avoid allocating an intermediate interface value. |
| 25 | // This allows exactly zero non-amortized allocations in all cases. |
| 26 | func Emit[T WriterTo](ctx context.Context, l *Logger, entry T) { |
| 27 | if l == nil { |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | if l.output == nil { |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | jw := NewJSONWriter() |
| 36 | defer jw.Release() |
| 37 | |
| 38 | jw.BeginObject() |
| 39 | jw.TimeField("t", l.timeFunc()) |
| 40 | |
| 41 | for _, param := range l.params { |
| 42 | param.WriteValueTo(jw) |
| 43 | } |
| 44 | |
| 45 | params := ctx.Value(loggerParamsKey) |
| 46 | if params != nil { |
| 47 | if params, ok := params.([]ParamWriter); ok { |
| 48 | for _, p := range params { |
| 49 | p.WriteValueTo(jw) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | entry.WriteTo(jw) |
| 55 | jw.EndObject() |
| 56 | jw.buf = append(jw.buf, '\n') |
| 57 | |
| 58 | l.output(jw.buf) |
| 59 | } |
| 60 | |
| 61 | // Log logs a message with no parameters. |
| 62 | func Log(ctx context.Context, l *Logger, text string) { |