libct/logs: remove ConfigureLogging

Previous commits removed all its users -- the only one left is package's
own unit tests.

Modify those unit tests to configure logrus directly, and remove
ConfigureLogging for good. The world is better without it.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-07-25 20:45:42 -07:00
parent f77fb7a3ee
commit 8d8415ee46
2 changed files with 13 additions and 77 deletions
-55
View File
@@ -3,30 +3,11 @@ package logs
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sync"
"github.com/sirupsen/logrus"
)
var (
configureMutex sync.Mutex
// loggingConfigured will be set once logging has been configured via invoking `ConfigureLogging`.
// Subsequent invocations of `ConfigureLogging` would be no-op
loggingConfigured = false
)
type Config struct {
LogLevel logrus.Level
LogFormat string
LogFilePath string
LogPipeFd int
LogCaller bool
}
func ForwardLogs(logPipe io.ReadCloser) chan error {
done := make(chan error, 1)
s := bufio.NewScanner(logPipe)
@@ -68,39 +49,3 @@ func processEntry(text []byte) {
}
logrus.StandardLogger().Logf(lvl, jl.Msg)
}
func ConfigureLogging(config Config) error {
configureMutex.Lock()
defer configureMutex.Unlock()
if loggingConfigured {
return errors.New("logging has already been configured")
}
logrus.SetLevel(config.LogLevel)
logrus.SetReportCaller(config.LogCaller)
// XXX: while 0 is a valid fd (usually stdin), here we assume
// that we never deliberately set LogPipeFd to 0.
if config.LogPipeFd > 0 {
logrus.SetOutput(os.NewFile(uintptr(config.LogPipeFd), "logpipe"))
} else if config.LogFilePath != "" {
f, err := os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0o644)
if err != nil {
return err
}
logrus.SetOutput(f)
}
switch config.LogFormat {
case "text":
// retain logrus's default.
case "json":
logrus.SetFormatter(new(logrus.JSONFormatter))
default:
return fmt.Errorf("unknown log-format %q", config.LogFormat)
}
loggingConfigured = true
return nil
}
+13 -22
View File
@@ -71,7 +71,7 @@ func logToLogWriter(t *testing.T, l *log, message string) {
type log struct {
w io.WriteCloser
file string
file *os.File
done chan error
}
@@ -90,19 +90,16 @@ func runLogForwarding(t *testing.T) *log {
if err != nil {
t.Fatal(err)
}
tempFile.Close()
logFile := tempFile.Name()
t.Cleanup(func() { os.Remove(logFile) })
logConfig := Config{LogLevel: logrus.InfoLevel, LogFormat: "json", LogFilePath: logFile}
loggingConfigured = false
if err := ConfigureLogging(logConfig); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
tempFile.Close()
os.Remove(tempFile.Name())
})
logrus.SetOutput(tempFile)
logrus.SetFormatter(&logrus.JSONFormatter{})
doneForwarding := ForwardLogs(logR)
return &log{w: logW, done: doneForwarding, file: logFile}
return &log{w: logW, done: doneForwarding, file: tempFile}
}
func finish(t *testing.T, l *log) {
@@ -113,16 +110,10 @@ func finish(t *testing.T, l *log) {
}
}
func truncateLogFile(t *testing.T, logFile string) {
func truncateLogFile(t *testing.T, file *os.File) {
t.Helper()
file, err := os.OpenFile(logFile, os.O_RDWR, 0o600)
if err != nil {
t.Fatalf("failed to open log file: %v", err)
return
}
defer file.Close()
err = file.Truncate(0)
err := file.Truncate(0)
if err != nil {
t.Fatalf("failed to truncate log file: %v", err)
}
@@ -131,7 +122,7 @@ func truncateLogFile(t *testing.T, logFile string) {
// check checks that file contains txt
func check(t *testing.T, l *log, txt string) {
t.Helper()
contents, err := ioutil.ReadFile(l.file)
contents, err := ioutil.ReadFile(l.file.Name())
if err != nil {
t.Fatal(err)
}
@@ -149,7 +140,7 @@ func checkWait(t *testing.T, l *log, txt string) {
iter = 3
)
for i := 0; ; i++ {
st, err := os.Stat(l.file)
st, err := l.file.Stat()
if err != nil {
t.Fatal(err)
}
@@ -157,7 +148,7 @@ func checkWait(t *testing.T, l *log, txt string) {
break
}
if i == iter {
t.Fatalf("waited %s for file %s to be non-empty but it still is", iter*delay, l.file)
t.Fatalf("waited %s for file %s to be non-empty but it still is", iter*delay, l.file.Name())
}
time.Sleep(delay)
}