2019-10-29 04:39:37 +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 logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/buffer"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2023-08-14 23:41:15 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
2019-10-29 04:39:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy.RegisterModule(ConsoleEncoder{})
|
|
|
|
caddy.RegisterModule(JSONEncoder{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsoleEncoder encodes log entries that are mostly human-readable.
|
|
|
|
type ConsoleEncoder struct {
|
2019-12-24 03:45:35 +08:00
|
|
|
zapcore.Encoder `json:"-"`
|
2019-10-29 04:39:37 +08:00
|
|
|
LogEncoderConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (ConsoleEncoder) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-11 04:36:46 +08:00
|
|
|
ID: "caddy.logging.encoders.console",
|
|
|
|
New: func() caddy.Module { return new(ConsoleEncoder) },
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision sets up the encoder.
|
|
|
|
func (ce *ConsoleEncoder) Provision(_ caddy.Context) error {
|
2022-10-05 11:18:48 +08:00
|
|
|
if ce.LevelFormat == "" {
|
|
|
|
ce.LevelFormat = "color"
|
|
|
|
}
|
|
|
|
if ce.TimeFormat == "" {
|
|
|
|
ce.TimeFormat = "wall_milli"
|
|
|
|
}
|
2019-10-29 04:39:37 +08:00
|
|
|
ce.Encoder = zapcore.NewConsoleEncoder(ce.ZapcoreEncoderConfig())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:00:33 +08:00
|
|
|
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2022-10-05 11:18:48 +08:00
|
|
|
// console {
|
|
|
|
// <common encoder config subdirectives...>
|
|
|
|
// }
|
2020-02-26 13:00:33 +08:00
|
|
|
//
|
|
|
|
// See the godoc on the LogEncoderConfig type for the syntax of
|
|
|
|
// subdirectives that are common to most/all encoders.
|
|
|
|
func (ce *ConsoleEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
2024-01-24 08:36:59 +08:00
|
|
|
d.Next() // consume encoder name
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
err := ce.LogEncoderConfig.UnmarshalCaddyfile(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-26 13:00:33 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
// JSONEncoder encodes entries as JSON.
|
|
|
|
type JSONEncoder struct {
|
2019-12-24 03:45:35 +08:00
|
|
|
zapcore.Encoder `json:"-"`
|
|
|
|
LogEncoderConfig
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (JSONEncoder) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-11 04:36:46 +08:00
|
|
|
ID: "caddy.logging.encoders.json",
|
|
|
|
New: func() caddy.Module { return new(JSONEncoder) },
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision sets up the encoder.
|
|
|
|
func (je *JSONEncoder) Provision(_ caddy.Context) error {
|
|
|
|
je.Encoder = zapcore.NewJSONEncoder(je.ZapcoreEncoderConfig())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:00:33 +08:00
|
|
|
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2022-10-05 11:18:48 +08:00
|
|
|
// json {
|
|
|
|
// <common encoder config subdirectives...>
|
|
|
|
// }
|
2020-02-26 13:00:33 +08:00
|
|
|
//
|
|
|
|
// See the godoc on the LogEncoderConfig type for the syntax of
|
|
|
|
// subdirectives that are common to most/all encoders.
|
|
|
|
func (je *JSONEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
2024-01-24 08:36:59 +08:00
|
|
|
d.Next() // consume encoder name
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
err := je.LogEncoderConfig.UnmarshalCaddyfile(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-26 13:00:33 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
// LogEncoderConfig holds configuration common to most encoders.
|
|
|
|
type LogEncoderConfig struct {
|
|
|
|
MessageKey *string `json:"message_key,omitempty"`
|
|
|
|
LevelKey *string `json:"level_key,omitempty"`
|
|
|
|
TimeKey *string `json:"time_key,omitempty"`
|
|
|
|
NameKey *string `json:"name_key,omitempty"`
|
|
|
|
CallerKey *string `json:"caller_key,omitempty"`
|
|
|
|
StacktraceKey *string `json:"stacktrace_key,omitempty"`
|
|
|
|
LineEnding *string `json:"line_ending,omitempty"`
|
|
|
|
TimeFormat string `json:"time_format,omitempty"`
|
2022-10-05 12:23:14 +08:00
|
|
|
TimeLocal bool `json:"time_local,omitempty"`
|
2019-10-29 04:39:37 +08:00
|
|
|
DurationFormat string `json:"duration_format,omitempty"`
|
|
|
|
LevelFormat string `json:"level_format,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:00:33 +08:00
|
|
|
// UnmarshalCaddyfile populates the struct from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2022-10-05 11:18:48 +08:00
|
|
|
// {
|
|
|
|
// message_key <key>
|
|
|
|
// level_key <key>
|
|
|
|
// time_key <key>
|
|
|
|
// name_key <key>
|
|
|
|
// caller_key <key>
|
|
|
|
// stacktrace_key <key>
|
|
|
|
// line_ending <char>
|
|
|
|
// time_format <format>
|
2022-10-05 12:23:14 +08:00
|
|
|
// time_local
|
2022-10-05 11:18:48 +08:00
|
|
|
// duration_format <format>
|
|
|
|
// level_format <format>
|
|
|
|
// }
|
2020-02-26 13:00:33 +08:00
|
|
|
func (lec *LogEncoderConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
2024-01-24 08:36:59 +08:00
|
|
|
for d.NextBlock(0) {
|
2020-02-26 13:00:33 +08:00
|
|
|
subdir := d.Val()
|
2022-10-05 12:23:14 +08:00
|
|
|
switch subdir {
|
|
|
|
case "time_local":
|
|
|
|
lec.TimeLocal = true
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-02-26 13:00:33 +08:00
|
|
|
var arg string
|
|
|
|
if !d.AllArgs(&arg) {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
switch subdir {
|
|
|
|
case "message_key":
|
|
|
|
lec.MessageKey = &arg
|
|
|
|
case "level_key":
|
|
|
|
lec.LevelKey = &arg
|
|
|
|
case "time_key":
|
|
|
|
lec.TimeKey = &arg
|
|
|
|
case "name_key":
|
|
|
|
lec.NameKey = &arg
|
|
|
|
case "caller_key":
|
|
|
|
lec.CallerKey = &arg
|
|
|
|
case "stacktrace_key":
|
|
|
|
lec.StacktraceKey = &arg
|
|
|
|
case "line_ending":
|
|
|
|
lec.LineEnding = &arg
|
|
|
|
case "time_format":
|
|
|
|
lec.TimeFormat = arg
|
2022-04-08 01:23:28 +08:00
|
|
|
case "duration_format":
|
|
|
|
lec.DurationFormat = arg
|
2020-02-26 13:00:33 +08:00
|
|
|
case "level_format":
|
|
|
|
lec.LevelFormat = arg
|
|
|
|
default:
|
|
|
|
return d.Errf("unrecognized subdirective %s", subdir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
// ZapcoreEncoderConfig returns the equivalent zapcore.EncoderConfig.
|
|
|
|
// If lec is nil, zap.NewProductionEncoderConfig() is returned.
|
|
|
|
func (lec *LogEncoderConfig) ZapcoreEncoderConfig() zapcore.EncoderConfig {
|
|
|
|
cfg := zap.NewProductionEncoderConfig()
|
|
|
|
if lec == nil {
|
|
|
|
lec = new(LogEncoderConfig)
|
|
|
|
}
|
|
|
|
if lec.MessageKey != nil {
|
|
|
|
cfg.MessageKey = *lec.MessageKey
|
|
|
|
}
|
2021-06-05 04:15:43 +08:00
|
|
|
if lec.LevelKey != nil {
|
|
|
|
cfg.LevelKey = *lec.LevelKey
|
|
|
|
}
|
2019-10-29 04:39:37 +08:00
|
|
|
if lec.TimeKey != nil {
|
|
|
|
cfg.TimeKey = *lec.TimeKey
|
|
|
|
}
|
|
|
|
if lec.NameKey != nil {
|
|
|
|
cfg.NameKey = *lec.NameKey
|
|
|
|
}
|
|
|
|
if lec.CallerKey != nil {
|
|
|
|
cfg.CallerKey = *lec.CallerKey
|
|
|
|
}
|
|
|
|
if lec.StacktraceKey != nil {
|
|
|
|
cfg.StacktraceKey = *lec.StacktraceKey
|
|
|
|
}
|
|
|
|
if lec.LineEnding != nil {
|
|
|
|
cfg.LineEnding = *lec.LineEnding
|
|
|
|
}
|
|
|
|
|
|
|
|
// time format
|
|
|
|
var timeFormatter zapcore.TimeEncoder
|
|
|
|
switch lec.TimeFormat {
|
|
|
|
case "", "unix_seconds_float":
|
|
|
|
timeFormatter = zapcore.EpochTimeEncoder
|
|
|
|
case "unix_milli_float":
|
|
|
|
timeFormatter = zapcore.EpochMillisTimeEncoder
|
|
|
|
case "unix_nano":
|
|
|
|
timeFormatter = zapcore.EpochNanosTimeEncoder
|
|
|
|
case "iso8601":
|
|
|
|
timeFormatter = zapcore.ISO8601TimeEncoder
|
|
|
|
default:
|
|
|
|
timeFormat := lec.TimeFormat
|
|
|
|
switch lec.TimeFormat {
|
|
|
|
case "rfc3339":
|
|
|
|
timeFormat = time.RFC3339
|
|
|
|
case "rfc3339_nano":
|
|
|
|
timeFormat = time.RFC3339Nano
|
|
|
|
case "wall":
|
|
|
|
timeFormat = "2006/01/02 15:04:05"
|
|
|
|
case "wall_milli":
|
|
|
|
timeFormat = "2006/01/02 15:04:05.000"
|
|
|
|
case "wall_nano":
|
|
|
|
timeFormat = "2006/01/02 15:04:05.000000000"
|
2021-07-15 01:07:38 +08:00
|
|
|
case "common_log":
|
|
|
|
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
timeFormatter = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
|
2022-10-05 12:23:14 +08:00
|
|
|
var time time.Time
|
|
|
|
if lec.TimeLocal {
|
|
|
|
time = ts.Local()
|
|
|
|
} else {
|
|
|
|
time = ts.UTC()
|
|
|
|
}
|
|
|
|
encoder.AppendString(time.Format(timeFormat))
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.EncodeTime = timeFormatter
|
|
|
|
|
|
|
|
// duration format
|
|
|
|
var durFormatter zapcore.DurationEncoder
|
|
|
|
switch lec.DurationFormat {
|
|
|
|
case "", "seconds":
|
|
|
|
durFormatter = zapcore.SecondsDurationEncoder
|
|
|
|
case "nano":
|
|
|
|
durFormatter = zapcore.NanosDurationEncoder
|
|
|
|
case "string":
|
|
|
|
durFormatter = zapcore.StringDurationEncoder
|
|
|
|
}
|
|
|
|
cfg.EncodeDuration = durFormatter
|
|
|
|
|
|
|
|
// level format
|
|
|
|
var levelFormatter zapcore.LevelEncoder
|
|
|
|
switch lec.LevelFormat {
|
|
|
|
case "", "lower":
|
|
|
|
levelFormatter = zapcore.LowercaseLevelEncoder
|
|
|
|
case "upper":
|
|
|
|
levelFormatter = zapcore.CapitalLevelEncoder
|
|
|
|
case "color":
|
|
|
|
levelFormatter = zapcore.CapitalColorLevelEncoder
|
|
|
|
}
|
|
|
|
cfg.EncodeLevel = levelFormatter
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufferpool = buffer.NewPool()
|
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ zapcore.Encoder = (*ConsoleEncoder)(nil)
|
|
|
|
_ zapcore.Encoder = (*JSONEncoder)(nil)
|
2020-02-26 13:00:33 +08:00
|
|
|
|
|
|
|
_ caddyfile.Unmarshaler = (*ConsoleEncoder)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*JSONEncoder)(nil)
|
2019-10-29 04:39:37 +08:00
|
|
|
)
|