Use wrapped logger type rather than system raw logger lib.

This commit is contained in:
kenshinx 2015-10-13 19:33:51 +08:00
parent 8152713ad2
commit 0ee6f08a1d
4 changed files with 41 additions and 23 deletions

View File

@ -40,8 +40,8 @@ func NewHandler() *GODNSHandler {
resolvConfig := settings.ResolvConfig resolvConfig := settings.ResolvConfig
clientConfig, err := dns.ClientConfigFromFile(resolvConfig.ResolvFile) clientConfig, err := dns.ClientConfigFromFile(resolvConfig.ResolvFile)
if err != nil { if err != nil {
logger.Printf(":%s is not a valid resolv.conf file\n", resolvConfig.ResolvFile) logger.Warn(":%s is not a valid resolv.conf file\n", resolvConfig.ResolvFile)
logger.Println(err) logger.Error(err.Error())
panic(err) panic(err)
} }
clientConfig.Timeout = resolvConfig.Timeout clientConfig.Timeout = resolvConfig.Timeout
@ -69,7 +69,7 @@ func NewHandler() *GODNSHandler {
// } // }
panic("Redis cache backend not implement yet") panic("Redis cache backend not implement yet")
default: default:
logger.Printf("Invalid cache backend %s", cacheConfig.Backend) logger.Error("Invalid cache backend %s", cacheConfig.Backend)
panic("Invalid cache backend") panic("Invalid cache backend")
} }

34
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"log"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -9,12 +8,12 @@ import (
) )
var ( var (
logger *log.Logger logger *GoDNSLogger
) )
func main() { func main() {
logger = initLogger(settings.Log.File) initLogger()
server := &Server{ server := &Server{
host: settings.Server.Host, host: settings.Server.Host,
@ -25,7 +24,7 @@ func main() {
server.Run() server.Run()
logger.Printf("godns %s start", settings.Version) logger.Info("godns %s start", settings.Version)
sig := make(chan os.Signal) sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt) signal.Notify(sig, os.Interrupt)
@ -34,7 +33,7 @@ forever:
for { for {
select { select {
case <-sig: case <-sig:
logger.Printf("signal received, stopping") logger.Info("signal received, stopping")
break forever break forever
} }
} }
@ -43,22 +42,23 @@ forever:
func Debug(format string, v ...interface{}) { func Debug(format string, v ...interface{}) {
if settings.Debug { if settings.Debug {
logger.Printf(format, v...) logger.Debug(format, v...)
} }
} }
func initLogger(log_file string) (logger *log.Logger) { func initLogger() {
if log_file != "" { logger = NewLogger()
f, err := os.Create(log_file)
if err != nil {
os.Exit(1)
}
logger = log.New(f, "[godns]", log.Ldate|log.Ltime)
} else {
logger = log.New(os.Stdout, "[godns]", log.Ldate|log.Ltime)
}
return logger
if settings.Log.Stdout {
logger.SetLogger("console", nil)
}
if settings.Log.File != "" {
config := map[string]interface{}{"file": settings.Log.File}
logger.SetLogger("file", config)
}
logger.SetLevel(settings.Log.LogLevel())
} }
func init() { func init() {

View File

@ -48,10 +48,10 @@ func (s *Server) Run() {
func (s *Server) start(ds *dns.Server) { func (s *Server) start(ds *dns.Server) {
logger.Printf("Start %s listener on %s\n", ds.Net, s.Addr()) logger.Info("Start %s listener on %s\n", ds.Net, s.Addr())
err := ds.ListenAndServe() err := ds.ListenAndServe()
if err != nil { if err != nil {
logger.Fatalf("Start %s listener on %s failed:%s", ds.Net, s.Addr(), err.Error()) logger.Error("Start %s listener on %s failed:%s", ds.Net, s.Addr(), err.Error())
} }
} }

View File

@ -13,6 +13,14 @@ var (
settings Settings settings Settings
) )
var LogLevelMap = map[string]int{
"DEBUG": LevelDebug,
"INFO": LevelInfo,
"NOTICE": LevelNotice,
"WARN": LevelWarn,
"ERROR": LevelError,
}
type Settings struct { type Settings struct {
Version string Version string
Debug bool Debug bool
@ -47,7 +55,17 @@ func (s RedisSettings) Addr() string {
} }
type LogSettings struct { type LogSettings struct {
File string Stdout bool
File string
Level string
}
func (ls LogSettings) LogLevel() int {
l, ok := LogLevelMap[ls.Level]
if !ok {
panic("Config error: invalid log level: " + ls.Level)
}
return l
} }
type CacheSettings struct { type CacheSettings struct {