|
|
@ -15,9 +15,6 @@ |
|
|
|
package logging |
|
|
|
|
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2" |
|
|
@ -30,7 +27,6 @@ import ( |
|
|
|
func init() { |
|
|
|
caddy.RegisterModule(ConsoleEncoder{}) |
|
|
|
caddy.RegisterModule(JSONEncoder{}) |
|
|
|
caddy.RegisterModule(SingleFieldEncoder{}) |
|
|
|
} |
|
|
|
|
|
|
|
// ConsoleEncoder encodes log entries that are mostly human-readable.
|
|
|
@ -115,85 +111,6 @@ func (je *JSONEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// SingleFieldEncoder writes a log entry that consists entirely
|
|
|
|
// of a single string field in the log entry. This is useful
|
|
|
|
// for custom, self-encoded log entries that consist of a
|
|
|
|
// single field in the structured log entry.
|
|
|
|
type SingleFieldEncoder struct { |
|
|
|
zapcore.Encoder `json:"-"` |
|
|
|
FieldName string `json:"field,omitempty"` |
|
|
|
FallbackRaw json.RawMessage `json:"fallback,omitempty" caddy:"namespace=caddy.logging.encoders inline_key=format"` |
|
|
|
} |
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (SingleFieldEncoder) CaddyModule() caddy.ModuleInfo { |
|
|
|
return caddy.ModuleInfo{ |
|
|
|
ID: "caddy.logging.encoders.single_field", |
|
|
|
New: func() caddy.Module { return new(SingleFieldEncoder) }, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Provision sets up the encoder.
|
|
|
|
func (se *SingleFieldEncoder) Provision(ctx caddy.Context) error { |
|
|
|
caddy.Log().Named("caddy.logging.encoders.single_field").Warn("the 'single_field' encoder is deprecated and will be removed soon!") |
|
|
|
if se.FallbackRaw != nil { |
|
|
|
val, err := ctx.LoadModule(se, "FallbackRaw") |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("loading fallback encoder module: %v", err) |
|
|
|
} |
|
|
|
se.Encoder = val.(zapcore.Encoder) |
|
|
|
} |
|
|
|
if se.Encoder == nil { |
|
|
|
se.Encoder = nopEncoder{} |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// Clone wraps the underlying encoder's Clone. This is
|
|
|
|
// necessary because we implement our own EncodeEntry,
|
|
|
|
// and if we simply let the embedded encoder's Clone
|
|
|
|
// be promoted, it would return a clone of that, and
|
|
|
|
// we'd lose our SingleFieldEncoder's EncodeEntry.
|
|
|
|
func (se SingleFieldEncoder) Clone() zapcore.Encoder { |
|
|
|
return SingleFieldEncoder{ |
|
|
|
Encoder: se.Encoder.Clone(), |
|
|
|
FieldName: se.FieldName, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// EncodeEntry partially implements the zapcore.Encoder interface.
|
|
|
|
func (se SingleFieldEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { |
|
|
|
for _, f := range fields { |
|
|
|
if f.Key == se.FieldName { |
|
|
|
buf := bufferpool.Get() |
|
|
|
buf.AppendString(f.String) |
|
|
|
if !strings.HasSuffix(f.String, "\n") { |
|
|
|
buf.AppendByte('\n') |
|
|
|
} |
|
|
|
return buf, nil |
|
|
|
} |
|
|
|
} |
|
|
|
if se.Encoder == nil { |
|
|
|
return nil, fmt.Errorf("no fallback encoder defined") |
|
|
|
} |
|
|
|
return se.Encoder.EncodeEntry(ent, fields) |
|
|
|
} |
|
|
|
|
|
|
|
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
|
|
|
|
//
|
|
|
|
// single_field <field_name>
|
|
|
|
//
|
|
|
|
func (se *SingleFieldEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
|
|
|
for d.Next() { |
|
|
|
var fieldName string |
|
|
|
if !d.AllArgs(&fieldName) { |
|
|
|
return d.ArgErr() |
|
|
|
} |
|
|
|
se.FieldName = d.Val() |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// LogEncoderConfig holds configuration common to most encoders.
|
|
|
|
type LogEncoderConfig struct { |
|
|
|
MessageKey *string `json:"message_key,omitempty"` |
|
|
@ -350,9 +267,7 @@ var bufferpool = buffer.NewPool() |
|
|
|
var ( |
|
|
|
_ zapcore.Encoder = (*ConsoleEncoder)(nil) |
|
|
|
_ zapcore.Encoder = (*JSONEncoder)(nil) |
|
|
|
_ zapcore.Encoder = (*SingleFieldEncoder)(nil) |
|
|
|
|
|
|
|
_ caddyfile.Unmarshaler = (*ConsoleEncoder)(nil) |
|
|
|
_ caddyfile.Unmarshaler = (*JSONEncoder)(nil) |
|
|
|
_ caddyfile.Unmarshaler = (*SingleFieldEncoder)(nil) |
|
|
|
) |
|
|
|