From 164bef63cc8996e64d6631e6ff984e0854c5d9fb Mon Sep 17 00:00:00 2001 From: zufardhiyaulhaq Date: Sat, 5 Dec 2020 01:13:12 +0100 Subject: [PATCH] add logging to file feature --- Dockerfile.development | 23 +++++++++++++++ main.go | 8 ++++-- util.go | 64 ++++++++++++++++++++++++++++++++++++++---- util_test.go | 60 +++++++++++++++++++++++++++++---------- 4 files changed, 132 insertions(+), 23 deletions(-) create mode 100644 Dockerfile.development diff --git a/Dockerfile.development b/Dockerfile.development new file mode 100644 index 0000000..9133840 --- /dev/null +++ b/Dockerfile.development @@ -0,0 +1,23 @@ +FROM golang:1.13-alpine AS builder +LABEL maintainer="joona@kuori.org" + +RUN apk add --update gcc musl-dev git + +ENV GOPATH /tmp/buildcache +COPY . /tmp/acme-dns +WORKDIR /tmp/acme-dns +RUN CGO_ENABLED=1 go build + +FROM alpine:latest + +WORKDIR /root/ +COPY --from=builder /tmp/acme-dns . +RUN mkdir -p /etc/acme-dns +RUN mkdir -p /var/lib/acme-dns +RUN rm -rf ./config.cfg +RUN apk --no-cache add ca-certificates && update-ca-certificates + +VOLUME ["/etc/acme-dns", "/var/lib/acme-dns"] +ENTRYPOINT ["./acme-dns"] +EXPOSE 53 80 443 +EXPOSE 53/udp diff --git a/main.go b/main.go index e9ca6e2..8e52aff 100644 --- a/main.go +++ b/main.go @@ -26,12 +26,13 @@ func main() { flag.Parse() // Read global config var err error + var readConfigLog string if fileIsAccessible(*configPtr) { - log.WithFields(log.Fields{"file": *configPtr}).Info("Using config file") Config, err = readConfig(*configPtr) + readConfigLog = *configPtr } else if fileIsAccessible("./config.cfg") { - log.WithFields(log.Fields{"file": "./config.cfg"}).Info("Using config file") Config, err = readConfig("./config.cfg") + readConfigLog = "./config.cfg" } else { log.Errorf("Configuration file not found.") os.Exit(1) @@ -41,7 +42,7 @@ func main() { os.Exit(1) } - setupLogging(Config.Logconfig.Format, Config.Logconfig.Level) + setupLogging(Config.Logconfig, readConfigLog) // Open database newDB := new(acmedb) @@ -104,6 +105,7 @@ func main() { func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer) { // Setup http logger logger := log.New() + setupHTTPLogging(logger, config.Logconfig) logwriter := logger.Writer() defer logwriter.Close() // Setup logging for different dependencies to log with logrus diff --git a/util.go b/util.go index c835a0d..6faf740 100644 --- a/util.go +++ b/util.go @@ -90,13 +90,23 @@ func sanitizeDomainQuestion(d string) string { return dom } -func setupLogging(format string, level string) { - if format == "json" { - log.SetFormatter(&log.JSONFormatter{}) +func setupLogging(logconfig logconfig, readConfigLog string) { + switch logconfig.Logtype { + default: + log.SetOutput(os.Stdout) + logconfig.Logtype = "stdout" + case "file": + file, err := os.OpenFile(logconfig.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err == nil { + log.SetOutput(file) + } else { + log.SetOutput(os.Stdout) + } } - switch level { + switch logconfig.Level { default: log.SetLevel(log.WarnLevel) + logconfig.Level = "warning" case "debug": log.SetLevel(log.DebugLevel) case "info": @@ -104,7 +114,51 @@ func setupLogging(format string, level string) { case "error": log.SetLevel(log.ErrorLevel) } - // TODO: file logging + if logconfig.Format == "json" { + log.SetFormatter(&log.JSONFormatter{}) + } else { + logconfig.Format = "text" + } + log.WithFields(log.Fields{"file": readConfigLog}).Info("Using config file") + log.WithFields(log.Fields{"type": logconfig.Logtype}).Info("Set log type") + log.WithFields(log.Fields{"level": logconfig.Level}).Info("Set log level") + log.WithFields(log.Fields{"format": logconfig.Format}).Info("Set log format") + if logconfig.Logtype == "file" { + log.WithFields(log.Fields{"file": logconfig.File}).Info("Set log file") + } +} + +func setupHTTPLogging(logger *log.Logger, logconfig logconfig) { + switch logconfig.Logtype { + default: + logger.SetOutput(os.Stdout) + case "file": + file, err := os.OpenFile(logconfig.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err == nil { + logger.SetOutput(file) + } else { + logger.SetOutput(os.Stdout) + } + } + switch logconfig.Level { + default: + logger.SetLevel(log.WarnLevel) + case "debug": + logger.SetLevel(log.DebugLevel) + case "info": + logger.SetLevel(log.InfoLevel) + case "error": + logger.SetLevel(log.ErrorLevel) + } + if logconfig.Format == "json" { + logger.SetFormatter(&log.JSONFormatter{}) + } + logger.WithFields(log.Fields{"type": logconfig.Logtype}).Info("HTTP logger set log type") + logger.WithFields(log.Fields{"level": logconfig.Level}).Info("HTTP logger Set log level") + logger.WithFields(log.Fields{"format": logconfig.Format}).Info("HTTP logger Set log format") + if logconfig.Logtype == "file" { + logger.WithFields(log.Fields{"file": logconfig.File}).Info("HTTP logger Set log file") + } } func getIPListFromHeader(header string) []string { diff --git a/util_test.go b/util_test.go index 4aecf00..cc4c523 100644 --- a/util_test.go +++ b/util_test.go @@ -9,21 +9,51 @@ import ( log "github.com/sirupsen/logrus" ) -func TestSetupLogging(t *testing.T) { - for i, test := range []struct { - format string - level string - expected string - }{ - {"text", "warning", "warning"}, - {"json", "debug", "debug"}, - {"text", "info", "info"}, - {"json", "error", "error"}, - {"text", "something", "warning"}, - } { - setupLogging(test.format, test.level) - if log.GetLevel().String() != test.expected { - t.Errorf("Test %d: Expected loglevel %s but got %s", i, test.expected, log.GetLevel().String()) +func TestLevelSetupLogging(t *testing.T) { + var configs = []map[logconfig]string{ + {{Format: "text", Level: "warning", Logtype: "stdout"}: "warning"}, + {{Format: "text", Level: "info", Logtype: "stdout"}: "info"}, + {{Format: "text", Level: "something", Logtype: "stdout"}: "warning"}, + {{Format: "json", Level: "debug", Logtype: "stdout"}: "debug"}, + {{Format: "json", Level: "error", Logtype: "stdout"}: "error"}, + {{Format: "text", Level: "warning", Logtype: "file"}: "warning"}, + {{Format: "text", Level: "info", Logtype: "file"}: "info"}, + {{Format: "text", Level: "something", Logtype: "file"}: "warning"}, + {{Format: "json", Level: "debug", Logtype: "file"}: "debug"}, + {{Format: "json", Level: "error", Logtype: "file"}: "error"}, + } + for i, config := range configs { + for logconfig, expected := range config { + setupLogging(logconfig, "using config") + if log.GetLevel().String() != expected { + t.Error(logconfig) + t.Errorf("Test %d: Expected loglevel %s but got %s", i, expected, log.GetLevel().String()) + } + } + } +} + +func TestLevelHTTPSetupLogging(t *testing.T) { + var configs = []map[logconfig]string{ + {{Format: "text", Level: "warning", Logtype: "stdout"}: "warning"}, + {{Format: "text", Level: "info", Logtype: "stdout"}: "info"}, + {{Format: "text", Level: "something", Logtype: "stdout"}: "warning"}, + {{Format: "json", Level: "debug", Logtype: "stdout"}: "debug"}, + {{Format: "json", Level: "error", Logtype: "stdout"}: "error"}, + {{Format: "text", Level: "warning", Logtype: "file"}: "warning"}, + {{Format: "text", Level: "info", Logtype: "file"}: "info"}, + {{Format: "text", Level: "something", Logtype: "file"}: "warning"}, + {{Format: "json", Level: "debug", Logtype: "file"}: "debug"}, + {{Format: "json", Level: "error", Logtype: "file"}: "error"}, + } + for i, config := range configs { + for logconfig, expected := range config { + logger := log.New() + setupHTTPLogging(logger, logconfig) + if logger.GetLevel().String() != expected { + t.Error(logconfig) + t.Errorf("Test %d: Expected loglevel %s but got %s", i, expected, logger.GetLevel().String()) + } } } }