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 (
|
2024-06-06 22:33:34 +08:00
|
|
|
"encoding/json"
|
2019-11-12 02:04:41 +08:00
|
|
|
"fmt"
|
2019-10-29 04:39:37 +08:00
|
|
|
"io"
|
2020-05-08 03:06:00 +08:00
|
|
|
"math"
|
2019-10-29 04:39:37 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-26 13:00:33 +08:00
|
|
|
"strconv"
|
2019-10-29 04:39:37 +08:00
|
|
|
|
2020-02-26 13:00:33 +08:00
|
|
|
"github.com/dustin/go-humanize"
|
2019-10-29 04:39:37 +08:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
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(FileWriter{})
|
|
|
|
}
|
|
|
|
|
2024-06-06 22:33:34 +08:00
|
|
|
// fileMode is a string made of 1 to 4 octal digits representing
|
|
|
|
// a numeric mode as specified with the `chmod` unix command.
|
|
|
|
// `"0777"` and `"777"` are thus equivalent values.
|
|
|
|
type fileMode os.FileMode
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
|
|
|
func (m *fileMode) UnmarshalJSON(b []byte) error {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, err := parseFileMode(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*m = fileMode(mode)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-09 01:34:18 +08:00
|
|
|
// MarshalJSON satisfies json.Marshaler.
|
|
|
|
func (m *fileMode) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf("\"%04o\"", *m)), nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 22:33:34 +08:00
|
|
|
// parseFileMode parses a file mode string,
|
|
|
|
// adding support for `chmod` unix command like
|
|
|
|
// 1 to 4 digital octal values.
|
|
|
|
func parseFileMode(s string) (os.FileMode, error) {
|
|
|
|
modeStr := fmt.Sprintf("%04s", s)
|
|
|
|
mode, err := strconv.ParseUint(modeStr, 8, 32)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return os.FileMode(mode), nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 04:36:46 +08:00
|
|
|
// FileWriter can write logs to files. By default, log files
|
|
|
|
// are rotated ("rolled") when they get large, and old log
|
|
|
|
// files get deleted, to ensure that the process does not
|
|
|
|
// exhaust disk space.
|
2019-10-29 04:39:37 +08:00
|
|
|
type FileWriter struct {
|
2019-12-11 04:36:46 +08:00
|
|
|
// Filename is the name of the file to write.
|
|
|
|
Filename string `json:"filename,omitempty"`
|
|
|
|
|
2024-06-06 22:33:34 +08:00
|
|
|
// The file permissions mode.
|
|
|
|
// 0600 by default.
|
|
|
|
Mode fileMode `json:"mode,omitempty"`
|
|
|
|
|
2019-12-11 04:36:46 +08:00
|
|
|
// Roll toggles log rolling or rotation, which is
|
|
|
|
// enabled by default.
|
|
|
|
Roll *bool `json:"roll,omitempty"`
|
|
|
|
|
|
|
|
// When a log file reaches approximately this size,
|
|
|
|
// it will be rotated.
|
|
|
|
RollSizeMB int `json:"roll_size_mb,omitempty"`
|
|
|
|
|
|
|
|
// Whether to compress rolled files. Default: true
|
|
|
|
RollCompress *bool `json:"roll_gzip,omitempty"`
|
|
|
|
|
|
|
|
// Whether to use local timestamps in rolled filenames.
|
|
|
|
// Default: false
|
|
|
|
RollLocalTime bool `json:"roll_local_time,omitempty"`
|
|
|
|
|
|
|
|
// The maximum number of rolled log files to keep.
|
|
|
|
// Default: 10
|
|
|
|
RollKeep int `json:"roll_keep,omitempty"`
|
|
|
|
|
|
|
|
// How many days to keep rolled log files. Default: 90
|
|
|
|
RollKeepDays int `json:"roll_keep_days,omitempty"`
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (FileWriter) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-11 04:36:46 +08:00
|
|
|
ID: "caddy.logging.writers.file",
|
|
|
|
New: func() caddy.Module { return new(FileWriter) },
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 02:04:41 +08:00
|
|
|
// Provision sets up the module
|
|
|
|
func (fw *FileWriter) Provision(ctx caddy.Context) error {
|
|
|
|
// Replace placeholder in filename
|
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
filename, err := repl.ReplaceOrErr(fw.Filename, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid filename for log file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Filename = filename
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
func (fw FileWriter) String() string {
|
|
|
|
fpath, err := filepath.Abs(fw.Filename)
|
|
|
|
if err == nil {
|
|
|
|
return fpath
|
|
|
|
}
|
|
|
|
return fw.Filename
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriterKey returns a unique key representing this fw.
|
|
|
|
func (fw FileWriter) WriterKey() string {
|
|
|
|
return "file:" + fw.Filename
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenWriter opens a new file writer.
|
|
|
|
func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
|
2024-06-06 22:33:34 +08:00
|
|
|
if fw.Mode == 0 {
|
|
|
|
fw.Mode = 0o600
|
|
|
|
}
|
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
// roll log files by default
|
2020-01-04 02:33:22 +08:00
|
|
|
if fw.Roll == nil || *fw.Roll {
|
2019-10-29 04:39:37 +08:00
|
|
|
if fw.RollSizeMB == 0 {
|
|
|
|
fw.RollSizeMB = 100
|
|
|
|
}
|
|
|
|
if fw.RollCompress == nil {
|
|
|
|
compress := true
|
|
|
|
fw.RollCompress = &compress
|
|
|
|
}
|
|
|
|
if fw.RollKeep == 0 {
|
|
|
|
fw.RollKeep = 10
|
|
|
|
}
|
|
|
|
if fw.RollKeepDays == 0 {
|
|
|
|
fw.RollKeepDays = 90
|
|
|
|
}
|
2019-11-12 02:04:41 +08:00
|
|
|
|
2024-06-13 05:17:46 +08:00
|
|
|
// create the file if it does not exist with the right mode.
|
|
|
|
// lumberjack will reuse the file mode across log rotation.
|
|
|
|
f_tmp, err := os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(fw.Mode))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-06 22:33:34 +08:00
|
|
|
f_tmp.Close()
|
2024-06-13 05:17:46 +08:00
|
|
|
// ensure already existing files have the right mode,
|
|
|
|
// since OpenFile will not set the mode in such case.
|
|
|
|
if err = os.Chmod(fw.Filename, os.FileMode(fw.Mode)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-06 22:33:34 +08:00
|
|
|
|
2019-10-29 04:39:37 +08:00
|
|
|
return &lumberjack.Logger{
|
|
|
|
Filename: fw.Filename,
|
|
|
|
MaxSize: fw.RollSizeMB,
|
|
|
|
MaxAge: fw.RollKeepDays,
|
|
|
|
MaxBackups: fw.RollKeep,
|
|
|
|
LocalTime: fw.RollLocalTime,
|
|
|
|
Compress: *fw.RollCompress,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise just open a regular file
|
2024-06-06 22:33:34 +08:00
|
|
|
return os.OpenFile(fw.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(fw.Mode))
|
2019-10-29 04:39:37 +08:00
|
|
|
}
|
2019-11-12 02:04:41 +08:00
|
|
|
|
2020-02-26 13:00:33 +08:00
|
|
|
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2022-11-15 00:38:02 +08:00
|
|
|
// file <filename> {
|
2024-06-06 22:33:34 +08:00
|
|
|
// mode <mode>
|
2022-11-15 00:38:02 +08:00
|
|
|
// roll_disabled
|
|
|
|
// roll_size <size>
|
|
|
|
// roll_uncompressed
|
|
|
|
// roll_local_time
|
|
|
|
// roll_keep <num>
|
|
|
|
// roll_keep_for <days>
|
|
|
|
// }
|
2020-02-26 13:00:33 +08:00
|
|
|
//
|
2020-05-08 03:06:00 +08:00
|
|
|
// The roll_size value has megabyte resolution.
|
|
|
|
// Fractional values are rounded up to the next whole megabyte (MiB).
|
|
|
|
//
|
2022-01-05 03:11:27 +08:00
|
|
|
// By default, compression is enabled, but can be turned off by setting
|
|
|
|
// the roll_uncompressed option.
|
|
|
|
//
|
2020-05-08 03:06:00 +08:00
|
|
|
// The roll_keep_for duration has day resolution.
|
|
|
|
// Fractional values are rounded up to the next whole number of days.
|
|
|
|
//
|
2024-06-06 22:33:34 +08:00
|
|
|
// If any of the mode, roll_size, roll_keep, or roll_keep_for subdirectives are
|
2020-05-08 03:06:00 +08:00
|
|
|
// omitted or set to a zero value, then Caddy's default value for that
|
|
|
|
// subdirective is used.
|
2020-02-26 13:00:33 +08:00
|
|
|
func (fw *FileWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
2024-01-24 08:36:59 +08:00
|
|
|
d.Next() // consume writer name
|
|
|
|
if !d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
fw.Filename = d.Val()
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
for d.NextBlock(0) {
|
|
|
|
switch d.Val() {
|
2024-06-06 22:33:34 +08:00
|
|
|
case "mode":
|
|
|
|
var modeStr string
|
|
|
|
if !d.AllArgs(&modeStr) {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
mode, err := parseFileMode(modeStr)
|
|
|
|
if err != nil {
|
|
|
|
return d.Errf("parsing mode: %v", err)
|
|
|
|
}
|
|
|
|
fw.Mode = fileMode(mode)
|
|
|
|
|
2024-01-24 08:36:59 +08:00
|
|
|
case "roll_disabled":
|
|
|
|
var f bool
|
|
|
|
fw.Roll = &f
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
case "roll_size":
|
|
|
|
var sizeStr string
|
|
|
|
if !d.AllArgs(&sizeStr) {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
size, err := humanize.ParseBytes(sizeStr)
|
|
|
|
if err != nil {
|
|
|
|
return d.Errf("parsing size: %v", err)
|
|
|
|
}
|
|
|
|
fw.RollSizeMB = int(math.Ceil(float64(size) / humanize.MiByte))
|
2020-02-26 13:00:33 +08:00
|
|
|
|
2024-01-24 08:36:59 +08:00
|
|
|
case "roll_uncompressed":
|
|
|
|
var f bool
|
|
|
|
fw.RollCompress = &f
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
case "roll_local_time":
|
|
|
|
fw.RollLocalTime = true
|
|
|
|
if d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
case "roll_keep":
|
|
|
|
var keepStr string
|
|
|
|
if !d.AllArgs(&keepStr) {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
keep, err := strconv.Atoi(keepStr)
|
|
|
|
if err != nil {
|
|
|
|
return d.Errf("parsing roll_keep number: %v", err)
|
|
|
|
}
|
|
|
|
fw.RollKeep = keep
|
|
|
|
|
|
|
|
case "roll_keep_for":
|
|
|
|
var keepForStr string
|
|
|
|
if !d.AllArgs(&keepForStr) {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
keepFor, err := caddy.ParseDuration(keepForStr)
|
|
|
|
if err != nil {
|
|
|
|
return d.Errf("parsing roll_keep_for duration: %v", err)
|
|
|
|
}
|
|
|
|
if keepFor < 0 {
|
|
|
|
return d.Errf("negative roll_keep_for duration: %v", keepFor)
|
2020-02-26 13:00:33 +08:00
|
|
|
}
|
2024-01-24 08:36:59 +08:00
|
|
|
fw.RollKeepDays = int(math.Ceil(keepFor.Hours() / 24))
|
2020-02-26 13:00:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 02:04:41 +08:00
|
|
|
// Interface guards
|
|
|
|
var (
|
2020-02-26 13:00:33 +08:00
|
|
|
_ caddy.Provisioner = (*FileWriter)(nil)
|
|
|
|
_ caddy.WriterOpener = (*FileWriter)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*FileWriter)(nil)
|
2019-11-12 02:04:41 +08:00
|
|
|
)
|