Skip to content

Commit

Permalink
feat: abstract log provider
Browse files Browse the repository at this point in the history
  • Loading branch information
wintbiit committed Dec 12, 2023
1 parent 25f5474 commit 505920d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
14 changes: 10 additions & 4 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
"go.uber.org/zap/zapcore"
)

type Logger struct {
*zap.SugaredLogger
}

func init() {
logger := NewLogger("main")
zap.ReplaceGlobals(logger)
zap.ReplaceGlobals(logger.SugaredLogger.Desugar())
}

func NewLogger(module string) *zap.Logger {
func NewLogger(module string) *Logger {
level := zapcore.InfoLevel
if utils.C.Debug {
level = zapcore.DebugLevel
Expand All @@ -23,7 +27,7 @@ func NewLogger(module string) *zap.Logger {
return NewLoggerWithLevel(module, level)
}

func NewLoggerWithLevel(module string, level zapcore.Level) *zap.Logger {
func NewLoggerWithLevel(module string, level zapcore.Level) *Logger {
encoder := zap.NewProductionEncoderConfig()
encoder.EncodeTime = zapcore.ISO8601TimeEncoder
encoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
Expand All @@ -38,5 +42,7 @@ func NewLoggerWithLevel(module string, level zapcore.Level) *zap.Logger {
logger.Named(module)
defer logger.Sync()

return logger
return &Logger{
SugaredLogger: logger.Sugar(),
}
}
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package main

import (
"github.com/miekg/dns"
"github.com/wintbiit/ninedns/log"
"github.com/wintbiit/ninedns/server"
"github.com/wintbiit/ninedns/utils"
"go.uber.org/zap"
)

var servers = make(map[string]*server.Server)
var (
servers = make(map[string]*server.Server)
logger = log.NewLogger("main")
)

func main() {
for name, domain := range utils.C.Domains {
serv, err := server.NewServer(&domain, name)
if err != nil {
zap.S().Errorf("Failed to create server for domain %s: %s", name, err)
logger.Errorf("Failed to create server for domain %s: %s", name, err)
continue
}

Expand All @@ -26,10 +29,10 @@ func main() {
}
}()

zap.S().Infof("Nine DNS started on %s", utils.C.Addr)
logger.Infof("Nine DNS started on %s", utils.C.Addr)

serv := &dns.Server{Addr: utils.C.Addr, Net: "udp"}
if err := serv.ListenAndServe(); err != nil {
zap.S().Fatal(err)
logger.Fatal(err)
}
}
7 changes: 4 additions & 3 deletions server/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"sort"
"strings"

"github.com/wintbiit/ninedns/log"

"github.com/wintbiit/ninedns/resolver"

"github.com/miekg/dns"

"github.com/wintbiit/ninedns/model"
"go.uber.org/zap"
)

type RuleSet struct {
model.Rule
*Server
Name string
l *zap.SugaredLogger
l *log.Logger
cidrs []*net.IPNet
}

Expand All @@ -36,7 +37,7 @@ func (s *Server) newRuleSet(name string, rule model.Rule) *RuleSet {
Rule: rule,
Server: s,
Name: name,
l: s.l.Named(s.DomainName + "/" + name),
l: log.NewLogger(s.DomainName + "/" + name),
cidrs: make([]*net.IPNet, len(rule.CIDRs)),
}

Expand Down
17 changes: 5 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ import (

"github.com/wintbiit/ninedns/provider"

"github.com/redis/go-redis/v9"

"github.com/miekg/dns"
"github.com/wintbiit/ninedns/cache"
"github.com/wintbiit/ninedns/log"
"github.com/wintbiit/ninedns/model"
"go.uber.org/zap"
)

type Server struct {
model.Domain
DomainName string
l *zap.SugaredLogger
l *log.Logger
dnsClient *dns.Client
cacheClient cache.API
providers map[string]provider.Provider
Expand All @@ -31,7 +28,7 @@ func NewServer(config *model.Domain, domain string) (*Server, error) {
server := &Server{
Domain: *config,
DomainName: domain,
l: log.NewLogger(domain).Sugar(),
l: log.NewLogger(domain),
rules: make(map[string]*RuleSet),
dnsClient: new(dns.Client),
providers: make(map[string]provider.Provider),
Expand Down Expand Up @@ -177,13 +174,9 @@ func (s *Server) MatchHandler(w dns.ResponseWriter) *RuleSet {

handlerName, err = s.cacheClient.GetRuntimeCache(fmt.Sprintf("handler:%s:%s", ip.String(), s.DomainName))
if err != nil {
if err == redis.Nil {
handlerName = s.matchRuleset(ip, port, zone, network)
if err := s.cacheClient.AddRuntimeCache("handler:"+ip.String(), handlerName, time.Duration(s.TTL)*time.Second); err != nil {
s.l.Errorf("Failed to add runtime cache: %s", err)
}
} else {
s.l.Errorf("Failed to get runtime cache: %s", err)
handlerName = s.matchRuleset(ip, port, zone, network)
if err := s.cacheClient.AddRuntimeCache("handler:"+ip.String(), handlerName, time.Duration(s.TTL)*time.Second); err != nil {
s.l.Errorf("Failed to add runtime cache: %s", err)
}
}

Expand Down
6 changes: 2 additions & 4 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/wintbiit/ninedns/model"
"go.uber.org/zap"
)

var C *model.Config
Expand All @@ -16,15 +15,14 @@ func init() {

f, err := os.Open(*config)
if err != nil {
zap.S().Fatalf("Failed to open config file: %s", err)
panic(err)
}
defer f.Close()

var conf model.Config
if err = UnmarshalFromReader(f, &conf); err != nil {
zap.S().Fatalf("Failed to parse config file: %s", err)
panic(err)
}

C = &conf
zap.S().Debugf("Loaded config: %+v", C)
}

0 comments on commit 505920d

Please sign in to comment.