Browse Source

Switched to logrus

Andrea Barberio 7 năm trước cách đây
mục cha
commit
0f423ad80d
9 tập tin đã thay đổi với 68 bổ sung29 xóa
  1. 19 19
      README.md
  2. 3 1
      cmds/client/main.go
  3. 5 4
      cmds/coredhcp/main.go
  4. 3 1
      config/config.go
  5. 3 1
      coredhcp.go
  6. 26 0
      logger/logger.go
  7. 3 1
      plugins/file/plugin.go
  8. 3 1
      plugins/plugin.go
  9. 3 1
      plugins/server_id/plugin.go

+ 19 - 19
README.md

@@ -38,22 +38,22 @@ Once you have a working configuration in `config.yml` (see [config.yml.example](
 $ cd cmds/coredhcp
 $ go build
 $ sudo ./coredhcp
-2018/12/19 14:27:17 Registering plugin "file"
-2018/12/19 14:27:17 Registering plugin "server_id"
-2018/12/19 14:27:17 Loading configuration
-2018/12/19 14:27:17 Found plugin: `server_id` with 2 args, `[LL 00:de:ad:be:ef:00]`
-2018/12/19 14:27:17 Found plugin: `file` with 1 args, `[leases.txt]`
-2018/12/19 14:27:17 Loading plugins...
-2018/12/19 14:27:17 Loading plugin `server_id`
-2018/12/19 14:27:17 plugins/server_id: loading `server_id` plugin
-2018/12/19 14:27:17 plugins/server_id: using ll 00:de:ad:be:ef:00
-2018/12/19 14:27:17 Loading plugin `file`
-2018/12/19 14:27:17 plugins/file: reading leases from leases.txt
-2018/12/19 14:27:17 plugins/file: loaded 1 leases from leases.txt
-2018/12/19 14:27:17 Starting DHCPv6 listener on [::]:547
-2018/12/19 14:27:17 Waiting
-2018/12/19 14:27:17 Server listening on [::]:547
-2018/12/19 14:27:17 Ready to handle requests
+INFO[2019-01-05T22:28:07Z] Registering plugin "file"
+INFO[2019-01-05T22:28:07Z] Registering plugin "server_id"
+INFO[2019-01-05T22:28:07Z] Loading configuration
+INFO[2019-01-05T22:28:07Z] Found plugin: `server_id` with 2 args, `[LL 00:de:ad:be:ef:00]`
+INFO[2019-01-05T22:28:07Z] Found plugin: `file` with 1 args, `[leases.txt]`
+INFO[2019-01-05T22:28:07Z] Loading plugins...
+INFO[2019-01-05T22:28:07Z] Loading plugin `server_id`
+INFO[2019-01-05T22:28:07Z] plugins/server_id: loading `server_id` plugin
+INFO[2019-01-05T22:28:07Z] plugins/server_id: using ll 00:de:ad:be:ef:00
+INFO[2019-01-05T22:28:07Z] Loading plugin `file`
+INFO[2019-01-05T22:28:07Z] plugins/file: reading leases from leases.txt
+INFO[2019-01-05T22:28:07Z] plugins/file: loaded 1 leases from leases.txt
+INFO[2019-01-05T22:28:07Z] Starting DHCPv6 listener on [::]:547
+INFO[2019-01-05T22:28:07Z] Waiting
+2019/01/05 22:28:07 Server listening on [::]:547
+2019/01/05 22:28:07 Ready to handle requests
 ...
 ```
 
@@ -63,10 +63,10 @@ Then try it with the local test client, that is located under
 $ cd cmds/client
 $ go build
 $ sudo ./client
-2018/12/19 14:29:05 &{ReadTimeout:3s WriteTimeout:3s LocalAddr:[::1]:546 RemoteAddr:[::1]:547}
-2018/12/19 14:29:05 DHCPv6Message
+INFO[2019-01-05T22:29:21Z] &{ReadTimeout:3s WriteTimeout:3s LocalAddr:[::1]:546 RemoteAddr:[::1]:547}
+INFO[2019-01-05T22:29:21Z] DHCPv6Message
   messageType=SOLICIT
-  transactionid=0x6101f0
+  transactionid=0x6d30ff
   options=[
     OptClientId{cid=DUID{type=DUID-LLT hwtype=Ethernet hwaddr=00:11:22:33:44:55}}
     OptRequestedOption{options=[DNS Recursive Name Server, Domain Search List]}

+ 3 - 1
cmds/client/main.go

@@ -6,13 +6,15 @@ package main
 
 import (
 	"flag"
-	"log"
 	"net"
 
+	"github.com/coredhcp/coredhcp/logger"
 	"github.com/insomniacslk/dhcp/dhcpv6"
 	"github.com/insomniacslk/dhcp/iana"
 )
 
+var log = logger.GetLogger()
+
 func main() {
 	flag.Parse()
 

+ 5 - 4
cmds/coredhcp/main.go

@@ -1,11 +1,11 @@
 package main
 
 import (
-	"log"
 	"time"
 
 	"github.com/coredhcp/coredhcp"
 	"github.com/coredhcp/coredhcp/config"
+	"github.com/coredhcp/coredhcp/logger"
 	_ "github.com/coredhcp/coredhcp/plugins/file"
 	_ "github.com/coredhcp/coredhcp/plugins/server_id"
 )
@@ -17,16 +17,17 @@ var (
 )
 
 func main() {
+	logger := logger.GetLogger()
 	config, err := config.Parse()
 	if err != nil {
-		log.Fatal(err)
+		logger.Fatal(err)
 	}
 	server := coredhcp.NewServer(config)
 	if err := server.Start(); err != nil {
-		log.Fatal(err)
+		logger.Fatal(err)
 	}
 	if err := server.Wait(); err != nil {
-		log.Print(err)
+		logger.Print(err)
 	}
 	time.Sleep(time.Second)
 }

+ 3 - 1
config/config.go

@@ -2,15 +2,17 @@ package config
 
 import (
 	"errors"
-	"log"
 	"net"
 	"strconv"
 	"strings"
 
+	"github.com/coredhcp/coredhcp/logger"
 	"github.com/spf13/cast"
 	"github.com/spf13/viper"
 )
 
+var log = logger.GetLogger()
+
 // Config holds the DHCPv6/v4 server configuration
 type Config struct {
 	v       *viper.Viper

+ 3 - 1
server.go → coredhcp.go

@@ -2,16 +2,18 @@ package coredhcp
 
 import (
 	"errors"
-	"log"
 	"net"
 
 	"github.com/coredhcp/coredhcp/config"
 	"github.com/coredhcp/coredhcp/handler"
+	"github.com/coredhcp/coredhcp/logger"
 	"github.com/coredhcp/coredhcp/plugins"
 	"github.com/insomniacslk/dhcp/dhcpv4"
 	"github.com/insomniacslk/dhcp/dhcpv6"
 )
 
+var log = logger.GetLogger()
+
 // Server is a CoreDHCP server structure that holds information about
 // DHCPv6 and DHCPv4 servers, and their respective handlers.
 type Server struct {

+ 26 - 0
logger/logger.go

@@ -0,0 +1,26 @@
+package logger
+
+import (
+	"sync"
+
+	"github.com/sirupsen/logrus"
+)
+
+var (
+	globalLogger   *logrus.Logger
+	getLoggerMutex sync.Mutex
+)
+
+// GetLogger returns a configured logger instance
+func GetLogger() *logrus.Logger {
+	if globalLogger == nil {
+		getLoggerMutex.Lock()
+		defer getLoggerMutex.Unlock()
+		logger := logrus.New()
+		logger.SetFormatter(&logrus.TextFormatter{
+			FullTimestamp: true,
+		})
+		globalLogger = logger
+	}
+	return globalLogger
+}

+ 3 - 1
plugins/file/plugin.go

@@ -5,16 +5,18 @@ import (
 	"errors"
 	"fmt"
 	"io/ioutil"
-	"log"
 	"net"
 	"strings"
 
 	"github.com/coredhcp/coredhcp/handler"
+	"github.com/coredhcp/coredhcp/logger"
 	"github.com/coredhcp/coredhcp/plugins"
 	"github.com/insomniacslk/dhcp/dhcpv4"
 	"github.com/insomniacslk/dhcp/dhcpv6"
 )
 
+var log = logger.GetLogger()
+
 func init() {
 	plugins.RegisterPlugin("file", setupFile6, setupFile4)
 }

+ 3 - 1
plugins/plugin.go

@@ -2,11 +2,13 @@ package plugins
 
 import (
 	"fmt"
-	"log"
 
 	"github.com/coredhcp/coredhcp/handler"
+	"github.com/coredhcp/coredhcp/logger"
 )
 
+var log = logger.GetLogger()
+
 // Plugin represents a plugin object.
 // Setup6 and Setup4 are the setup functions for DHCPv6 and DHCPv4 handlers
 // respectively. Both setup functions can be nil.

+ 3 - 1
plugins/server_id/plugin.go

@@ -3,17 +3,19 @@ package clientport
 import (
 	"errors"
 	"fmt"
-	"log"
 	"net"
 	"strings"
 
 	"github.com/coredhcp/coredhcp/handler"
+	"github.com/coredhcp/coredhcp/logger"
 	"github.com/coredhcp/coredhcp/plugins"
 	"github.com/insomniacslk/dhcp/dhcpv4"
 	"github.com/insomniacslk/dhcp/dhcpv6"
 	"github.com/insomniacslk/dhcp/iana"
 )
 
+var log = logger.GetLogger()
+
 func init() {
 	plugins.RegisterPlugin("server_id", setupServerID6, setupServerID4)
 }