Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: logging to file & additional minor #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Dockerfile.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.13-alpine AS builder
LABEL maintainer="[email protected]"

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
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
64 changes: 59 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,75 @@ 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":
log.SetLevel(log.InfoLevel)
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 {
Expand Down
60 changes: 45 additions & 15 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
}
Expand Down