Skip to content

Commit

Permalink
internalize the CLF template (#8)
Browse files Browse the repository at this point in the history
* internalize the CLF template

The field `{common_log}` is being deprecated along with all of its reference (see: caddyserver/caddy#4148). This commit is the first step in rebuilding the CLF template to avoid breaking users of this module for the sake of reconstructing CLF.

* use remote_addr, which includes the port, in place of the remote host

BREAKING: users of CLF will start seeing the port along with the remote host
  • Loading branch information
mohammed90 committed Jul 26, 2021
1 parent c10acf8 commit cd12651
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 184 deletions.
24 changes: 19 additions & 5 deletions formatencoder.go
Expand Up @@ -29,7 +29,14 @@ func init() {
caddy.RegisterModule(FormattedEncoder{})
}

const commonLogFormat = `{common_log}`
const (
// commonLogFormat is the common log format. https://en.wikipedia.org/wiki/Common_Log_Format
commonLogEmptyValue = "-"
commonLogFormat = `{request>remote_addr} ` + commonLogEmptyValue + ` {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size}`
commonLogTimeFormat = "02/Jan/2006:15:04:05 -0700"

commonLogFormatShortcut = `{common_log}`
)

// FormattedEncoder allows the user to provide custom template for log prints. The
// encoder builds atop the json encoder, thus it follows its message structure. The placeholders
Expand Down Expand Up @@ -57,8 +64,14 @@ func (se *FormattedEncoder) Provision(ctx caddy.Context) error {
if se.Template == "" {
return fmt.Errorf("missing template for formatted log encoder")
}
if se.Template == commonLogFormatShortcut {
se.Template = commonLogFormat
}
if se.Template == commonLogFormat {
se.TimeFormat = commonLogTimeFormat
}
if se.Placeholder == "" {
se.Placeholder = `-`
se.Placeholder = commonLogEmptyValue
}
se.Encoder = zapcore.NewJSONEncoder(se.ZapcoreEncoderConfig())
return nil
Expand All @@ -71,9 +84,10 @@ func (se *FormattedEncoder) Provision(ctx caddy.Context) error {
// we'd lose our FormattedEncoder's EncodeEntry.
func (se FormattedEncoder) Clone() zapcore.Encoder {
return FormattedEncoder{
Encoder: se.Encoder.Clone(),
Template: se.Template,
Placeholder: se.Placeholder,
LogEncoderConfig: se.LogEncoderConfig,
Encoder: se.Encoder.Clone(),
Template: se.Template,
Placeholder: se.Placeholder,
}
}

Expand Down

0 comments on commit cd12651

Please sign in to comment.