diff --git a/pkg/log/logging.go b/pkg/log/logging.go deleted file mode 100644 index c2184ff..0000000 --- a/pkg/log/logging.go +++ /dev/null @@ -1,162 +0,0 @@ -package log - -import ( - "fmt" - golog "log" - "os" - "strings" - "sync" -) - -// Level is used to indicate the severity of a log entry -type Level int - -const ( - // ERROR indicates a significant problem was encountered - ERROR Level = iota - // WARN indicates something that may be a problem - WARN - // INFO indicates a purely informational log entry - INFO - // TRACE entries are meant for development purposes only - TRACE -) - -var ( - // MaxLevel is the highest Level we will log (max TRACE, min ERROR) - MaxLevel = TRACE - - // logfname is the name of the logfile - logfname string - - // logf is the file we send log output to, will be nil for stderr or stdout - logf *os.File - - mu sync.RWMutex -) - -// Initialize logging. If logfile is equal to "stderr" or "stdout", then -// we will log to that output stream. Otherwise the specificed file will -// opened for writing, and all log data will be placed in it. -func Initialize(logfile string) error { - mu.Lock() - defer mu.Unlock() - if logfile != "stderr" { - // stderr is the go logging default - if logfile == "stdout" { - // set to stdout - golog.SetOutput(os.Stdout) - } else { - logfname = logfile - if err := openLogFile(); err != nil { - return err - } - // Platform specific - closeStdin() - } - } - return nil -} - -// SetLogLevel sets MaxLevel based on the provided string -func SetLogLevel(level string) (ok bool) { - mu.Lock() - defer mu.Unlock() - switch strings.ToUpper(level) { - case "ERROR": - MaxLevel = ERROR - case "WARN": - MaxLevel = WARN - case "INFO": - MaxLevel = INFO - case "TRACE": - MaxLevel = TRACE - default: - golog.Print("Error, unknown log level requested: " + level) - return false - } - return true -} - -// Errorf logs a message to the 'standard' Logger (always), accepts format strings -func Errorf(msg string, args ...interface{}) { - mu.RLock() - defer mu.RUnlock() - msg = "[ERROR] " + msg - golog.Printf(msg, args...) -} - -// Warnf logs a message to the 'standard' Logger if MaxLevel is >= WARN, accepts format strings -func Warnf(msg string, args ...interface{}) { - mu.RLock() - defer mu.RUnlock() - if MaxLevel >= WARN { - msg = "[WARN ] " + msg - golog.Printf(msg, args...) - } -} - -// Infof logs a message to the 'standard' Logger if MaxLevel is >= INFO, accepts format strings -func Infof(msg string, args ...interface{}) { - mu.RLock() - defer mu.RUnlock() - if MaxLevel >= INFO { - msg = "[INFO ] " + msg - golog.Printf(msg, args...) - } -} - -// Tracef logs a message to the 'standard' Logger if MaxLevel is >= TRACE, accepts format strings -func Tracef(msg string, args ...interface{}) { - mu.RLock() - defer mu.RUnlock() - if MaxLevel >= TRACE { - msg = "[TRACE] " + msg - golog.Printf(msg, args...) - } -} - -// Rotate closes the current log file, then reopens it. This gives an external -// log rotation system the opportunity to move the existing log file out of the -// way and have Inbucket create a new one. -func Rotate() { - mu.Lock() - defer mu.Unlock() - // Rotate logs if configured - if logf != nil { - closeLogFile() - // There is nothing we can do if the log open fails - _ = openLogFile() - } else { - Infof("Ignoring SIGHUP, logfile not configured") - } -} - -// Close the log file if we have one open -func Close() { - mu.Lock() - defer mu.Unlock() - if logf != nil { - closeLogFile() - } -} - -// openLogFile creates or appends to the logfile passed on commandline -func openLogFile() error { - // use specified log file - var err error - logf, err = os.OpenFile(logfname, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) - if err != nil { - return fmt.Errorf("failed to create %v: %v", logfname, err) - } - golog.SetOutput(logf) - // Platform specific - reassignStdout() - return nil -} - -// closeLogFile closes the current logfile -func closeLogFile() { - // We are never in a situation where we can do anything about failing to close - _ = logf.Close() -} diff --git a/pkg/log/stdout_unix.go b/pkg/log/stdout_unix.go deleted file mode 100644 index 0908102..0000000 --- a/pkg/log/stdout_unix.go +++ /dev/null @@ -1,32 +0,0 @@ -// +build !windows - -package log - -import ( - "log" - "os" - - "golang.org/x/sys/unix" -) - -// closeStdin will close stdin on Unix platforms - this is standard practice -// for daemons -func closeStdin() { - if err := os.Stdin.Close(); err != nil { - // Not a fatal error - log.Printf("Failed to close os.Stdin during log setup") - } -} - -// reassignStdout points stdout/stderr to our logfile on systems that support -// the Dup2 syscall per https://github.com/golang/go/issues/325 -func reassignStdout() { - if err := unix.Dup2(int(logf.Fd()), 1); err != nil { - // Not considered fatal - log.Printf("Failed to re-assign stdout to logfile: %v", err) - } - if err := unix.Dup2(int(logf.Fd()), 2); err != nil { - // Not considered fatal - log.Printf("Failed to re-assign stderr to logfile: %v", err) - } -} diff --git a/pkg/log/stdout_windows.go b/pkg/log/stdout_windows.go deleted file mode 100644 index f883d24..0000000 --- a/pkg/log/stdout_windows.go +++ /dev/null @@ -1,37 +0,0 @@ -// +build windows - -package log - -import ( - "log" - "os" -) - -var stdOutsClosed = false - -// closeStdin does nothing on Windows, it would always fail -func closeStdin() { - // Nop -} - -// reassignStdout points stdout/stderr to our logfile on systems that do not -// support the Dup2 syscall -func reassignStdout() { - if !stdOutsClosed { - // Close std* streams to prevent accidental output, they will be redirected to - // our logfile below - - // Warning: this will hide panic() output, sorry Windows users - if err := os.Stderr.Close(); err != nil { - // Not considered fatal - log.Printf("Failed to close os.Stderr during log setup") - } - if err := os.Stdin.Close(); err != nil { - // Not considered fatal - log.Printf("Failed to close os.Stdin during log setup") - } - os.Stdout = logf - os.Stderr = logf - stdOutsClosed = true - } -}