summaryrefslogtreecommitdiff
path: root/modules/logging
diff options
context:
space:
mode:
Diffstat (limited to 'modules/logging')
-rw-r--r--modules/logging/encoders.go5
-rw-r--r--modules/logging/filewriter.go7
-rw-r--r--modules/logging/filterencoder.go7
-rw-r--r--modules/logging/filters.go41
-rw-r--r--modules/logging/filters_test.go36
-rw-r--r--modules/logging/netwriter.go35
6 files changed, 105 insertions, 26 deletions
diff --git a/modules/logging/encoders.go b/modules/logging/encoders.go
index 1cfab8e..a4409e7 100644
--- a/modules/logging/encoders.go
+++ b/modules/logging/encoders.go
@@ -17,11 +17,12 @@ package logging
import (
"time"
- "github.com/caddyserver/caddy/v2"
- "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
+
+ "github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func init() {
diff --git a/modules/logging/filewriter.go b/modules/logging/filewriter.go
index f902a6d..11c051d 100644
--- a/modules/logging/filewriter.go
+++ b/modules/logging/filewriter.go
@@ -22,10 +22,11 @@ import (
"path/filepath"
"strconv"
- "github.com/caddyserver/caddy/v2"
- "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/dustin/go-humanize"
"gopkg.in/natefinch/lumberjack.v2"
+
+ "github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func init() {
@@ -126,7 +127,7 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
}
// otherwise just open a regular file
- return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
+ return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o666)
}
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
diff --git a/modules/logging/filterencoder.go b/modules/logging/filterencoder.go
index 1a6842e..4d51e64 100644
--- a/modules/logging/filterencoder.go
+++ b/modules/logging/filterencoder.go
@@ -19,12 +19,13 @@ import (
"fmt"
"time"
- "github.com/caddyserver/caddy/v2"
- "github.com/caddyserver/caddy/v2/caddyconfig"
- "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
+
+ "github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func init() {
diff --git a/modules/logging/filters.go b/modules/logging/filters.go
index 8cc84c7..233d5d7 100644
--- a/modules/logging/filters.go
+++ b/modules/logging/filters.go
@@ -25,10 +25,11 @@ import (
"strconv"
"strings"
+ "go.uber.org/zap/zapcore"
+
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
- "go.uber.org/zap/zapcore"
)
func init() {
@@ -81,8 +82,7 @@ func hash(s string) string {
// of the SHA-256 hash of the content. Operates
// on string fields, or on arrays of strings
// where each string is hashed.
-type HashFilter struct {
-}
+type HashFilter struct{}
// CaddyModule returns the Caddy module information.
func (HashFilter) CaddyModule() caddy.ModuleInfo {
@@ -100,12 +100,15 @@ func (f *HashFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Filter filters the input field with the replacement value.
func (f *HashFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
+ newArray := make(caddyhttp.LoggableStringArray, len(array))
for i, s := range array {
- array[i] = hash(s)
+ newArray[i] = hash(s)
}
+ in.Interface = newArray
} else {
in.String = hash(in.String)
}
+
return in
}
@@ -219,9 +222,11 @@ func (m *IPMaskFilter) Provision(ctx caddy.Context) error {
// Filter filters the input field.
func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
+ newArray := make(caddyhttp.LoggableStringArray, len(array))
for i, s := range array {
- array[i] = m.mask(s)
+ newArray[i] = m.mask(s)
}
+ in.Interface = newArray
} else {
in.String = m.mask(in.String)
}
@@ -368,9 +373,23 @@ func (m *QueryFilter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Filter filters the input field.
func (m QueryFilter) Filter(in zapcore.Field) zapcore.Field {
- u, err := url.Parse(in.String)
+ if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
+ newArray := make(caddyhttp.LoggableStringArray, len(array))
+ for i, s := range array {
+ newArray[i] = m.processQueryString(s)
+ }
+ in.Interface = newArray
+ } else {
+ in.String = m.processQueryString(in.String)
+ }
+
+ return in
+}
+
+func (m QueryFilter) processQueryString(s string) string {
+ u, err := url.Parse(s)
if err != nil {
- return in
+ return s
}
q := u.Query()
@@ -392,9 +411,7 @@ func (m QueryFilter) Filter(in zapcore.Field) zapcore.Field {
}
u.RawQuery = q.Encode()
- in.String = u.String()
-
- return in
+ return u.String()
}
type cookieFilterAction struct {
@@ -580,9 +597,11 @@ func (m *RegexpFilter) Provision(ctx caddy.Context) error {
// Filter filters the input field with the replacement value if it matches the regexp.
func (f *RegexpFilter) Filter(in zapcore.Field) zapcore.Field {
if array, ok := in.Interface.(caddyhttp.LoggableStringArray); ok {
+ newArray := make(caddyhttp.LoggableStringArray, len(array))
for i, s := range array {
- array[i] = f.regexp.ReplaceAllString(s, f.Value)
+ newArray[i] = f.regexp.ReplaceAllString(s, f.Value)
}
+ in.Interface = newArray
} else {
in.String = f.regexp.ReplaceAllString(in.String, f.Value)
}
diff --git a/modules/logging/filters_test.go b/modules/logging/filters_test.go
index e9c3e77..8f7ba0d 100644
--- a/modules/logging/filters_test.go
+++ b/modules/logging/filters_test.go
@@ -83,7 +83,7 @@ func TestIPMaskMultiValue(t *testing.T) {
}
}
-func TestQueryFilter(t *testing.T) {
+func TestQueryFilterSingleValue(t *testing.T) {
f := QueryFilter{[]queryFilterAction{
{replaceAction, "foo", "REDACTED"},
{replaceAction, "notexist", "REDACTED"},
@@ -102,6 +102,40 @@ func TestQueryFilter(t *testing.T) {
}
}
+func TestQueryFilterMultiValue(t *testing.T) {
+ f := QueryFilter{
+ Actions: []queryFilterAction{
+ {Type: replaceAction, Parameter: "foo", Value: "REDACTED"},
+ {Type: replaceAction, Parameter: "notexist", Value: "REDACTED"},
+ {Type: deleteAction, Parameter: "bar"},
+ {Type: deleteAction, Parameter: "notexist"},
+ {Type: hashAction, Parameter: "hash"},
+ },
+ }
+
+ if f.Validate() != nil {
+ t.Fatalf("the filter must be valid")
+ }
+
+ out := f.Filter(zapcore.Field{Interface: caddyhttp.LoggableStringArray{
+ "/path1?foo=a&foo=b&bar=c&bar=d&baz=e&hash=hashed",
+ "/path2?foo=c&foo=d&bar=e&bar=f&baz=g&hash=hashed",
+ }})
+ arr, ok := out.Interface.(caddyhttp.LoggableStringArray)
+ if !ok {
+ t.Fatalf("field is wrong type: %T", out.Interface)
+ }
+
+ expected1 := "/path1?baz=e&foo=REDACTED&foo=REDACTED&hash=e3b0c442"
+ expected2 := "/path2?baz=g&foo=REDACTED&foo=REDACTED&hash=e3b0c442"
+ if arr[0] != expected1 {
+ t.Fatalf("query parameters in entry 0 have not been filtered correctly: got %s, expected %s", arr[0], expected1)
+ }
+ if arr[1] != expected2 {
+ t.Fatalf("query parameters in entry 1 have not been filtered correctly: got %s, expected %s", arr[1], expected2)
+ }
+}
+
func TestValidateQueryFilter(t *testing.T) {
f := QueryFilter{[]queryFilterAction{
{},
diff --git a/modules/logging/netwriter.go b/modules/logging/netwriter.go
index 5a6cf39..1939cb7 100644
--- a/modules/logging/netwriter.go
+++ b/modules/logging/netwriter.go
@@ -40,6 +40,11 @@ type NetWriter struct {
// The timeout to wait while connecting to the socket.
DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
+ // If enabled, allow connections errors when first opening the
+ // writer. The error and subsequent log entries will be reported
+ // to stderr instead until a connection can be re-established.
+ SoftStart bool `json:"soft_start,omitempty"`
+
addr caddy.NetworkAddress
}
@@ -92,7 +97,12 @@ func (nw NetWriter) OpenWriter() (io.WriteCloser, error) {
}
conn, err := reconn.dial()
if err != nil {
- return nil, err
+ if !nw.SoftStart {
+ return nil, err
+ }
+ // don't block config load if remote is down or some other external problem;
+ // we can dump logs to stderr for now (see issue #5520)
+ fmt.Fprintf(os.Stderr, "[ERROR] net log writer failed to connect: %v (will retry connection and print errors here in the meantime)\n", err)
}
reconn.connMu.Lock()
reconn.Conn = conn
@@ -104,6 +114,7 @@ func (nw NetWriter) OpenWriter() (io.WriteCloser, error) {
//
// net <address> {
// dial_timeout <duration>
+// soft_start
// }
func (nw *NetWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
@@ -128,6 +139,12 @@ func (nw *NetWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return d.ArgErr()
}
nw.DialTimeout = caddy.Duration(timeout)
+
+ case "soft_start":
+ if d.NextArg() {
+ return d.ArgErr()
+ }
+ nw.SoftStart = true
}
}
}
@@ -151,8 +168,10 @@ func (reconn *redialerConn) Write(b []byte) (n int, err error) {
reconn.connMu.RLock()
conn := reconn.Conn
reconn.connMu.RUnlock()
- if n, err = conn.Write(b); err == nil {
- return
+ if conn != nil {
+ if n, err = conn.Write(b); err == nil {
+ return
+ }
}
// problem with the connection - lock it and try to fix it
@@ -161,8 +180,10 @@ func (reconn *redialerConn) Write(b []byte) (n int, err error) {
// if multiple concurrent writes failed on the same broken conn, then
// one of them might have already re-dialed by now; try writing again
- if n, err = reconn.Conn.Write(b); err == nil {
- return
+ if reconn.Conn != nil {
+ if n, err = reconn.Conn.Write(b); err == nil {
+ return
+ }
}
// there's still a problem, so try to re-attempt dialing the socket
@@ -178,7 +199,9 @@ func (reconn *redialerConn) Write(b []byte) (n int, err error) {
return
}
if n, err = conn2.Write(b); err == nil {
- reconn.Conn.Close()
+ if reconn.Conn != nil {
+ reconn.Conn.Close()
+ }
reconn.Conn = conn2
}
} else {