Commit 930ca1cc authored by Matthew Holt's avatar Matthew Holt

main,log,errors: Option to disable log rotation ("rolling")

For log and errors directive, as well as process log.
parent 23627bbf
...@@ -56,6 +56,8 @@ func init() { ...@@ -56,6 +56,8 @@ func init() {
flag.StringVar(&certmagic.Email, "email", "", "Default ACME CA account email address") flag.StringVar(&certmagic.Email, "email", "", "Default ACME CA account email address")
flag.DurationVar(&certmagic.HTTPTimeout, "catimeout", certmagic.HTTPTimeout, "Default ACME CA HTTP timeout") flag.DurationVar(&certmagic.HTTPTimeout, "catimeout", certmagic.HTTPTimeout, "Default ACME CA HTTP timeout")
flag.StringVar(&logfile, "log", "", "Process log file") flag.StringVar(&logfile, "log", "", "Process log file")
flag.IntVar(&logRollMB, "log-roll-mb", 100, "Roll process log when it reaches this many megabytes (0 to disable rolling)")
flag.BoolVar(&logRollCompress, "log-roll-compress", true, "Gzip-compress rolled process log files")
flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file") flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
flag.BoolVar(&caddy.Quiet, "quiet", false, "Quiet mode (no initialization output)") flag.BoolVar(&caddy.Quiet, "quiet", false, "Quiet mode (no initialization output)")
flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate") flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate")
...@@ -84,12 +86,26 @@ func Run() { ...@@ -84,12 +86,26 @@ func Run() {
case "": case "":
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
default: default:
if logRollMB > 0 {
log.SetOutput(&lumberjack.Logger{ log.SetOutput(&lumberjack.Logger{
Filename: logfile, Filename: logfile,
MaxSize: 100, MaxSize: logRollMB,
MaxAge: 14, MaxAge: 14,
MaxBackups: 10, MaxBackups: 10,
Compress: logRollCompress,
}) })
} else {
err := os.MkdirAll(filepath.Dir(logfile), 0755)
if err != nil {
mustLogFatalf("%v", err)
}
f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
mustLogFatalf("%v", err)
}
// don't close file; log should be writeable for duration of process
log.SetOutput(f)
}
} }
//Load all additional envs as soon as possible //Load all additional envs as soon as possible
...@@ -501,6 +517,8 @@ var ( ...@@ -501,6 +517,8 @@ var (
cpu string cpu string
envFile string envFile string
logfile string logfile string
logRollMB int
logRollCompress bool
revoke string revoke string
version bool version bool
plugins bool plugins bool
......
...@@ -23,7 +23,7 @@ import ( ...@@ -23,7 +23,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/hashicorp/go-syslog" gsyslog "github.com/hashicorp/go-syslog"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
...@@ -162,7 +162,7 @@ selectwriter: ...@@ -162,7 +162,7 @@ selectwriter:
return err return err
} }
if l.Roller != nil { if l.Roller != nil && !l.Roller.Disabled {
file.Close() file.Close()
l.Roller.Filename = l.Output l.Roller.Filename = l.Output
l.writer = l.Roller.GetLogWriter() l.writer = l.Roller.GetLogWriter()
......
...@@ -20,11 +20,12 @@ import ( ...@@ -20,11 +20,12 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"gopkg.in/natefinch/lumberjack.v2" lumberjack "gopkg.in/natefinch/lumberjack.v2"
) )
// LogRoller implements a type that provides a rolling logger. // LogRoller implements a type that provides a rolling logger.
type LogRoller struct { type LogRoller struct {
Disabled bool
Filename string Filename string
MaxSize int MaxSize int
MaxAge int MaxAge int
...@@ -66,10 +67,11 @@ func IsLogRollerSubdirective(subdir string) bool { ...@@ -66,10 +67,11 @@ func IsLogRollerSubdirective(subdir string) bool {
return subdir == directiveRotateSize || return subdir == directiveRotateSize ||
subdir == directiveRotateAge || subdir == directiveRotateAge ||
subdir == directiveRotateKeep || subdir == directiveRotateKeep ||
subdir == directiveRotateCompress subdir == directiveRotateCompress ||
subdir == directiveRotateDisable
} }
var invalidRollerParameterErr = errors.New("invalid roller parameter") var errInvalidRollParameter = errors.New("invalid roller parameter")
// ParseRoller parses roller contents out of c. // ParseRoller parses roller contents out of c.
func ParseRoller(l *LogRoller, what string, where ...string) error { func ParseRoller(l *LogRoller, what string, where ...string) error {
...@@ -79,16 +81,16 @@ func ParseRoller(l *LogRoller, what string, where ...string) error { ...@@ -79,16 +81,16 @@ func ParseRoller(l *LogRoller, what string, where ...string) error {
// rotate_compress doesn't accept any parameters. // rotate_compress doesn't accept any parameters.
// others only accept one parameter // others only accept one parameter
if (what == directiveRotateCompress && len(where) != 0) || if ((what == directiveRotateCompress || what == directiveRotateDisable) && len(where) != 0) ||
(what != directiveRotateCompress && len(where) != 1) { ((what != directiveRotateCompress && what != directiveRotateDisable) && len(where) != 1) {
return invalidRollerParameterErr return errInvalidRollParameter
} }
var ( var (
value int value int
err error err error
) )
if what != directiveRotateCompress { if what != directiveRotateCompress && what != directiveRotateDisable {
value, err = strconv.Atoi(where[0]) value, err = strconv.Atoi(where[0])
if err != nil { if err != nil {
return err return err
...@@ -96,6 +98,8 @@ func ParseRoller(l *LogRoller, what string, where ...string) error { ...@@ -96,6 +98,8 @@ func ParseRoller(l *LogRoller, what string, where ...string) error {
} }
switch what { switch what {
case directiveRotateDisable:
l.Disabled = true
case directiveRotateSize: case directiveRotateSize:
l.MaxSize = value l.MaxSize = value
case directiveRotateAge: case directiveRotateAge:
...@@ -127,6 +131,7 @@ const ( ...@@ -127,6 +131,7 @@ const (
// defaultRotateKeep is 10 files. // defaultRotateKeep is 10 files.
defaultRotateKeep = 10 defaultRotateKeep = 10
directiveRotateDisable = "rotate_disable"
directiveRotateSize = "rotate_size" directiveRotateSize = "rotate_size"
directiveRotateAge = "rotate_age" directiveRotateAge = "rotate_age"
directiveRotateKeep = "rotate_keep" directiveRotateKeep = "rotate_keep"
...@@ -134,5 +139,7 @@ const ( ...@@ -134,5 +139,7 @@ const (
) )
// lumberjacks maps log filenames to the logger // lumberjacks maps log filenames to the logger
// that is being used to keep them rolled/maintained. // that is being used to keep them rolled/maintained;
var lumberjacks = make(map[string]*lumberjack.Logger) // if rolling is disabled, it's just a regular
// *os.File, not a lumberjack
var lumberjacks = make(map[string]io.Writer)
...@@ -104,7 +104,7 @@ type Logger struct { ...@@ -104,7 +104,7 @@ type Logger struct {
LocalTime bool `json:"localtime" yaml:"localtime"` LocalTime bool `json:"localtime" yaml:"localtime"`
// Compress determines if the rotated log files should be compressed // Compress determines if the rotated log files should be compressed
// using gzip. // using gzip. The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"` Compress bool `json:"compress" yaml:"compress"`
size int64 size int64
......
...@@ -733,7 +733,7 @@ ...@@ -733,7 +733,7 @@
"importpath": "gopkg.in/natefinch/lumberjack.v2", "importpath": "gopkg.in/natefinch/lumberjack.v2",
"repository": "https://gopkg.in/natefinch/lumberjack.v2", "repository": "https://gopkg.in/natefinch/lumberjack.v2",
"vcs": "git", "vcs": "git",
"revision": "df99d62fd42d8b3752c8a42c6723555372c02a03", "revision": "7d6a1875575e09256dc552b4c0e450dcd02bd10e",
"branch": "v2.0", "branch": "v2.0",
"notests": true "notests": true
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment