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

34
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"log"
"os"
"os/signal"
"runtime"
@ -9,12 +8,12 @@ import (
)
var (
logger *log.Logger
logger *GoDNSLogger
)
func main() {
logger = initLogger(settings.Log.File)
initLogger()
server := &Server{
host: settings.Server.Host,
@ -25,7 +24,7 @@ func main() {
server.Run()
logger.Printf("godns %s start", settings.Version)
logger.Info("godns %s start", settings.Version)
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
@ -34,7 +33,7 @@ forever:
for {
select {
case <-sig:
logger.Printf("signal received, stopping")
logger.Info("signal received, stopping")
break forever
}
}
@ -43,22 +42,23 @@ forever:
func Debug(format string, v ...interface{}) {
if settings.Debug {
logger.Printf(format, v...)
logger.Debug(format, v...)
}
}
func initLogger(log_file string) (logger *log.Logger) {
if log_file != "" {
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
func initLogger() {
logger = NewLogger()
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() {

View File

@ -48,10 +48,10 @@ func (s *Server) Run() {
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()
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
)
var LogLevelMap = map[string]int{
"DEBUG": LevelDebug,
"INFO": LevelInfo,
"NOTICE": LevelNotice,
"WARN": LevelWarn,
"ERROR": LevelError,
}
type Settings struct {
Version string
Debug bool
@ -47,7 +55,17 @@ func (s RedisSettings) Addr() string {
}
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 {