Browse Source

logging: Remove common_log field and single_field encoder (#4149) (#4282)

master
Matt Holt 3 years ago
committed by GitHub
parent
commit
0eb0b60f47
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      caddyconfig/httpcaddyfile/builtins_test.go
  2. 4
      caddytest/integration/caddyfile_adapt/global_options_log_custom.txt
  3. 9
      modules/caddyhttp/server.go
  4. 85
      modules/logging/encoders.go

3
caddyconfig/httpcaddyfile/builtins_test.go

@ -37,7 +37,6 @@ func TestLogDirectiveSyntax(t *testing.T) {
format filter { format filter {
wrap console wrap console
fields { fields {
common_log delete
request>remote_addr ip_mask { request>remote_addr ip_mask {
ipv4 24 ipv4 24
ipv6 32 ipv6 32
@ -47,7 +46,7 @@ func TestLogDirectiveSyntax(t *testing.T) {
} }
} }
`, `,
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"encoder":{"fields":{"common_log":{"filter":"delete"},"request\u003eremote_addr":{"filter":"ip_mask","ipv4_cidr":24,"ipv6_cidr":32}},"format":"filter","wrap":{"format":"console"}},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`, output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"encoder":{"fields":{"request\u003eremote_addr":{"filter":"ip_mask","ipv4_cidr":24,"ipv6_cidr":32}},"format":"filter","wrap":{"format":"console"}},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`,
expectError: false, expectError: false,
}, },
{ {

4
caddytest/integration/caddyfile_adapt/global_options_log_custom.txt

@ -3,7 +3,6 @@
format filter { format filter {
wrap console wrap console
fields { fields {
common_log delete
request>remote_addr ip_mask { request>remote_addr ip_mask {
ipv4 24 ipv4 24
ipv6 32 ipv6 32
@ -19,9 +18,6 @@
"custom-logger": { "custom-logger": {
"encoder": { "encoder": {
"fields": { "fields": {
"common_log": {
"filter": "delete"
},
"request\u003eremote_addr": { "request\u003eremote_addr": {
"filter": "ip_mask", "filter": "ip_mask",
"ipv4_cidr": 24, "ipv4_cidr": 24,

9
modules/caddyhttp/server.go

@ -187,7 +187,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
userID, _ := repl.GetString("http.auth.user.id") userID, _ := repl.GetString("http.auth.user.id")
log("handled request", log("handled request",
zap.String("common_log", repl.ReplaceAll(commonLogFormat, commonLogEmptyValue)),
zap.String("user_id", userID), zap.String("user_id", userID),
zap.Duration("duration", duration), zap.Duration("duration", duration),
zap.Int("size", wrec.Size()), zap.Int("size", wrec.Size()),
@ -627,14 +626,6 @@ func cloneURL(from, to *url.URL) {
} }
} }
const (
// commonLogFormat is the common log format. https://en.wikipedia.org/wiki/Common_Log_Format
commonLogFormat = `{http.request.remote.host} ` + commonLogEmptyValue + ` {http.auth.user.id} [{time.now.common_log}] "{http.request.orig_method} {http.request.orig_uri} {http.request.proto}" {http.response.status} {http.response.size}`
// commonLogEmptyValue is the common empty log value.
commonLogEmptyValue = "-"
)
// Context keys for HTTP request context values. // Context keys for HTTP request context values.
const ( const (
// For referencing the server instance // For referencing the server instance

85
modules/logging/encoders.go

@ -15,9 +15,6 @@
package logging package logging
import ( import (
"encoding/json"
"fmt"
"strings"
"time" "time"
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
@ -30,7 +27,6 @@ import (
func init() { func init() {
caddy.RegisterModule(ConsoleEncoder{}) caddy.RegisterModule(ConsoleEncoder{})
caddy.RegisterModule(JSONEncoder{}) caddy.RegisterModule(JSONEncoder{})
caddy.RegisterModule(SingleFieldEncoder{})
} }
// ConsoleEncoder encodes log entries that are mostly human-readable. // ConsoleEncoder encodes log entries that are mostly human-readable.
@ -115,85 +111,6 @@ func (je *JSONEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil 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. // LogEncoderConfig holds configuration common to most encoders.
type LogEncoderConfig struct { type LogEncoderConfig struct {
MessageKey *string `json:"message_key,omitempty"` MessageKey *string `json:"message_key,omitempty"`
@ -350,9 +267,7 @@ var bufferpool = buffer.NewPool()
var ( var (
_ zapcore.Encoder = (*ConsoleEncoder)(nil) _ zapcore.Encoder = (*ConsoleEncoder)(nil)
_ zapcore.Encoder = (*JSONEncoder)(nil) _ zapcore.Encoder = (*JSONEncoder)(nil)
_ zapcore.Encoder = (*SingleFieldEncoder)(nil)
_ caddyfile.Unmarshaler = (*ConsoleEncoder)(nil) _ caddyfile.Unmarshaler = (*ConsoleEncoder)(nil)
_ caddyfile.Unmarshaler = (*JSONEncoder)(nil) _ caddyfile.Unmarshaler = (*JSONEncoder)(nil)
_ caddyfile.Unmarshaler = (*SingleFieldEncoder)(nil)
) )

Loading…
Cancel
Save