Skip to content

Commit

Permalink
Went logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
joohoi committed Nov 26, 2016
1 parent 3ae7dee commit 4269069
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 59 deletions.
10 changes: 5 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/kataras/iris"
)

Expand All @@ -20,7 +21,6 @@ func (a authMiddleware) Serve(ctx *iris.Context) {
if err := ctx.ReadJSON(&postData); err == nil {
// Check that the subdomain belongs to the user
if au.Subdomain == postData.Subdomain {
log.Debugf("Accepted authentication from [%s]", usernameStr)
ctx.Next()
return
}
Expand All @@ -44,12 +44,12 @@ func webRegisterPost(ctx *iris.Context) {
errstr := fmt.Sprintf("%v", err)
regJSON = iris.Map{"error": errstr}
regStatus = iris.StatusInternalServerError
log.Debugf("Error in registration, [%v]", err)
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration")
} else {
regJSON = iris.Map{"username": nu.Username, "password": nu.Password, "fulldomain": nu.Subdomain + "." + DNSConf.General.Domain, "subdomain": nu.Subdomain}
regStatus = iris.StatusCreated

log.Debugf("Successful registration, created user [%s]", nu.Username)
log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user")
}
ctx.JSON(regStatus, regJSON)
}
Expand All @@ -72,13 +72,13 @@ func webUpdatePost(ctx *iris.Context) {
if validSubdomain(a.Subdomain) && validTXT(a.Value) {
err := DB.Update(a)
if err != nil {
log.Warningf("Error trying to update [%v]", err)
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error while trying to update record")
webUpdatePostError(ctx, errors.New("internal error"), iris.StatusInternalServerError)
return
}
ctx.JSON(iris.StatusOK, iris.Map{"txt": a.Value})
} else {
log.Warningf("Bad data, subdomain: [%s], txt: [%s]", a.Subdomain, a.Value)
log.WithFields(log.Fields{"subdomain": a.Subdomain, "txt": a.Value}).Debug("Bad data for subdomain")
webUpdatePostError(ctx, errors.New("bad data"), iris.StatusBadRequest)
return
}
Expand Down
10 changes: 5 additions & 5 deletions config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ corsorigins = [
[logconfig]
# logging level: "error", "warning", "info" or "debug"
loglevel = "debug"
# possible values: stdout, file
# possible values: stdout, TODO file & integrations
logtype = "stdout"
# file path for logfile
logfile = "./acme-dns.log"
# format
logformat = "[%{time:2006/01/02 15:04:05.000}] - %{level: 5s} - %{message}"
# file path for logfile TODO
# logfile = "./acme-dns.log"
# format, either "json" or "text"
logformat = "text"
5 changes: 2 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"errors"
log "github.com/Sirupsen/logrus"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/satori/go.uuid"
Expand All @@ -28,7 +29,7 @@ var recordsTable = `
func getSQLiteStmt(s string) string {
re, err := regexp.Compile("\\$[0-9]")
if err != nil {
log.Errorf("%v", err)
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in regexp")
return s
}
return re.ReplaceAllString(s, "?")
Expand Down Expand Up @@ -122,7 +123,6 @@ func (d *database) GetByUsername(u uuid.UUID) (ACMETxt, error) {

func (d *database) GetByDomain(domain string) ([]ACMETxt, error) {
domain = sanitizeString(domain)
log.Debugf("Trying to select domain [%s] from table", domain)
var a []ACMETxt
getSQL := `
SELECT Username, Password, Subdomain, Value
Expand Down Expand Up @@ -157,7 +157,6 @@ func (d *database) GetByDomain(domain string) ([]ACMETxt, error) {

func (d *database) Update(a ACMETxt) error {
// Data in a is already sanitized
log.Debugf("Trying to update domain [%s] with TXT data [%s]", a.Subdomain, a.Value)
timenow := time.Now().Unix()
updSQL := `
UPDATE records SET Value=$1, LastActive=$2
Expand Down
14 changes: 8 additions & 6 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
"strings"
"time"
Expand All @@ -25,7 +26,7 @@ func answerTXT(q dns.Question) ([]dns.RR, int, error) {

atxt, err := DB.GetByDomain(sanitizeDomainQuestion(domain))
if err != nil {
log.Errorf("Error while trying to get record [%v]", err)
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error while trying to get record")
return ra, dns.RcodeNameError, err
}
for _, v := range atxt {
Expand All @@ -37,7 +38,8 @@ func answerTXT(q dns.Question) ([]dns.RR, int, error) {
rcode = dns.RcodeSuccess
}
}
log.Debugf("Answering TXT question for domain [%s]", domain)

log.WithFields(log.Fields{"domain": domain}).Info("Answering TXT question for domain")
return ra, rcode, nil
}

Expand All @@ -53,7 +55,7 @@ func answer(q dns.Question) ([]dns.RR, int, error) {
if !ok {
rcode = dns.RcodeNameError
}
log.Debugf("Answering [%s] question for domain [%s] with rcode [%s]", dns.TypeToString[rtype], domain, dns.RcodeToString[rcode])
log.WithFields(log.Fields{"qtype": dns.TypeToString[rtype], "domain": domain, "rcode": dns.RcodeToString[rcode]}).Debug("Answering question for domain")
return r, rcode, nil
}

Expand All @@ -74,7 +76,7 @@ func (r *Records) Parse(recs []string) {
for _, v := range recs {
rr, err := dns.NewRR(strings.ToLower(v))
if err != nil {
log.Errorf("Could not parse RR from config: [%v] for RR: [%s]", err, v)
log.WithFields(log.Fields{"error": err.Error(), "rr": v}).Warning("Could not parse RR from config")
continue
}
// Add parsed RR to the list
Expand All @@ -86,7 +88,7 @@ func (r *Records) Parse(recs []string) {
SOAstring := fmt.Sprintf("%s. SOA %s. %s. %s 28800 7200 604800 86400", strings.ToLower(DNSConf.General.Domain), strings.ToLower(DNSConf.General.Nsname), strings.ToLower(DNSConf.General.Nsadmin), serial)
soarr, err := dns.NewRR(SOAstring)
if err != nil {
log.Errorf("Error [%v] while trying to add SOA record: [%s]", err, SOAstring)
log.WithFields(log.Fields{"error": err.Error(), "soa": SOAstring}).Warning("Error while adding SOA record")
} else {
rrmap = appendRR(rrmap, soarr)
}
Expand All @@ -100,6 +102,6 @@ func appendRR(rrmap map[uint16]map[string][]dns.RR, rr dns.RR) map[uint16]map[st
rrmap[rr.Header().Rrtype] = newrr
}
rrmap[rr.Header().Rrtype][rr.Header().Name] = append(rrmap[rr.Header().Rrtype][rr.Header().Name], rr)
log.Debugf("Adding new record of type [%s] for domain [%s]", dns.TypeToString[rr.Header().Rrtype], rr.Header().Name)
log.WithFields(log.Fields{"recordtype": dns.TypeToString[rr.Header().Rrtype], "domain": rr.Header().Name}).Debug("Adding new record type to domain")
return rrmap
}
4 changes: 3 additions & 1 deletion dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"errors"
"flag"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
"os"
"strings"
"testing"
"time"
)

var testAddr1 = "0.0.0.0:15353"
Expand Down Expand Up @@ -103,9 +105,9 @@ func startDNSServer(addr string) (*dns.Server, resolver) {
err := server.ListenAndServe()
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
}()
time.sleep(2)
return server, resolver{server: addr}
}

Expand Down
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package main

import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris"
"github.com/miekg/dns"
"github.com/op/go-logging"
"os"
)

// Logging config
var log = logging.MustGetLogger("acme-dns")

// DNSConf is global configuration struct
var DNSConf DNSConfig

Expand All @@ -25,7 +22,7 @@ func main() {
// Read global config
configTmp, err := readConfig("config.cfg")
if err != nil {
fmt.Printf("Got error %v\n", DNSConf.Logconfig.File)
fmt.Printf("Got error %v\n", err)
os.Exit(1)
}
DNSConf = configTmp
Expand Down
38 changes: 10 additions & 28 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package main
import (
"crypto/rand"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/op/go-logging"
log "github.com/Sirupsen/logrus"
"github.com/satori/go.uuid"
"math/big"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -70,34 +68,18 @@ func newACMETxt() (ACMETxt, error) {
}

func setupLogging() {
var logformat = logging.MustStringFormatter(DNSConf.Logconfig.Format)
var logBackend *logging.LogBackend
switch DNSConf.Logconfig.Logtype {
default:
// Setup logging - stdout
logBackend = logging.NewLogBackend(os.Stdout, "", 0)
case "file":
// Logging to file
logfh, err := os.OpenFile(DNSConf.Logconfig.File, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("Could not open log file %s\n", DNSConf.Logconfig.File)
os.Exit(1)
}
defer logfh.Close()
logBackend = logging.NewLogBackend(logfh, "", 0)
if DNSConf.Logconfig.Format == "json" {
log.SetFormatter(&log.JSONFormatter{})
}
logFormatter := logging.NewBackendFormatter(logBackend, logformat)
logLevel := logging.AddModuleLevel(logFormatter)
switch DNSConf.Logconfig.Level {
default:
logLevel.SetLevel(logging.DEBUG, "")
case "warning":
logLevel.SetLevel(logging.WARNING, "")
case "error":
logLevel.SetLevel(logging.ERROR, "")
log.SetLevel(log.WarnLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
logLevel.SetLevel(logging.INFO, "")
log.SetLevel(log.InfoLevel)
case "error":
log.SetLevel(log.ErrorLevel)
}
logging.SetBackend(logFormatter)

// TODO: file logging
}
12 changes: 6 additions & 6 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"revision": "99064174e013895bbd9b025c31100bd1d9b590ca",
"revisionTime": "2016-07-17T15:07:09Z"
},
{
"checksumSHA1": "jRtYpPa7CRuA+LP4ELF9c9CjJao=",
"path": "github.com/Sirupsen/logrus",
"revision": "a437dfd2463eaedbec3dfe443e477d3b0a810b3f",
"revisionTime": "2016-11-18T19:45:39Z"
},
{
"checksumSHA1": "kMfAFLobZymMrCOm/Xi/g9gnJOU=",
"path": "github.com/ajg/form",
Expand Down Expand Up @@ -319,12 +325,6 @@
"revision": "b1479103caacaa39319f75e7f57fc545287fca0d",
"revisionTime": "2016-05-20T21:31:28Z"
},
{
"checksumSHA1": "BoXdUBWB8UnSlFlbnuTQaPqfCGk=",
"path": "github.com/op/go-logging",
"revision": "970db520ece77730c7e4724c61121037378659d9",
"revisionTime": "2016-03-15T20:05:05Z"
},
{
"checksumSHA1": "zKKp5SZ3d3ycKe4EKMNT0BqAWBw=",
"origin": "github.com/stretchr/testify/vendor/github.com/pmezard/go-difflib/difflib",
Expand Down

0 comments on commit 4269069

Please sign in to comment.