From cc63eca0c88da9de606aab8fda044e588222c8c5 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Wed, 6 Nov 2019 21:06:34 +0100 Subject: [PATCH] Add flag to disable timestamps for process log (#2781) * Add flag to enable/disable timstamps for process log. solves #2615 * Remove timestamp argument from log.Printf() * Add log-timestamps=false to systemd unit * Copy log flags * Fix argument list --- caddy/caddymain/run.go | 7 +++++++ caddyhttp/errors/errors.go | 13 ++++--------- caddyhttp/httpserver/logger.go | 2 +- dist/init/linux-systemd/caddy.service | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/caddy/caddymain/run.go b/caddy/caddymain/run.go index 33151003c..163cbc848 100644 --- a/caddy/caddymain/run.go +++ b/caddy/caddymain/run.go @@ -60,6 +60,7 @@ func init() { flag.StringVar(&certmagic.Default.Email, "email", "", "Default ACME CA account email address") flag.DurationVar(&certmagic.HTTPTimeout, "catimeout", certmagic.HTTPTimeout, "Default ACME CA HTTP timeout") flag.StringVar(&logfile, "log", "", "Process log file") + flag.BoolVar(&logTimestamps, "log-timestamps", true, "Enable timestamps for the process log") 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") @@ -86,6 +87,11 @@ func Run() { caddy.OnProcessExit = append(caddy.OnProcessExit, certmagic.CleanUpOwnLocks) certmagic.UserAgent = appName + "/" + cleanModVersion + if !logTimestamps { + // Disable timestamps for logging + log.SetFlags(0) + } + // Set up process log before anything bad happens switch logfile { case "stdout": @@ -590,6 +596,7 @@ var ( envFile string fromJSON bool logfile string + logTimestamps bool logRollMB int logRollCompress bool revoke string diff --git a/caddyhttp/errors/errors.go b/caddyhttp/errors/errors.go index 5e10b2d04..568bab87b 100644 --- a/caddyhttp/errors/errors.go +++ b/caddyhttp/errors/errors.go @@ -22,7 +22,6 @@ import ( "os" "runtime" "strings" - "time" "github.com/caddyserver/caddy" "github.com/caddyserver/caddy/caddyhttp/httpserver" @@ -50,7 +49,7 @@ func (h ErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er status, err := h.Next.ServeHTTP(w, r) if err != nil { - errMsg := fmt.Sprintf("%s [ERROR %d %s] %v", time.Now().Format(timeFormat), status, r.URL.Path, err) + errMsg := fmt.Sprintf("[ERROR %d %s] %v", status, r.URL.Path, err) if h.Debug { // Write error to response instead of to log w.Header().Set("Content-Type", "text/plain; charset=utf-8") @@ -79,8 +78,7 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, r *http.Request, code int errorPage, err := os.Open(pagePath) if err != nil { // An additional error handling an error... - h.Log.Printf("%s [NOTICE %d %s] could not load error page: %v", - time.Now().Format(timeFormat), code, r.URL.String(), err) + h.Log.Printf("[NOTICE %d %s] could not load error page: %v", code, r.URL.String(), err) httpserver.DefaultErrorFunc(w, r, code) return } @@ -93,8 +91,7 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, r *http.Request, code int if err != nil { // Epic fail... sigh. - h.Log.Printf("%s [NOTICE %d %s] could not respond with %s: %v", - time.Now().Format(timeFormat), code, r.URL.String(), pagePath, err) + h.Log.Printf("[NOTICE %d %s] could not respond with %s: %v", code, r.URL.String(), pagePath, err) httpserver.DefaultErrorFunc(w, r, code) } @@ -148,7 +145,7 @@ func (h ErrorHandler) recovery(w http.ResponseWriter, r *http.Request) { file = file[pkgPathPos+len(delim):] } - panicMsg := fmt.Sprintf("%s [PANIC %s] %s:%d - %v", time.Now().Format(timeFormat), r.URL.String(), file, line, rec) + panicMsg := fmt.Sprintf("[PANIC %s] %s:%d - %v", r.URL.String(), file, line, rec) if h.Debug { // Write error and stack trace to the response rather than to a log var stackBuf [4096]byte @@ -160,5 +157,3 @@ func (h ErrorHandler) recovery(w http.ResponseWriter, r *http.Request) { h.errorPage(w, r, http.StatusInternalServerError) } } - -const timeFormat = "02/Jan/2006:15:04:05 -0700" diff --git a/caddyhttp/httpserver/logger.go b/caddyhttp/httpserver/logger.go index e5c9ac027..577464adc 100644 --- a/caddyhttp/httpserver/logger.go +++ b/caddyhttp/httpserver/logger.go @@ -171,7 +171,7 @@ selectwriter: } } - l.Logger = log.New(l.writer, "", 0) + l.Logger = log.New(l.writer, "", log.Flags()) return nil diff --git a/dist/init/linux-systemd/caddy.service b/dist/init/linux-systemd/caddy.service index 07cfeb6ff..d636565e6 100644 --- a/dist/init/linux-systemd/caddy.service +++ b/dist/init/linux-systemd/caddy.service @@ -20,7 +20,7 @@ Group=www-data Environment=CADDYPATH=/etc/ssl/caddy ; Always set "-root" to something safe in case it gets forgotten in the Caddyfile. -ExecStart=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp +ExecStart=/usr/local/bin/caddy -log stdout -log-timestamps=false -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp ExecReload=/bin/kill -USR1 $MAINPID ; Use graceful shutdown with a reasonable timeout