diff options
author | Stan Hu <stanhu@gmail.com> | 2020-03-07 00:46:26 -0800 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2020-03-10 00:41:24 -0700 |
commit | 488102039cb5e79114954ad91663ce28c99153c8 (patch) | |
tree | 808256b23022449477aa8e17ad3865ba6a3213b6 /internal/logger/logger.go | |
parent | b920520599142435ce06ad155099544adc923618 (diff) | |
download | gitlab-shell-sh-log-http-requests.tar.gz |
Log internal HTTP requestssh-log-http-requests
This restores the previous behavior of logging the success and failures
of internal HTTP requests.
Part of https://gitlab.com/gitlab-org/gitlab/issues/207916
Diffstat (limited to 'internal/logger/logger.go')
-rw-r--r-- | internal/logger/logger.go | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/internal/logger/logger.go b/internal/logger/logger.go index f1db4b0..f9cf734 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -3,10 +3,13 @@ package logger import ( "fmt" "io" + "io/ioutil" golog "log" "log/syslog" + "math" "os" "sync" + "time" "gitlab.com/gitlab-org/gitlab-shell/internal/config" @@ -26,13 +29,19 @@ func Configure(cfg *config.Config) error { defer mutex.Unlock() pid = os.Getpid() + ProgName, _ = os.Executable() - var err error - logWriter, err = os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_APPEND, 0) + // Avoid leaking output if we can't set up the logging output + log.SetOutput(ioutil.Discard) + + output, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) if err != nil { + setupBootstrapLogger() + logPrint("Unable to configure logging", err) return err } + logWriter = output log.SetOutput(logWriter) if cfg.LogFormat == "json" { log.SetFormatter(&log.JSONFormatter{}) @@ -41,12 +50,14 @@ func Configure(cfg *config.Config) error { return nil } +// If our log file is not available we want to log somewhere else, but +// not to standard error because that leaks information to the user. This +// function attempts to log to syslog. func logPrint(msg string, err error) { - mutex.Lock() - defer mutex.Unlock() - if logWriter == nil { - bootstrapLogPrint(msg, err) + if bootstrapLogger != nil { + bootstrapLogger.Print(ProgName+":", msg+":", err) + } return } @@ -56,6 +67,10 @@ func logPrint(msg string, err error) { } func Fatal(msg string, err error) { + mutex.Lock() + defer mutex.Unlock() + setupBootstrapLogger() + logPrint(msg, err) // We don't show the error to the end user because it can leak // information that is private to the GitLab server. @@ -63,20 +78,23 @@ func Fatal(msg string, err error) { os.Exit(1) } -// If our log file is not available we want to log somewhere else, but -// not to standard error because that leaks information to the user. This -// function attempts to log to syslog. -// // We assume the logging mutex is already locked. -func bootstrapLogPrint(msg string, err error) { +func setupBootstrapLogger() { if bootstrapLogger == nil { - var err error - bootstrapLogger, err = syslog.NewLogger(syslog.LOG_ERR|syslog.LOG_USER, 0) - if err != nil { - // The message will not be logged. - return - } + bootstrapLogger, _ = syslog.NewLogger(syslog.LOG_ERR|syslog.LOG_USER, 0) } +} + +func ElapsedTime(since time.Time) float64 { + // Later versions of Go support Milliseconds directly: + // https://go-review.googlesource.com/c/go/+/167387/ + return roundFloat(time.Since(since).Seconds() * 1e3) +} + +func roundFloat(x float64) float64 { + return round(x, 0.001) +} - bootstrapLogger.Print(ProgName+":", msg+":", err) +func round(x, unit float64) float64 { + return math.Round(x/unit) * unit } |