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
This commit is contained in:
Julian Kornberger 2019-11-06 21:06:34 +01:00 committed by Matt Holt
parent aa94f2b802
commit cc63eca0c8
4 changed files with 13 additions and 11 deletions

View File

@ -60,6 +60,7 @@ func init() {
flag.StringVar(&certmagic.Default.Email, "email", "", "Default ACME CA account email address") 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.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.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.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.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")
@ -86,6 +87,11 @@ func Run() {
caddy.OnProcessExit = append(caddy.OnProcessExit, certmagic.CleanUpOwnLocks) caddy.OnProcessExit = append(caddy.OnProcessExit, certmagic.CleanUpOwnLocks)
certmagic.UserAgent = appName + "/" + cleanModVersion certmagic.UserAgent = appName + "/" + cleanModVersion
if !logTimestamps {
// Disable timestamps for logging
log.SetFlags(0)
}
// Set up process log before anything bad happens // Set up process log before anything bad happens
switch logfile { switch logfile {
case "stdout": case "stdout":
@ -590,6 +596,7 @@ var (
envFile string envFile string
fromJSON bool fromJSON bool
logfile string logfile string
logTimestamps bool
logRollMB int logRollMB int
logRollCompress bool logRollCompress bool
revoke string revoke string

View File

@ -22,7 +22,6 @@ import (
"os" "os"
"runtime" "runtime"
"strings" "strings"
"time"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver" "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) status, err := h.Next.ServeHTTP(w, r)
if err != nil { 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 { if h.Debug {
// Write error to response instead of to log // Write error to response instead of to log
w.Header().Set("Content-Type", "text/plain; charset=utf-8") 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) errorPage, err := os.Open(pagePath)
if err != nil { if err != nil {
// An additional error handling an error... <insert grumpy cat here> // An additional error handling an error... <insert grumpy cat here>
h.Log.Printf("%s [NOTICE %d %s] could not load error page: %v", h.Log.Printf("[NOTICE %d %s] could not load error page: %v", code, r.URL.String(), err)
time.Now().Format(timeFormat), code, r.URL.String(), err)
httpserver.DefaultErrorFunc(w, r, code) httpserver.DefaultErrorFunc(w, r, code)
return return
} }
@ -93,8 +91,7 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, r *http.Request, code int
if err != nil { if err != nil {
// Epic fail... sigh. // Epic fail... sigh.
h.Log.Printf("%s [NOTICE %d %s] could not respond with %s: %v", h.Log.Printf("[NOTICE %d %s] could not respond with %s: %v", code, r.URL.String(), pagePath, err)
time.Now().Format(timeFormat), code, r.URL.String(), pagePath, err)
httpserver.DefaultErrorFunc(w, r, code) httpserver.DefaultErrorFunc(w, r, code)
} }
@ -148,7 +145,7 @@ func (h ErrorHandler) recovery(w http.ResponseWriter, r *http.Request) {
file = file[pkgPathPos+len(delim):] 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 { if h.Debug {
// Write error and stack trace to the response rather than to a log // Write error and stack trace to the response rather than to a log
var stackBuf [4096]byte var stackBuf [4096]byte
@ -160,5 +157,3 @@ func (h ErrorHandler) recovery(w http.ResponseWriter, r *http.Request) {
h.errorPage(w, r, http.StatusInternalServerError) h.errorPage(w, r, http.StatusInternalServerError)
} }
} }
const timeFormat = "02/Jan/2006:15:04:05 -0700"

View File

@ -171,7 +171,7 @@ selectwriter:
} }
} }
l.Logger = log.New(l.writer, "", 0) l.Logger = log.New(l.writer, "", log.Flags())
return nil return nil

View File

@ -20,7 +20,7 @@ Group=www-data
Environment=CADDYPATH=/etc/ssl/caddy Environment=CADDYPATH=/etc/ssl/caddy
; Always set "-root" to something safe in case it gets forgotten in the Caddyfile. ; 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 ExecReload=/bin/kill -USR1 $MAINPID
; Use graceful shutdown with a reasonable timeout ; Use graceful shutdown with a reasonable timeout