2022-09-16 00:05:36 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
2024-04-17 06:26:18 +08:00
|
|
|
"encoding/json"
|
2022-09-16 00:05:36 +08:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2023-08-14 23:41:15 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
2022-09-16 00:05:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerLogConfig describes a server's logging configuration. If
|
|
|
|
// enabled without customization, all requests to this server are
|
|
|
|
// logged to the default logger; logger destinations may be
|
|
|
|
// customized per-request-host.
|
|
|
|
type ServerLogConfig struct {
|
|
|
|
// The default logger name for all logs emitted by this server for
|
2024-04-17 06:26:18 +08:00
|
|
|
// hostnames that are not in the logger_names map.
|
2022-09-16 00:05:36 +08:00
|
|
|
DefaultLoggerName string `json:"default_logger_name,omitempty"`
|
|
|
|
|
2024-04-17 06:26:18 +08:00
|
|
|
// LoggerNames maps request hostnames to one or more custom logger
|
2024-04-22 20:33:07 +08:00
|
|
|
// names. For example, a mapping of `"example.com": ["example"]` would
|
2024-04-17 06:26:18 +08:00
|
|
|
// cause access logs from requests with a Host of example.com to be
|
|
|
|
// emitted by a logger named "http.log.access.example". If there are
|
|
|
|
// multiple logger names, then the log will be emitted to all of them.
|
2024-04-22 20:33:07 +08:00
|
|
|
// If the logger name is an empty, the default logger is used, i.e.
|
|
|
|
// the logger "http.log.access".
|
|
|
|
//
|
|
|
|
// Keys must be hostnames (without ports), and may contain wildcards
|
|
|
|
// to match subdomains. The value is an array of logger names.
|
|
|
|
//
|
2024-04-17 06:26:18 +08:00
|
|
|
// For backwards compatibility, if the value is a string, it is treated
|
|
|
|
// as a single-element array.
|
|
|
|
LoggerNames map[string]StringArray `json:"logger_names,omitempty"`
|
2022-09-16 00:05:36 +08:00
|
|
|
|
|
|
|
// By default, all requests to this server will be logged if
|
|
|
|
// access logging is enabled. This field lists the request
|
|
|
|
// hosts for which access logging should be disabled.
|
|
|
|
SkipHosts []string `json:"skip_hosts,omitempty"`
|
|
|
|
|
|
|
|
// If true, requests to any host not appearing in the
|
2024-04-17 06:26:18 +08:00
|
|
|
// logger_names map will not be logged.
|
2022-09-16 00:05:36 +08:00
|
|
|
SkipUnmappedHosts bool `json:"skip_unmapped_hosts,omitempty"`
|
|
|
|
|
|
|
|
// If true, credentials that are otherwise omitted, will be logged.
|
|
|
|
// The definition of credentials is defined by https://fetch.spec.whatwg.org/#credentials,
|
|
|
|
// and this includes some request and response headers, i.e `Cookie`,
|
|
|
|
// `Set-Cookie`, `Authorization`, and `Proxy-Authorization`.
|
|
|
|
ShouldLogCredentials bool `json:"should_log_credentials,omitempty"`
|
2024-05-19 04:48:42 +08:00
|
|
|
|
|
|
|
// Log each individual handler that is invoked.
|
|
|
|
// Requires that the log emit at DEBUG level.
|
|
|
|
//
|
|
|
|
// NOTE: This may log the configuration of your
|
|
|
|
// HTTP handler modules; do not enable this in
|
|
|
|
// insecure contexts when there is sensitive
|
|
|
|
// data in the configuration.
|
|
|
|
//
|
|
|
|
// EXPERIMENTAL: Subject to change or removal.
|
|
|
|
Trace bool `json:"trace,omitempty"`
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
|
|
|
|
2024-04-17 06:26:18 +08:00
|
|
|
// wrapLogger wraps logger in one or more logger named
|
|
|
|
// according to user preferences for the given host.
|
2024-05-11 21:31:44 +08:00
|
|
|
func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, req *http.Request) []*zap.Logger {
|
|
|
|
// using the `log_name` directive or the `access_logger_names` variable,
|
|
|
|
// the logger names can be overridden for the current request
|
|
|
|
if names := GetVar(req.Context(), AccessLoggerNameVarKey); names != nil {
|
|
|
|
if namesSlice, ok := names.([]any); ok {
|
|
|
|
loggers := make([]*zap.Logger, 0, len(namesSlice))
|
|
|
|
for _, loggerName := range namesSlice {
|
|
|
|
// no name, use the default logger
|
|
|
|
if loggerName == "" {
|
|
|
|
loggers = append(loggers, logger)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// make a logger with the given name
|
|
|
|
loggers = append(loggers, logger.Named(loggerName.(string)))
|
|
|
|
}
|
|
|
|
return loggers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the hostname from the request, with the port number stripped
|
|
|
|
host, _, err := net.SplitHostPort(req.Host)
|
2024-04-22 20:33:07 +08:00
|
|
|
if err != nil {
|
2024-05-11 21:31:44 +08:00
|
|
|
host = req.Host
|
2024-04-22 20:33:07 +08:00
|
|
|
}
|
|
|
|
|
2024-05-11 21:31:44 +08:00
|
|
|
// get the logger names for this host from the config
|
|
|
|
hosts := slc.getLoggerHosts(host)
|
|
|
|
|
|
|
|
// make a list of named loggers, or the default logger
|
2024-04-17 06:26:18 +08:00
|
|
|
loggers := make([]*zap.Logger, 0, len(hosts))
|
|
|
|
for _, loggerName := range hosts {
|
2024-04-22 20:33:07 +08:00
|
|
|
// no name, use the default logger
|
2024-04-17 06:26:18 +08:00
|
|
|
if loggerName == "" {
|
2024-04-22 20:33:07 +08:00
|
|
|
loggers = append(loggers, logger)
|
2024-04-17 06:26:18 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-05-11 21:31:44 +08:00
|
|
|
// make a logger with the given name
|
2024-04-17 06:26:18 +08:00
|
|
|
loggers = append(loggers, logger.Named(loggerName))
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
2024-04-17 06:26:18 +08:00
|
|
|
return loggers
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
|
|
|
|
2024-04-17 06:26:18 +08:00
|
|
|
func (slc ServerLogConfig) getLoggerHosts(host string) []string {
|
2022-09-16 00:05:36 +08:00
|
|
|
// try the exact hostname first
|
2024-04-22 20:33:07 +08:00
|
|
|
if hosts, ok := slc.LoggerNames[host]; ok {
|
2024-04-17 06:26:18 +08:00
|
|
|
return hosts
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// try matching wildcard domains if other non-specific loggers exist
|
|
|
|
labels := strings.Split(host, ".")
|
|
|
|
for i := range labels {
|
|
|
|
if labels[i] == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labels[i] = "*"
|
|
|
|
wildcardHost := strings.Join(labels, ".")
|
2024-04-22 20:33:07 +08:00
|
|
|
if hosts, ok := slc.LoggerNames[wildcardHost]; ok {
|
2024-04-17 06:26:18 +08:00
|
|
|
return hosts
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 06:26:18 +08:00
|
|
|
return []string{slc.DefaultLoggerName}
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (slc *ServerLogConfig) clone() *ServerLogConfig {
|
|
|
|
clone := &ServerLogConfig{
|
|
|
|
DefaultLoggerName: slc.DefaultLoggerName,
|
2024-04-17 06:26:18 +08:00
|
|
|
LoggerNames: make(map[string]StringArray),
|
2022-09-16 00:05:36 +08:00
|
|
|
SkipHosts: append([]string{}, slc.SkipHosts...),
|
|
|
|
SkipUnmappedHosts: slc.SkipUnmappedHosts,
|
|
|
|
ShouldLogCredentials: slc.ShouldLogCredentials,
|
|
|
|
}
|
|
|
|
for k, v := range slc.LoggerNames {
|
2024-04-17 06:26:18 +08:00
|
|
|
clone.LoggerNames[k] = append([]string{}, v...)
|
2022-09-16 00:05:36 +08:00
|
|
|
}
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2024-04-17 06:26:18 +08:00
|
|
|
// StringArray is a slices of strings, but also accepts
|
|
|
|
// a single string as a value when JSON unmarshaling,
|
|
|
|
// converting it to a slice of one string.
|
|
|
|
type StringArray []string
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
|
|
|
func (sa *StringArray) UnmarshalJSON(b []byte) error {
|
|
|
|
var jsonObj any
|
|
|
|
err := json.Unmarshal(b, &jsonObj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch obj := jsonObj.(type) {
|
|
|
|
case string:
|
|
|
|
*sa = StringArray([]string{obj})
|
|
|
|
return nil
|
|
|
|
case []any:
|
|
|
|
s := make([]string, 0, len(obj))
|
|
|
|
for _, v := range obj {
|
|
|
|
value, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("unsupported type")
|
|
|
|
}
|
|
|
|
s = append(s, value)
|
|
|
|
}
|
|
|
|
*sa = StringArray(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("unsupported type")
|
|
|
|
}
|
|
|
|
|
2022-09-16 00:05:36 +08:00
|
|
|
// errLogValues inspects err and returns the status code
|
|
|
|
// to use, the error log message, and any extra fields.
|
|
|
|
// If err is a HandlerError, the returned values will
|
|
|
|
// have richer information.
|
|
|
|
func errLogValues(err error) (status int, msg string, fields []zapcore.Field) {
|
|
|
|
var handlerErr HandlerError
|
|
|
|
if errors.As(err, &handlerErr) {
|
|
|
|
status = handlerErr.StatusCode
|
|
|
|
if handlerErr.Err == nil {
|
|
|
|
msg = err.Error()
|
|
|
|
} else {
|
|
|
|
msg = handlerErr.Err.Error()
|
|
|
|
}
|
|
|
|
fields = []zapcore.Field{
|
|
|
|
zap.Int("status", handlerErr.StatusCode),
|
|
|
|
zap.String("err_id", handlerErr.ID),
|
|
|
|
zap.String("err_trace", handlerErr.Trace),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
status = http.StatusInternalServerError
|
|
|
|
msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:46:41 +08:00
|
|
|
// ExtraLogFields is a list of extra fields to log with every request.
|
|
|
|
type ExtraLogFields struct {
|
|
|
|
fields []zapcore.Field
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a field to the list of extra fields to log.
|
|
|
|
func (e *ExtraLogFields) Add(field zap.Field) {
|
|
|
|
e.fields = append(e.fields, field)
|
|
|
|
}
|
|
|
|
|
2023-12-14 06:40:15 +08:00
|
|
|
// Set sets a field in the list of extra fields to log.
|
|
|
|
// If the field already exists, it is replaced.
|
|
|
|
func (e *ExtraLogFields) Set(field zap.Field) {
|
|
|
|
for i := range e.fields {
|
|
|
|
if e.fields[i].Key == field.Key {
|
|
|
|
e.fields[i] = field
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.fields = append(e.fields, field)
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:46:41 +08:00
|
|
|
const (
|
|
|
|
// Variable name used to indicate that this request
|
|
|
|
// should be omitted from the access logs
|
2024-03-06 08:03:59 +08:00
|
|
|
LogSkipVar string = "log_skip"
|
2023-04-27 10:46:41 +08:00
|
|
|
|
|
|
|
// For adding additional fields to the access logs
|
|
|
|
ExtraLogFieldsCtxKey caddy.CtxKey = "extra_log_fields"
|
2024-05-11 21:31:44 +08:00
|
|
|
|
|
|
|
// Variable name used to indicate the logger to be used
|
|
|
|
AccessLoggerNameVarKey string = "access_logger_names"
|
2023-04-27 10:46:41 +08:00
|
|
|
)
|